封装到集合里面
封装数据到List集合
第一步在action声明List 第二步生成list变量的set和get方法 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集合 第二步生成get和set方法 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>