在开发Web工程的过程中经常遇到以下几种情况:表单提交成功后对页面进行了刷新操作;表单提交成功后按浏览器返回,在次对表单进行提交;由服务器响应延迟,客户端多次单击“提交”按钮。上面几个动作都会产生一个结果:相同的数据重复提交。
在实际应用中解决重复提交有很多方法,Struts2框架也给出了解决重复提交的办法。在Struts2中我们可以通过TokenInterceptor或TokenSeessionInterceptor这两个拦截器来处理重复提交,
一、TokenInterceptor
1、编写Action类:
package com.test.tokenInterceptor;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
@Override
public String execute() throws Exception{
return SUCCESS;
}
}
@Override
public String execute() throws Exception{
return SUCCESS;
}
}
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>
<package name="struts2" extends="struts-default">
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="Login" class="com.test.tokenInterceptor.LoginAction">
<result name="success">/jsp/success.jsp</result>
<result name="invalid.token">/wrong.jsp</result>
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="token"/>
</action>
</package>
</struts>
3、编写页面文件、包括三个页面:login.jsp,success.jsp,wrong.jsp
------------------------------login.jsp------------------------------------
<
%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
< %@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>login</title>
</head>
<body>
<center>
<s:form action="Login">
<s:token/>
<s:textfield name="usernaame" label="姓名"/>
<s:textfield name="password" label="密码"/>
<s:submit/>
</s:form>
</center>
</body>
</html>
< %@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>login</title>
</head>
<body>
<center>
<s:form action="Login">
<s:token/>
<s:textfield name="usernaame" label="姓名"/>
<s:textfield name="password" label="密码"/>
<s:submit/>
</s:form>
</center>
</body>
</html>
------------------------------success.jsp----------------------------------
<
%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>提交成功</title>
</head>
<body>
<html>
<head>
<title>提交成功</title>
</head>
<body>
操作成功
</body>
</html>
</body>
</html>
------------------------------wrong.jsp-----------------------------------
<
%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
< %@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<center>
<a href="">重复提交了!!!单击返回登录页面</a>
<s:actionerror/>
</center>
</body>
</html>
< %@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<center>
<a href="">重复提交了!!!单击返回登录页面</a>
<s:actionerror/>
</center>
</body>
</html>
二、TokenSeessionInterceptor
该拦截器实现的原理和Token拦截器相似,不同之处在于对无效令牌信息的处理。
<?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="struts2" extends="struts-default">
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
" http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="Login" class="com.test.tokenInterceptor.LoginAction">
<result name="success">/success.jsp</result>
<!--配置tokenSession拦截器-->
<interceptor-ref name="tokenSession"/>
</action>
</package>
</struts>
<interceptor-ref name="tokenSession"/>
</action>
</package>
</struts>
当程序被发现运行以后,TokenSession 拦截器检测到重复提交操作的时候,总是返回第一次成功提交的页面。