封装集合类型的数据

来源:互联网 发布:智慧城市业务数据库 编辑:程序博客网 时间:2024/06/05 00:24

--------------------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;

}

publicvoid setUsername(String username) {

this.username = username;

}

 

public String getPassword() {

return password;

}

publicvoid setPassword(String password) {

this.password = password;

}

 

public String getAddress() {

return address;

}

publicvoid 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 ListActionextends ActionSupport {

 

/*

* (1) Action中声明 List对象

*

*注意:是声明,不是创建

*/

private List<User> list;

  

 

/*

* (2)提供 List对象的 get set方法

*/

public List<User> getList() {

return list;

}

  

publicvoid setList(List<User> list) {

this.list = list;

}

 

 

@Override

public String execute()throws Exception {

System.out.println(list);

return NONE;

}

 

}

  

  

  

3)配置Action

  

<?xmlversion="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>

 

<packagename="demo"extends="struts-default"namespace="/">

 

<actionname="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>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>表单</title>

</head>

<body>

<!--

(3)在表单输入项的 name属性值处写 OGNL表达式

 

list[0].username中的 list Action 中声明的 List对象

-->

<formaction="${pageContext.request.contextPath }/list.action"method="post">

 

<!-- list[0]表示 List集合中的第一个 User对象 -->

username:<inputtype="text"name="list[0].username"/>

<br/>

password:<inputtype="password"name="list[0].password"/>

<br/>

address:<inputtype="text"name="list[0].address"/>

 

<br/><br/>

 

<!-- list[0]表示 List集合中的第一个 User对象 -->

username:<inputtype="text"name="list[1].username"/>

<br/>

password:<inputtype="password"name="list[1].password"/>

<br/>

address:<inputtype="text"name="list[1].address"/>

<br/>

<inputtype="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 MapActionextends ActionSupport {

 

/*

* (1) Action中声明 Map对象

*

*注意:是声明,不是创建

*/

private Map<String, User> map;

  

 

/*

* (2)提供 Map对象的 get set方法

*/

public Map<String, User> getMap() {

return map;

}

  

publicvoid 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:

  

<?xmlversion="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>

 

<packagename="demo"extends="struts-default"namespace="/">

 

<actionname="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>

<metahttp-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>表单</title>

</head>

<body>

<!--

(3)在表单输入项的 name属性值处写 OGNL表达式

 

map['one'].username中的 map Action 中声明的 Map对象

-->

<formaction="${pageContext.request.contextPath }/map.action"method="post">

 

<!--

map['one']表示 Map集合中的第一个 User对象

 

one Key值,username+password+address Value

-->

username:<inputtype="text"name="map['one'].username"/>

<br/>

password:<inputtype="password"name="map['one'].password"/>

<br/>

address:<inputtype="text"name="map['one'].address"/>

 

<br/><br/>

 

<!-- map['two']表示 Map集合中的第二个 User对象 -->

username:<inputtype="text"name="map['two'].username"/>

<br/>

password:<inputtype="password"name="map['two'].password"/>

<br/>

address:<inputtype="text"name="map['two'].address"/>

<br/>

<inputtype="submit"value="提交"/>

</form>

</body>

</html>

  

  

  

5)访问路径

  

http://localhost:8080/工程名/map.jsp

  

  

  

  

  

  

  

  

【made by siwuxie095】