struts2之防止表单重复提交

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.ui.theme" value="simple" />

    <!-- struts2在防止表单重复提交的拦截中有2个,分别是:token,tokenSession。tokenSession继承token而来。
          通常情况下,使用tokenSession客户端感觉会比较友好。 -->
    <!-- 如果重复提交,会跳转到error.jsp页面 -->
    <package name="person" namespace="/test" extends="struts-default">
        <action name="token" class="com.ljq.action.PersonAction">
            <interceptor-ref name="defaultStack" />
            <interceptor-ref name="token" />
            <!-- 如果重复提交,跳转到error.jsp页面 -->
            <result name="invalid.token">/WEB-INF/page/error.jsp</result> 
            <result>/WEB-INF/page/message.jsp</result>
        </action>
        <action name="tokenSession" class="com.ljq.action.PersonAction">
            <interceptor-ref name="defaultStack" />
            <interceptor-ref name="tokenSession" />
            <!-- 如果重复提交,不会跳转到error.jsp页面 -->
            <result name="invalid.token">/WEB-INF/page/error.jsp</result> 
            <result>/WEB-INF/page/message.jsp</result>
        </action>
    </package>
</struts>


PersonAction类
package com.ljq.action;

import java.util.ArrayList;
import java.util.List;

public class PersonAction {
    
    private String name;

    @SuppressWarnings("unchecked")
    //观看控制台
    //如果token生效则不会在控制台输出name的值,而会输出如下警告: 2011-3-14 20:45:32 com.opensymphony.xwork2.util.logging.commons.CommonsLogger 
    //warn 警告: Form token EDZ4S96RNDN5VD8B1CQTK6FTHIJUPC66 does not match the session token null.
    public String execute() {
        List ls = new ArrayList();
        ls.add(name);
        for (int i = 0; i < ls.size(); i++) {
            System.out.println(ls.get(i));
        }
        return "success";
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


index.jsp表单页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>防止表单重复提交</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
  </head>
  
  <body>
      <!-- 防止表单重复提交,记得在form表单里填上<s:token></s:token>      -->
      <!-- action="token"、action="tokenSession" -->
      <s:form action="token" namespace="/test" method="post">
          姓名:<s:textfield name="name"/><s:token></s:token>
          <input type="submit" value="发送"/>
      </s:form>
  </body>
</html>


message.jsp返回成功页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
  </head>
  
  <body>
     <s:property value="name"/><br/>
     <%=new Date() %>
  </body>
</html>



error.jsp表单重复提交提示页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'error.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
      您已经提交了表单,请不要重复提交。
  </body>
</html>


转载地址:http://www.cnblogs.com/linjiqin/archive/2011/03/14/1984177.html






首先要在jsp的from标签里加入<s:token/>防重复提交标签,<s:token/> 生成如下的内容:(struts.token.name 标识哪个隐藏域存了 token 值)
     <input type="hidden" name="struts.token.name" value="struts.token"/>
     <input type="hidden" name="struts.token" value="7GXL55LPSGU19SDC9D3VP54I20XT3BVA"/>
注意自定义的表单域别重名了。它的作用是防止表单重复提交,每次加载页面 struts.token 的值都不一样,如果两次提交时该值一样,则认为是重复提交。此时要启用 TokenInterceptor(token) 拦截器,最好是也启用 TokenSessionStoreInterceptor(token-session) 拦截器,不然后台会出现错误提示:

2008-11-17 20:39:21 com.opensymphony.xwork2.interceptor.ParametersInterceptor setParameters
严重: ParametersInterceptor - [setParameters]: Unexpected Exception catched: Error setting expression 'struts.token' with value '[Ljava.lang.String;@1c2e163'
2008-11-17 20:39:21 com.opensymphony.xwork2.interceptor.ParametersInterceptor setParameters
严重: ParametersInterceptor - [setParameters]: Unexpected Exception catched: Error setting expression 'struts.token.name' with value '[Ljava.lang.String;@abaf8c'

但不影响使用。不过如果只有 token-session 拦截器却是不行的。

token 和 token-session 拦截器的启用,是在 struts.xml 配置文件中,既可以为包启用,也可以单独为某个 action 启用:

1) 为包启用 token 和 token-session
<package name="TestStruts" extends="struts-default"> 
    <interceptors> 
    <interceptor-stack name="myStack"> 
        <interceptor-ref name="token"/> 
        <interceptor-ref name="token-session"/> 
    <interceptor-ref name="defaultStack" />              
    </interceptor-stack> 
    </interceptors> 
    <default-interceptor-ref name="myStack" /> 
    <action name="Login" class="com.unmi.struts2.action.LoginAction"> 
        <result name="input">/login.jsp</result> 
        <result name="invalid.token">/exception.jsp</result> 
    </action> 
............................................................................
2) 为 Action 启用 token 和 token-session
<action name="Login" class="com.unmi.struts2.action.LoginAction"> 
    <interceptor-ref name="token" /> 
    <interceptor-ref name="token-session" /> 
    <interceptor-ref name="defaultStack" /> 
    <result name="input">/login.jsp</result> 
    <result name="invalid.token">/exception.jsp</result>  
</action>
............................................................................

注意 token、token-session 和 defaultStack 的顺序要保证,还需要加上名为 "invalid.token" 的 result,当发现重复提交时转向到这个逻辑页,如 /exception.jsp,在 /exception.jsp 加上 <s:actionerror /> 在出现重复提交时就会提示:The form has already been processed or no token was supplied, please try again.
<interceptor-ref name="token"/>
<interceptor-ref name="token-session"/>
<!--注意struts2.0 拦截器名字为token-session struts2.1.2 已经更改为tokenSession -->
token: 在活动中检查合法令牌(token), 防止表单的重复提交;
token-session: 同上, 但是在接到非法令牌时将提交的数据保存在session中;

你可能感兴趣的:(struts2)