1.封装数据到对象
实现步骤:
(1)在Action类中实现ActionSupport声明实体类;
(2)生成实体类对象名的set()和get()的方法;
(3)在表单输入项的name属性值里写表达式形式:对象.属性。
实体类:
public class Student implements Serializable{ // 封装 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() { } // 此处省略set和get方法 } |
前台:
<form method="post" action="user_addStu"> 学生1 编号:<input type="text" name="stu.stuNo" /> 姓名:<input type="text" name="stu.stuName" /> 年龄:<input type="text" name="stu.stuAge" /><br/> <input type="submit" value="提交" /> </form> |
Action类:
public class UserAction extends ActionSupport { private Student stu; //此处省略公开setList的set和get方法
public String addStu(){ System.out.println(stu); return SUCCESS; } } |
2封装数据到List集合
List集合使用‘变量名[索引].属性’的形式。
前台:
<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> |
实例类:同上
Action类:
public class UserAction extends ActionSupport { @Element(Student.class) private List<Student> stuList; //此处省略公开setList的set和get方法
public String addStuList(){ for (Student stu : stuList) { System.out.println(stu); } return SUCCESS; } } |
3.封装数据到Map集合
Map集合使用的是‘变量名.key名.属性’ 也可以是‘变量名['key'].属性’。
前台:
<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> |
实体类:同上
Action类:
public class UserAction extends ActionSupport { @Key(String.class) @Element(Student.class) private Map<String, Student> stuMap = new HashMap<>(); //此处省略公开setList的set和get方法
public String addStuMap(){ for (String key : stuMap.keySet()) { System.out.println(key + "----" + stuMap.get(key)); } return SUCCESS; } } |
4.封装数据到Set集合
Set集合比较特殊,必须使用到OGNL中makeNew的运算符来表示。格式:变量名.makeNew[索引].属性
前台:
<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> |
实体类:同上
Action类:
public class UserAction extends ActionSupport { // Student中的标识字段,该字段需要get方法,该配置不可少 @KeyProperty("stuId") @Element(Student.class) private Set<Student> stuSet = new HashSet<>(); //此处省略公开setList的set和get方法
public String addStuSet(){ System.out.println(stuSet); for (Student stu : stuSet) { System.out.println(stu); } return SUCCESS; } } |
注意:案例中的数据是做测试用的,正确做法是从后台出数据。