--------------------siwuxie095
(一)封装数据到 List 集合
1、具体步骤
(1)在 Action 中声明 List 对象
(2)提供 List 对象的 get 和 set 方法
(3)在表单输入项的 name 属性值处写 OGNL 表达式
2、具体实现
(1)编写实体类
User.java:
package com.siwuxie095.entity;
// User 实体类 public class User {
private String username; private String password; private String address;
public String getUsername() { return username; } public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }
public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }
@Override public String toString() { return "User [username=" + username + ", password=" + password + ", address=" + address + "]"; }
} |
(2)编写 Action
ListAction.java:
package com.siwuxie095.action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport; import com.siwuxie095.entity.User;
/** * 封装数据到 List 集合 */ public class ListAction extends ActionSupport {
/* * (1) 在 Action 中声明 List 对象 * * 注意:是声明,不是创建 */ private List<User> list;
/* * (2) 提供 List 对象的 get 和 set 方法 */ public List<User> getList() { return list; }
public void setList(List<User> list) { this.list = list; }
@Override public String execute() throws Exception { System.out.println(list); return NONE; }
} |
(3)配置 Action
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="demo" extends="struts-default" namespace="/">
<action name="list" class="com.siwuxie095.action.ListAction"></action>
</package>
</struts> |
(4)编写页面
<%@ 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>表单</title> </head> <body> <!-- (3) 在表单输入项的 name 属性值处写 OGNL 表达式
list[0].username 中的 list 即在 Action 中声明的 List 对象 --> <form action="${pageContext.request.contextPath }/list.action" method="post">
<!-- list[0] 表示 List 集合中的第一个 User 对象 --> username:<input type="text" name="list[0].username"/> <br/> password:<input type="password" name="list[0].password"/> <br/> address:<input type="text" name="list[0].address"/>
<br/><br/>
<!-- list[0] 表示 List 集合中的第一个 User 对象 --> username:<input type="text" name="list[1].username"/> <br/> password:<input type="password" name="list[1].password"/> <br/> address:<input type="text" name="list[1].address"/> <br/> <input type="submit" value="提交"/> </form> </body> </html> |
(5)访问路径
http://localhost:8080/工程名/list.jsp
(二)封装数据到 Map 集合
1、具体步骤
(1)在 Action 中声明 Map 对象
(2)提供 Map 对象的 get 和 set 方法
(3)在表单输入项的 name 属性值处写 OGNL 表达式
2、具体实现
(1)编写实体类(同上 User.java)
(2)编写 Action
MapAction.java:
package com.siwuxie095.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport; import com.siwuxie095.entity.User;
/** * 封装数据到 Map 集合 */ public class MapAction extends ActionSupport {
/* * (1) 在 Action 中声明 Map 对象 * * 注意:是声明,不是创建 */ private Map<String, User> map;
/* * (2) 提供 Map 对象的 get 和 set 方法 */ public Map<String, User> getMap() { return map; }
public void setMap(Map<String, User> map) { this.map = map; }
@Override public String execute() throws Exception {
/* * 遍历 Map 集合 * * 先通过 map.keySet() 获得 key * 再通过 map.get(key) 获得 user */ for (String key : map.keySet()) { User user=map.get(key); System.out.println(key+"-"+user); }
//或:直接输出 map //System.out.println(map);
return NONE; } } |
(3)配置 Action
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="demo" extends="struts-default" namespace="/">
<action name="map" class="com.siwuxie095.action.MapAction"></action>
</package>
</struts> |
(4)编写页面
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>表单</title> </head> <body> <!-- (3) 在表单输入项的 name 属性值处写 OGNL 表达式
map['one'].username 中的 map 即在 Action 中声明的 Map 对象 --> <form action="${pageContext.request.contextPath }/map.action" method="post">
<!-- map['one'] 表示 Map 集合中的第一个 User 对象
one 即 Key 值,username+password+address 即 Value 值 --> username:<input type="text" name="map['one'].username"/> <br/> password:<input type="password" name="map['one'].password"/> <br/> address:<input type="text" name="map['one'].address"/>
<br/><br/>
<!-- map['two'] 表示 Map 集合中的第二个 User 对象 --> username:<input type="text" name="map['two'].username"/> <br/> password:<input type="password" name="map['two'].password"/> <br/> address:<input type="text" name="map['two'].address"/> <br/> <input type="submit" value="提交"/> </form> </body> </html> |
(5)访问路径
http://localhost:8080/工程名/map.jsp
【made by siwuxie095】