对应数据在前台与后天中的交互,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:管理员