Struts2 使用token拦截器控制(防止)重复,刷新,后退 提交数据

使用这个拦截器非常简单,也是别人那里转来的,就是这个描述的很清楚,所以转过来,希望能帮助到更多的人。

先来个图片流程:



具体代码:

input.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello Struts2</title>
</head>
<body>
    <form action="user.action" method="post">
        name:<input type="text" name="name"/>
        age:<input type="text" name="age"/>
        <input type="submit" value="提 交"/>
        <s:token></s:token>
    </form>
</body>
</html>

addOK.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello Struts2</title>
</head>
<body>
    addOK.
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello Struts2</title>
</head>
<body>
    不可重复提交数据!
<a href="${pageContext.request.contextPath}/index.jsp">返回主页</a>
</body>
</html>



struts.xml


<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.configuration.xml.reload" value="true"/>
    
    <package name="test" namespace="/" extends="struts-default">
    
        <action name="input" class="com.bjsxt.action.InputAction">
            <result>/input.jsp</result>
        </action>
    
        <action name="user" class="com.bjsxt.action.UserAction">
            <result>/addOK.jsp</result>
	    <!--注意这三个是有顺序问题的,在某些博客上没有注明这点,害死人-->
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <interceptor-ref name="token"></interceptor-ref>
            <result name="invalid.token">/error.jsp</result>
        </action>
    </package>
</struts>



userAction


package com.bjsxt.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

    private static final long serialVersionUID = -8003780600877800393L;
    
    private String name;
    
    private int age;
    
    public String execute(){
        System.out.println("a user added!");
        return SUCCESS;
    }
    
    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;
    }
    
}


如有问题,可留言!!!







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值