Struts2 处理传入多个值
1. 处理数目不定的字符串
2. 处理数目不定的JavaBean对象
1. 处理数目不定的字符串
action
package com.java1234.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;
}
}
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>
<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>
2. 处理数目不定的JavaBean对象
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>
<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>
model
package com.java1234.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 + "]";
}
}
action
package com.java1234.action;
import java.util.List;
import com.java1234.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;
}
}
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="helloWorld" extends="struts-default">
<action name="hobby" class="com.java1234.action.HobbyAction">
<result name="success">success.jsp</result>
</action>
<action name="student" class="com.java1234.action.StudentAction">
<result name="success">success.jsp</result>
</action>
</package>
</struts>
struts.xml配置
一. pageckage 配置
name 包名
extends 继承
namespace 包命名空间
abstract 抽象包
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="foreground" namespace="/fore" extends="struts-default">
<action name="studentList" class="com.java1234.action.ForeStudent">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
<package name="background" namespace="/back" extends="struts-default">
<action name="studentList" class="com.java1234.action.BackStudent" method="show">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
<package name="infoFilter" abstract="true"></package>
</struts>
注意点: HTTP Status 404 - /HeadStruts2/success.jsp
return SUCCESS的时候它会多了一级/fore/success.jsp。
可以支持EL表达式,绝对路径
action
ForeStudent
package com.java1234.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class ForeStudent extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("执行了ForeStudent Action的默认方法");
return SUCCESS;
}
}
BackStudent
package com.java1234.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class BackStudent extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("执行了BackStudent Action的默认方法");
return SUCCESS;
}
public String show()throws Exception{
System.out.println("执行了 BackStudent show方法");
return SUCCESS;
}
}
一. action 配置
name action名
class 处理类
method 方法
XML
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="foreground" namespace="/fore" extends="struts-default">
<action name="studentList" class="com.java1234.action.ForeStudent">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
<package name="background" namespace="/back" extends="struts-default">
<action name="studentList" class="com.java1234.action.BackStudent" method="show">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
<package name="infoFilter" abstract="true"></package>
</struts>
action
BackStudent
package com.java1234.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class BackStudent extends ActionSupport{
@Override
public String execute() throws Exception {
System.out.println("执行了BackStudent Action的默认方法");
return SUCCESS;
}
public String show()throws Exception{
System.out.println("执行了 BackStudent show方法");
return SUCCESS;
}
}