Struts2--逻辑视图,复杂类型数据封装

INPUT逻辑视图配置

  数据校验或者参数类型传唤错误时,配置input逻辑视图

1.struts.xml配置文件。配置input错误的结果页面

   <package name="demo04" extends="struts-default" namespace="/">
        <!--配置全局input结果页面-->
        <global-results>
            <result name="input">/index.jsp</result>
        </global-results>
        <action name="userAction3" class="com.action.UserAction3" method="m1">
            <result name="users">/jsp/users.jsp </result>
        </action>
    </package>

2.jsp页面配置,引入sturts的标签,和在form表单内说明。

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<!--引入struts2的标签库-->
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<body>
    <h2> 模型驱动:采用模型驱动方式</h2>
    <s:fielderror/> <%-- input转换异常时,会有提示消息显示--%>
    <form action="${pageContext.request.contextPath}/userAction3.action" method="post">
        <input type="password" name="id" value="110"><br>
        <input type="text" name="uname" value="cat"><br>
        <input type="password" name="pwd" value="abc"><br>
        <input type="submit"><br>
    </form>
</body>
</html>

页面请求数据封装到List集合当中

1.Product实体类

public class Product {
    private String name;
    private Integer price;

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }
}

2.from表单

<form action="/productSave.action" method="post">
    名称:<input type="text" name="list[0].name"><br>
    价格:<input type="text" name="list[0].prict"><br>
    名称:<input type="text" name="list[1].name"><br>
    价格:<input type="text" name="list[1].prict"><br>
    名称:<input type="text" name="list[2].name"><br>
    价格:<input type="text" name="list[2].prict"><br>
    <input type="submit">
</form>

3.struts配置文件

  <package name="demo05" extends="struts-default" namespace="/">
        <!--配置全局input结果页面-->
        <global-results>
            <result name="input">/index.jsp</result>
        </global-results>
        <action name="productSave" class="com.action.ProductAction" method="m1">
        </action>
    </package>

4.Action类

public class ProductAction extends ActionSupport {
    private List<Product> list;
  //必须提供get,set方法
    public List<Product> getList() {
        return list;
    }

    public void setList(List<Product> list) {
        this.list = list;
    }

    public String m1(){
        for (Product product : list) {
            System.out.println("list:"+product);
        }
        return NONE;
    }
}

 

 

页面请求数据封装到Map集合当中

1.form表单

<form action="/productMap.action" method="post">
    名称:<input type="text" name="map['o'].name"><br>
    价格:<input type="text" name="map['o'].price"><br>
    名称:<input type="text" name="map['t'].name"><br>
    价格:<input type="text" name="map['t'].price"><br>
    名称:<input type="text" name="map['th'].name"><br>
    价格:<input type="text" name="map['th'].price"><br>
    <input type="submit">
</form>

2.struts配置文件 

 <package name="demo05" extends="struts-default" namespace="/">
        <!--配置全局input结果页面-->
        <global-results>
            <result name="input">/index.jsp</result>
        </global-results>
        <action name="productSave" class="com.action.ProductAction1" method="m1">
        </action>
        <action name="productMap" class="com.action.ProductAction2" method="m1">
        </action>
    </package>

3.Action类

public class ProductAction2 extends ActionSupport {
    private Map<String,Product> map;

    public Map<String, Product> getMap() {
        return map;
    }

    public void setMap(Map<String, Product> map) {
        this.map = map;
    }

    public String m1(){
        for (String key : map.keySet()) {
            Product product=map.get(key);
            System.out.println(key+"  "+product);
        }
        return NONE;
    }
}

 

  

 

posted @ 2020-03-12 14:41  Jason–json  阅读(207)  评论(0编辑  收藏  举报