1. 程式人生 > >struts2前臺傳遞List、Set、Map集合資料到後臺

struts2前臺傳遞List、Set、Map集合資料到後臺

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

主要有簡單類,List集合,Set集合,Map集合資料的在前臺與後天間的傳遞與展示

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程式碼:

<%@ 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屬性的書寫格式

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

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

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

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

4.Map集合使用的是‘變數名.key名.屬性’ 也可以是‘變數名['key'].屬性’

    這裡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:管理員