Struts2防止表单重复提交

Struts2防止表单重复提交

struts.xml配置文件

<?xml version="1.0"encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

   "-//Apache Software Foundation//DTD Struts Configuration2.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页面 -->

           <resultname="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页面 -->

           <resultname="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 extends ActionSupport {

   private String name;

   @SuppressWarnings("unchecked")

   //观看控制台

   //如果token生效则不会在控制台输出name的值,而会输出如下警告: 2011-3-14 20:45:32com.opensymphony.xwork2.util.logging.commons.CommonsLogger

   //warn 警告: Form token EDZ4S96RNDN5VD8B1CQTK6FTHIJUPC66 does not match thesession 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//DTDHTML 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:textfieldname="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//DTDHTML 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//DTDHTML 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 mypage">

   <!--

   <link rel="stylesheet" type="text/css"href="styles.css">

   -->



 </head>

 

 <body>

     您已经提交了表单,请不要重复提交。

 </body>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值