欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2防重复提交

程序员文章站 2022-07-12 16:14:30
...
1. struts2防重复提交

Student.java

package com.andrew.model;
public class Student {
    private String name;
    private String age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
}

StudentAction.java

package com.andrew.action;
import com.andrew.model.Student;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport {
    private Student student;
    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public String add() throws Exception {
        System.out.println("开始添加学生:" + student);
        Thread.sleep(5000);
        System.out.println(student.getName() + "添加完成");
        return SUCCESS;
    }
}

student.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
<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>

success.jsp

学生添加成功!


2. 使用<s:token/>标签防重复提交

<s:token></s:token>:加在form里;
使用token拦截器:
<interceptor-refname="token"></interceptor-ref>
<interceptor-refname="defaultStack"></interceptor-ref>
<resultname="invalid.token">/student.jsp</result>:在struts.xml里配置,假如出现重复提交,则直接回到页面;
<s:actionerror/>:在页面上显示错误信息;

struts.xml

<package name="manager" extends="struts-default">
    <action name="student" class="com.andrew.action.StudentAction" method="add">
        <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>

http://localhost:8080/HeadFirstStruts2Chap09/student.jsp
a01 11 submit
submit
The form has already been processed or no token was supplied, please try again.
http://localhost:8080/HeadFirstStruts2Chap09/student
学生添加成功! 


3. 使用tokenSession拦截器防重复提交

tokenSesssion拦截器直接无视重复提交的请求;
<interceptor-refname="tokenSession"></interceptor-ref>
<interceptor-refname="defaultStack"></interceptor-ref>

struts.xml

<package name="manager" extends="struts-default">
    <action name="student" class="com.andrew.action.StudentAction" method="add">
        <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> -->
        <interceptor-ref name="tokenSession"></interceptor-ref>
        <interceptor-ref name="defaultStack"></interceptor-ref>
    </action>
</package>

http://localhost:8080/HeadFirstStruts2Chap09/student.jsp
a02 12 submit
submit
控制台:
警告: Form token 7PUG6529S0A9H8YD7I15HOY6ITPDYJDK does not match the session token null.
http://localhost:8080/HeadFirstStruts2Chap09/student
学生添加成功!
相关标签: Java struts2