struts2处理传入多个值
程序员文章站
2022-07-12 16:13:42
...
1. struts2处理传入多个值
1) 处理数目不定的字符串
2) 处理数目不定的JavaBean对象
新建项目HeadFirstStruts2Chap02_02
1) 处理数目不定的字符串
HobbyAction.java package com.andrew.action; import com.opensymphony.xwork2.ActionSupport; public class HobbyAction extends ActionSupport { private String[] hobby; public String[] getHobby() { return hobby; } public void setHobby(String[] hobby) { this.hobby = hobby; } @Override public String execute() throws Exception { System.out.println("执行了HobbyAction的默认方法"); if (hobby != null) { for (String h : hobby) { System.out.println(h); } } return SUCCESS; } } struts.xml <action name="hobby" class="com.andrew.action.HobbyAction"> <result name="success">success.jsp</result> </action> hobby.jsp <form action="hobby" method="post"> 爱好: <input type="checkbox" name="hobby" value="唱歌"/>唱歌 <input type="checkbox" name="hobby" value="跳舞"/>跳舞 <input type="checkbox" name="hobby" value="睡觉"/>睡觉 <input type="checkbox" name="hobby" value="玩CF"/>玩CF <input type="submit" value="提交"/> </form> success.jsp OK! http://localhost:8080/HeadFirstStruts2Chap02_02/hobby.jsp 运行结果: Ok! 控制台: 执行了HobbyAction的默认方法 唱歌 跳舞 睡觉 玩CF
2) 处理数目不定的JavaBean对象
Student.java package com.andrew.model; public class Student { private String name; private String sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } } StudentAction.java package com.andrew.action; import java.util.List; import com.andrew.model.Student; import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport { private List<Student> students; public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } @Override public String execute() throws Exception { System.out.println("执行了StudentAction的默认方法"); for (Student s : students) { System.out.println(s); } return SUCCESS; } } struts.xml <action name="student" class="com.andrew.action.StudentAction"> <result name="success">success.jsp</result> </action> addstudents.jsp <form action="student" method="post"> <table> <tr><th>姓名</th><th>性别</th><th>年龄</th></tr> <tr> <td><input type="text" name="students[0].name"/></td> <td><input type="text" name="students[0].sex"/></td> <td><input type="text" name="students[0].age"/></td> </tr> <tr> <td><input type="text" name="students[1].name"/></td> <td><input type="text" name="students[1].sex"/></td> <td><input type="text" name="students[1].age"/></td> </tr> <tr><td colspan="3"><input type="submit" value="提交"/></td></tr> </table> </form> success.jsp Ok! http://localhost:8080/HeadFirstStruts2Chap02_02/hobby.jsp a 1 11 b 2 12 submit 运行结果: Ok! 控制台: 执行了StudentAction的默认方法 Student [name=a, sex=1, age=11] Student [name=b, sex=2, age=12]
上一篇: struts2配置struts.xml
推荐阅读
-
struts2处理传入多个值
-
mysql表默认null 但是传入空值 laravel怎么处理是正确姿势?
-
使用redis缓存,缓存key值为区间值(可做处理),当传入区间中的一个区间数时,怎么找到对应的缓存
-
高级Ibatis查询(动态传入多个值、在不同时间断查询)
-
php 接受多个name相同的值 以数组的形式处理
-
mysql表默认null 但是传入空值 laravel怎么处理是正确姿势?
-
使用redis缓存,缓存key值为区间值(可做处理),当传入区间中的一个区间数时,怎么找到对应的缓存
-
php 接受多个name相同的值 以数组的形式处理
-
解决php 处理 form 表单提交多个 name 属性值相同的 input 标签问题
-
mysql表默认null 但是传入空值 laravel怎么处理是正确姿势?