struts2前臺傳遞List、Set、Map集合數據到後臺

對應數據在前臺與後天中的交互,struts2框架替咱們作了很大部分的數據封裝工做,這裏就關於一些常見類型數據傳遞的格式和配置注意事項作簡單的記錄。html

主要有簡單類,List集合,Set集合,Map集合數據的在前臺與後天間的傳遞與展現java

package com.supre.idisk.model;
 
import java.io.Serializable;
 
public class Student {
 
	private int stuId;
	private String stuNo;
	private String stuName;
	private String stuAge;
	public Student(int stuId, String stuNo, String stuName, String stuAge) {
		super();
		this.stuId = stuId;
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.stuAge = stuAge;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public int getStuId() {
		return stuId;
	}
	public void setStuId(int stuId) {
		this.stuId = stuId;
	}
	public String getStuNo() {
		return stuNo;
	}
	public void setStuNo(String stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public String getStuAge() {
		return stuAge;
	}
	public void setStuAge(String stuAge) {
		this.stuAge = stuAge;
	}
	@Override
	public String toString() {
		return "Student [stuId=" + stuId + ", stuNo=" + stuNo + ", stuName="
				+ stuName + ", stuAge=" + stuAge + "]";
	}
	
}

爲了測試方便就將各類形式數據放在了一塊兒框架

前臺錄入數據的jsp代碼: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>
<h3>簡單對象</h3>
<form method="post" action="user_addStudent">
編號:<input type="text" name="student.stuNo" /><br/>
姓名:<input type="text" name="student.stuName" /><br/>
年齡:<input type="text" name="student.stuAge" /><br/>
<input type="submit" value="提交" />
</form>
<hr>
<h3>List集合</h3>
<form method="post" action="user_addStuList">
學生1 
編號:<input type="text" name="stuList[0].stuNo" />
姓名:<input type="text" name="stuList[0].stuName" />
年齡:<input type="text" name="stuList[0].stuAge" /><br/>
學生2 
編號:<input type="text" name="stuList[1].stuNo" />
姓名:<input type="text" name="stuList[1].stuName" />
年齡:<input type="text" name="stuList[1].stuAge" /><br/>
學生3 
編號:<input type="text" name="stuList[2].stuNo" />
姓名:<input type="text" name="stuList[2].stuName" />
年齡:<input type="text" name="stuList[2].stuAge" /><br/>
<input type="submit" value="提交" />
</form>
<hr>
<h3>Set集合</h3>
<form method="post" action="user_addStuSet">
學生1 
編號:<input type="text" name="stuSet.makeNew[0].stuNo" />
姓名:<input type="text" name="stuSet.makeNew[0].stuName" />
年齡:<input type="text" name="stuSet.makeNew[0].stuAge" /><br/>
學生2 
編號:<input type="text" name="stuSet.makeNew[1].stuNo" />
姓名:<input type="text" name="stuSet.makeNew[1].stuName" />
年齡:<input type="text" name="stuSet.makeNew[1].stuAge" /><br/>
學生3 
編號:<input type="text" name="stuSet.makeNew[2].stuNo" />
姓名:<input type="text" name="stuSet.makeNew[2].stuName" />
年齡:<input type="text" name="stuSet.makeNew[2].stuAge" /><br/>
<input type="submit" value="提交" />
</form>
<hr>
 
<h3>Map集合</h3>
<form method="post" action="user_addStuMap">
學生1 
編號:<input type="text" name="stuMap['stu1'].stuNo" />
姓名:<input type="text" name="stuMap['stu1'].stuName" />
年齡:<input type="text" name="stuMap['stu1'].stuAge" /><br/>
學生2 
編號:<input type="text" name="stuMap.stu2.stuNo" />
姓名:<input type="text" name="stuMap.stu2.stuName" />
年齡:<input type="text" name="stuMap.stu2.stuAge" /><br/>
學生3 
編號:<input type="text" name="stuMap['stu3'].stuNo" />
姓名:<input type="text" name="stuMap['stu3'].stuName" />
年齡:<input type="text" name="stuMap['stu3'].stuAge" /><br/>
<input type="submit" value="提交" />
</form>
<hr>
</body>
</html>

說明:主要是name屬性的書寫格式ide

1.簡單類直接使用‘變量名.屬性’的形式post

2. List集合使用‘變量名[索引].屬性’的形式測試

3. Set集合比較特殊,必須使用到OGNL中makeNew的運算符來表示ui

       格式:‘變量名.makeNew[索引].屬性’this

4.Map集合使用的是‘變量名.key名.屬性’ 也能夠是‘變量名['key'].屬性’debug

    這裏key使用是String類型,上面兩種形式均可以,其餘類型的key就須要本身親測了
後臺接收數據的Action類代碼:
 

package com.supre.idisk.action;
 
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.Element;
import com.opensymphony.xwork2.util.Key;
import com.opensymphony.xwork2.util.KeyProperty;
import com.supre.idisk.model.Student;
 
public class UserAction extends ActionSupport {
	
	private Student student;
    @Element(Student.class)
	private List<Student> stuList;
	@KeyProperty("stuId")  //Student中的標識字段,該字段須要get方法,該配置不可少
	@Element(Student.class)  
	private Set<Student> stuSet = new HashSet<>();
	@Key(String.class)
	@Element(Student.class)
	private Map<String, Student> stuMap = new HashMap<>();
	
	public String addStudent(){
		System.out.println("-------簡單對象");
		System.out.println(student);
		return SUCCESS;
	}
	
	public String addStuList(){
		System.out.println("-------List集合");
		for (Student stu : stuList) {
			System.out.println(stu);
		}
		return SUCCESS;
	}
	
	public String addStuSet(){
		System.out.println("-------Set集合");
		System.out.println(stuSet);
		for (Student stu : stuSet) {
			System.out.println(stu);
		}
		return SUCCESS;
	}	
	
	public String addStuMap(){
		System.out.println("-------Map集合");
		for (String key : stuMap.keySet()) {
			System.out.println(key + "----" + stuMap.get(key));
		}
		return SUCCESS;
	}
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public List<Student> getStuList() {
		return stuList;
	}
	public void setStuList(List<Student> stuList) {
		this.stuList = stuList;
	}
	public Set<Student> getStuSet() {
		return stuSet;
	}
	public void setStuSet(Set<Student> stuSet) {
		this.stuSet = stuSet;
	}
	public Map<String, Student> getStuMap() {
		return stuMap;
	}
	public void setStuMap(Map<String, Student> stuMap) {
		this.stuMap = stuMap;
	}
	
	
}

注意:

1.其中的變量必須提供標準的get和set方法

2.Set集合和Map集合必須初始化,即在定義的時候就賦上對象

3. Set集合上的KeyProperty註解必須配上,其餘註解配置都可以省略。

4.這裏的配置也可使用配置文件實現,想了解的能夠搜索:struts2類型轉換配置
前臺對相關數據的展現jsp代碼!

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
<h3>簡單對象</h3>
編號:${student.stuNo}<br/>
姓名:${student.stuName}<br/>
年齡:${student.stuAge}<br/>
<hr>
<h3>List集合</h3>
<s:iterator value="stuList" var="bean" status="state">
	學生${state.index+1}:
	編號:${bean.stuNo}
	姓名:${bean.stuName}
	年齡:${bean.stuAge}<br/>
</s:iterator>
<hr>
<h3>Set集合</h3>
<s:iterator value="stuSet" var="bean" status="state">
	學生${state.index+1}:
	編號:${bean.stuNo}
	姓名:${bean.stuName}
	年齡:${bean.stuAge}<br/>
</s:iterator>
<hr>
<h3>Map集合</h3>
<s:iterator value="stuMap" var="bean" status="state">
	學生${bean.key}:
	編號:${bean.value.stuNo}
	姓名:${bean.value.stuName}
	年齡:${bean.value.stuAge}<br/>
</s:iterator>
<hr>
</body>
</html>

說明:關於展現主要仍是使用el表達式,這裏Map的展現是須要使用到'key'和'value'
總結:

List集合和Map集合傳到後臺比較簡單,只須要提供get和set方法,同時Map初始化,在前臺jsp中安指定格式傳遞,後臺就能收到數據。

Set比較特殊

1先前臺jsp必須使用makeNew,即格式‘變量名.makeNew[索引].屬性’

2 後臺set必須配置@KeyProperty,同時須要提供get和set方法,以及初始化

 

Set集合注意:這裏若是屬性中是一個對象,即採用‘變量名.makeNew[索引].屬性.屬性’的形式傳遞數據的時候,在後臺接到Set集合中的數據都同樣的。

例如:假如Student中有一個屬性爲Group對象

使用

<input type='text'name='stuSet.makeNew[0].group.groupId'> //這裏輸入 1
<input type='text'name='stuSet.makeNew[1].group.groupId'>   //這裏輸入2
<input type='text'name='stuSet.makeNew[2].group.groupId'>   //這裏輸入3

假如後臺遍歷打印groupId即

for (Student stu : stuList) {
   System.out.println(stu.group.groupId);
}

時,發現打印的數據所有同樣的。

這個初步估計應該是跟Set集合的特性有關,暫時未去深究緣由以及解決辦法,後續中再抽時間去探討這個現象

補充:

         若是後臺用Map<String,Object>map接前臺的數據,前臺仍是以map.key的形式傳值,則後臺拿到的數據中,map.get(key)取到的數據爲String[]類型,能夠經過debug查看,則值前臺傳遞來的值會按前臺map.key的存放在map.get(key)中。固然後臺若是仍是Map<String String> 接值的話就不存在String[],若是前臺出現同名,也只會以「, 」拼接在一塊兒放入value中。

例如

前臺有:這裏其餘標籤就加上了

<input type="text" name="map.userNo" value="admin">
<input type="text" name="map.userNo" value="zhangsan">
<input type="text" name="map.userNo" value="lisi">
<input type="text" name="map.userName" value="管理員">

(1)後臺:Map<String,Object>map接值

Set<String> keys=map.keySet()
for(String key:keys){
	//String  value =  (String) map.get(key);  會報錯
	String[]  value =  (String[]) map.get(key);
	System.out.println("key:"+key);
	for(String val:value){
	System.out.println("value:"+val);
	}
}

打印結果:

key:userNo
value:admin
value:zhangsan

value:lisi
key:userName
value:管理員

(2)後臺:Map<String,String>map接值

Set<String> keys=map.keySet()
for(String key:keys){
	String value = map.get(key);  		
	System.out.println("key:"+key);
	System.out.println("value:"+value);
}

打印結果:

key:userNo
value:admin, zhangsan, lisi

key:userName
value:管理員
相關文章
相關標籤/搜索