Struts2之把数据封装到集合中

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011301372/article/details/85029112

封装复杂类型的参数(集合类型 Collection,Map接口等)
需求:页面中有可能想批量添加一些数据,就使用该技术。把数据封装到集合中
把数据封装到Collection中

  • 因为Collection接口都会有下标值,所有页面的写法会有一些区别,注意:
    <input type="text" name="products[0].name"/>
  • Action中的写法,需要提供products的集合,并且提供get和set方法

把数据封装到Map中

  • Map集合是键值对的形式,页面的写法
    <input type = "text" name="map['one'].name"/>
  • |Action中提供map集合,并且提供get和set方法
public class Regist4Action extends ActionSupport {
    private List<User> list;

    public List<User> getList() {
        return list;
    }

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

    public String execute() throws Exception{
        for(User user :list){
            System.out.println(user);
        }
        return NONE;
    }
}
<h3>向List集合封装数据(默认采用属性驱动的方式)</h3>
<!--后台:List<User> list-->
<form action = "${pageContext.request.contextPath}/regist4.action" method="post">
    姓名:<input type="text" name="list[0].username"/><br/>
    密码:<input type="password" name="list[0].password"/><br/>
    年龄:<input type="password" name="list[0].age"/><br/>

    姓名:<input type="text" name="list[1].username"/><br/>
    密码:<input type="password" name="list[1].password"/><br/>
    年龄:<input type="password" name="list[1].age"/><br/>
    <input type="submit" value="注册">
</form>
  <!--把数据封装到list集合中-->
        <action name="regist4" class="com.zst.demo2.Regist4Action"/>

猜你喜欢

转载自blog.csdn.net/u011301372/article/details/85029112