1复制项目HeadFirstStruts2chapter08 改名 HeadFirstStruts2chapter09,web project settings,新建com.cruise.model包, Student类,新建com.cruise.action包,类StudentAction。
Student如下:
package com.cruise.model;
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
StudentAction如下:设置延迟5秒,模拟网络延迟
package com.cruise.action;
import com.cruise.model.Student;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport{
Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String addStudent() throws Exception {
System.out.println("添加学生"+student.getName());
Thread.sleep(5000);
return super.execute();
}
}
2配置struts.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>
<constant name="struts.multipart.maxSize" value="20000000"></constant>
<package name="manager" extends="struts-default">
<action name="student" class="com.cruise.action.StudentAction" method="addStudent">
<result name="success" >/success.jsp</result>
</action>
</package>
</struts>
3success.jsp 文件,
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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>
学生添加成功!!!
</table>
</body>
</html>
4student.jsp文件,
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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>
添加学生:<br>
<form action="student" method="post" >
学生姓名:<input type="text" name="student.name"/><br>
学生年龄:<input type="text" name="student.age"/><br>
<input type="submit" value="提交" />
</form>
</table>
</body>
</html>
5测试http://localhost:8080/HeadFirstStruts2chapter09/student.jsp 再服务器响应之前,如果多次点击会出现重复提交的现象,
第一种解决方案:
8在form表单中添加标签和actionerror标签,在struts.xml中,加入token拦截器,
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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>
添加学生:<br>
<s:actionerror/>
<form action="student" method="post" >
<s:token></s:token>
学生姓名:<input type="text" name="student.name"/><br>
学生年龄:<input type="text" name="student.age"/><br>
<input type="submit" value="提交" />
</form>
</table>
</body>
</html>
struts.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>
<constant name="struts.multipart.maxSize" value="20000000"></constant>
<package name="manager" extends="struts-default">
<action name="student" class="com.cruise.action.StudentAction" method="addStudent">
<result name="success" >/success.jsp</result>
<result name="invalid.token">/student.jsp</result>
<interceptor-ref name="token"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
9测试http://localhost:8080/HeadFirstStruts2chapter09/student.jsp
第二种解决方案:
10配置struts.xml文件,jsp文件不用动
<?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>
<constant name="struts.multipart.maxSize" value="20000000"></constant>
<package name="manager" extends="struts-default">
<action name="student" class="com.cruise.action.StudentAction" method="addStudent">
<result name="success" >/success.jsp</result>
<interceptor-ref name="tokenSession"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
</struts>
11测试http://localhost:8080/HeadFirstStruts2chapter09/student.jsp