struts2_day02_15-封装数据到list集合_16-封装数据到map集合

来源:互联网 发布:数码兽数据库 黄龙兽 编辑:程序博客网 时间:2024/06/17 10:50

封装到集合里面

封装数据到List集合

第一步action声明List

第二步生成list变量的setget方法

package com.hlg.data;import java.util.List;import com.hlg.entity.Book;import com.hlg.entity.User;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;/** * 封装数据到list * @author Administrator * */public class ListAction extends ActionSupport{private List<User> list;public List<User> getList() {return list;}public void setList(List<User> list) {this.list = list;}public String execute(){System.out.println(list);return NONE;}}


<action name="list" class="com.hlg.data.ListAction"></action>


第三步在表单输入项里面写表达式

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="${pageContext.request.contextPath }/list.action" method="post">username:<input type="text" name="list[0].username"><br>password:<input type="text" name="list[0].password"><br>address:<input type="text" name="list[0].address"><br><br>username:<input type="text" name="list[1].username"><br>password:<input type="text" name="list[1].password"><br>address:<input type="text" name="list[1].address"><br><br><input type="submit" value="提交"></form></body></html>




封装数据到Map集合

第一步声明map集合

第二步生成getset方法

package com.hlg.data;import java.util.Map;import com.hlg.entity.User;import com.opensymphony.xwork2.ActionSupport;/** * 封装数据到list * @author Administrator * */public class MapAction extends ActionSupport{private Map<String,User> map;public Map<String, User> getMap() {return map;}public void setMap(Map<String, User> map) {this.map = map;}public String execute(){System.out.println(map);return NONE;}}


第三步在表单输入项的name属性值里面写表达式


/struts2_day02/WebContent/map.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="${pageContext.request.contextPath }/map.action" method="post">username:<input type="text" name="map['one'].username"><br>password:<input type="text" name="map['one'].password"><br>address:<input type="text" name="map['one'].address"><br><br>username:<input type="text" name="map['abc'].username"><br>password:<input type="text" name="map['abc'].password"><br>address:<input type="text" name="map['abc'].address"><br><br><input type="submit" value="提交"></form></body></html>


阅读全文
0 0
原创粉丝点击