一、struts2接收传入多个值
1.配置struts.xml文件,success.jsp
<action name="hobby" class="com.newbeedaly.action.HobbyAction">
<result name="success">success.jsp</result>
</action>
<action name="student" class="com.newbeedaly.action.StudentAction">
<result name="success">success.jsp</result>
</action>
<%@ 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>
Ok!
</body>
</html>
2.多值接收hobby.jsp,HobbyAction.java
<%@ 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>
<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>
</body>
</html>
package com.newbeedaly.action;
import com.opensymphony.xwork2.Action;
public class HobbyAction implements Action{
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("执行了Action的默认方法");
if(hobby!=null){
for(String h:hobby){
System.out.println(h);
}
}
return SUCCESS;
}
}
3.多个对象接收,Student.java,addStudents.jsp,StudentAction.java
package com.newbeedaly.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 + "]";
}
}
<%@ 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>
<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>
</body>
</html>
package com.newbeedaly.action;
import java.util.List;
import com.newbeedaly.model.Student;
import com.opensymphony.xwork2.Action;
public class StudentAction implements Action{
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("执行了Action的默认方法");
for(Student s:students){
System.out.println(s);
}
return SUCCESS;
}
}
二、struts2的struts.xml配置
<struts>
<!-- name值没有任何作用,主要为了划分模块 -->
<!-- namespace 请求Action时url必须添加,不同包下action的name可以重复,extend可以继承拦截器 -->
<package name="foreground" namespace="/fore" extends="struts-default">
<!-- 不加el表达式,默认访问fore目录下的jsp -->
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
<package name="background" namespace="/back" extends="struts-default">
<action name="studentList" class="com.newbeedaly.action.BackStudent" method="show">
<!-- method指定执行方法 -->
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
<!-- 抽象包,定义拦截器,用其他包继承 -->
<package name="infoFilter" abstract="true"></package>
</struts>