在struts2中,我们可以利用struts2自带的token拦截器轻松实现防止表单重复提交功能!

1. 在相应的action配置中增加:

   <interceptor-ref name="token"></interceptor-ref>

  <result name="invalid.token">/error.jsp</result>

2. 增加error.jsp文件,代码如下:

   <h1>禁止重复提交</h1>

3. 在所提交的表单上增加:<s:token></s:token>标记。

  <form action="firstAction">

    <input name="uname" value="zhangsan"><br>
    <s:token></s:token>
    <input type="submit" value="提交">
  </form>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

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="crm" namespace="/" extends="struts-default" >
    <interceptors>
      <interceptor name="myInter" class="com.huawei.interceptor.MyInterceptor" />
      <interceptor-stack name="myStack">
        <interceptor-ref name="defaultStack" />
        <!-- <interceptor-ref name="myInter" /> -->
        <interceptor-ref name="token" />
      </interceptor-stack>
    </interceptors>
    <default-interceptor-ref name="myStack" />
    <!-- 全局result -->
    <global-results>
      <result name="success">/ok.jsp</result>
    </global-results>
  </package>
  <package name="default" namespace="/" extends="crm">
    <action name="firstAction" class="com.huawei.s2.action.FirstAction" >
      <result name="invalid.token" >/error.jsp</result>
    </action>
  </package>
</struts>

Action:

package com.huawei.s2.action;
public class FirstAction {
  private String uname;
  public String execute(){
    System.out.println(uname+"========FirstAction=======");
    return "success";
  }
  public String getUname() {
    return uname;
  }
  public void setUname(String uname) {
    this.uname = uname;
  }
}

interceptor:

package com.huawei.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
* 要么实现 Interceptor 接口 要么 继承类
* @author Administrator
*
*/
public class MyInterceptor extends AbstractInterceptor{

  @Override
  public String intercept(ActionInvocation invocation) throws Exception {
    System.out.println(invocation.getAction());
    System.out.println(invocation.getInvocationContext());
    System.out.println(invocation.getProxy().getActionName());
    System.out.println(invocation.getProxy().getMethod());
    System.out.println(invocation.getInvocationContext().getParameters());
    System.out.println("action执行前");
    invocation.invoke();
    System.out.println("action执行后");
    return null;
  }

}

jsp:

1.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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%>">
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <title>This is my JSP 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">
</head>
<body>
  <form action="firstAction">
    <input name="uname" value="zhangsan"><br>
    <s:token></s:token>
    <input type="submit" value="提交">
  </form>
</body>
</html>

ok.jsp:

<%@ page language="java" import="java.util.*" 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">
<html>
  <head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>This is my JSP 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">
  </head>
  <body>
    <h1>提交成功</h1>
  </body>
</html>

error.jsp:

<%@ page language="java" import="java.util.*" 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">
<html>
  <head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>This is my JSP 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">
  </head>
  <body>
    <h1>禁止重复提交</h1>
  </body>
</html>

转载于:https://www.cnblogs.com/hwgok/p/5540452.html

struts2 自带的 token防止表单重复提交拦截器相关推荐

  1. php token 表单重复提交,PHP生成token防止表单重复提交2个例子

    防止表单重复提交的解决方案非常的简单,我们下面两个例子都是生成一个随机的token验证用户是不是由我们站内提交并且进行重复验证即可实现了. 在网上搜索了一下有很多站长都这样说的 1.提交按钮置disa ...

  2. python表单防重复提交_关于PHP使用token防止表单重复提交的方法

    这篇文章主要介绍了PHP使用token防止表单重复提交的方法,通过生成一个加密后的随机数存入session的token变量,同时将该值放入表单隐藏提交,达到防止表单重复提交的功能,需要的朋友可以参考下 ...

  3. Struts2框架学习之七:避免表单重复提交

    前言 防止表单重复提交在web开发中是一个经常遇到的问题,一般来避免重复提交有两种方式:客户端JavaScript代码实现和服务端代码实现.这里主要介绍服务端的实现方式.在服务端实现表单重复提交的基本 ...

  4. SpringMVC中实现的token,防表单重复提交

    一:首先创建一个token处理类  ,这里的类名叫 TokenHandler private static Logger logger = Logger.getLogger(TokenHandler. ...

  5. token防止表单重复提交

    在平时开发中,如果网速比较慢的情况下,用户提交表单后,发现服务器半天都没有响应,那么用户可能会以为是自己没有提交表单,就会再点击提交按钮重复提交表单,我们在开发中必须防止表单重复提交. 一.表单重复提 ...

  6. 【精品】防止表单重复提交 方法汇总

    背景 表单重复提交会造成数据重复,增加服务器负载,严重甚至会造成服务器宕机等情况,有效防止表单重复提交有一定的必要性. 常见的防止表单重复提交解决方案有以下几种: 一.通过一个标识来控制表单提交之后, ...

  7. 5位随机数重复的概率 php_php防止表单重复提交的方法

    Token,就是令牌,最大的特点就是随机性,不可预测. Token一般用在两个地方--防止表单重复提交.anti csrf攻击(跨站点请求伪造). 两者在原理上都是通过session token来实现 ...

  8. JavaWeb -- Struts1 使用示例: 表单校验 防表单重复提交 表单数据封装到实体

    1. struts 工作流程图 超链接 2. 入门案例 struts入门案例:1.写一个注册页面,把请求交给 struts处理<form action="${pageContext.r ...

  9. 使用Struts2防止表单重复提交

    用户重复提交表单在某些场合将会造成非常严重的后果.例如,在使用信用卡进行在线支付的时候,如果服务器的响应速度太慢,用户有可能会多次点击提交按钮,而这可能导致那张信用卡上的金额被消费了多次.因此,重复提 ...

最新文章

  1. spring整合mybatis(入门级简单教程2)
  2. SpringBoot文件上传异常之temporary upload location not valid
  3. VA01保存后都更新了什么表
  4. CloudStack 4.2 与CloudStack 4.1二级存储API发生变化
  5. 逆向工程核心原理学习笔记(六):实战开辟新内存区域写入缓冲区跳转修改字符串
  6. arraylist线程安全吗_Java中的集合和线程安全
  7. mysql char 二进制_SQL:char 和 varchar、binary 和 varbinary、二进制字符串、严格模式、汉字编码方式...
  8. 最全数据指标体系集合!覆盖9个行业4个业务场景,全是干货
  9. C++实现包含空格、标点、字符、数字的字符串的逆序输出,并且还可以实现一句语言中每个单词的倒序输出
  10. FlightGear--64位编译指南
  11. 告别码公式的痛苦,公式OCR终于来了!
  12. IIS无法启动计算机上的服务W3SVC如何修复、万维网发布服务(w3svc)已停止解决办法
  13. 生活技巧:过日子学着点
  14. AttributeError: partially initialized module ‘re‘ has no attribute ‘findall‘
  15. signature=ac75cb7977a45c0f7d8a73dca59a4c27,合肥2016年8月24日至2016年9月5日交通违章查询...
  16. 1442. 单词处理器 Java题解
  17. 小程序: webview与小程序之间的跳转
  18. 2022届互联网校招薪资开奖,拼多多最高年薪 75 万!
  19. 关于qq邮箱 该文件已达到200次的下载限制,您已不能下载该文件 的问题处理
  20. Kubernetes学习笔记(二):Pod控制器详解:资源元信息、ReplicaSet、Deployment、DaemonSet、Job、CronJob

热门文章

  1. java 自动生成mybatis文件_如何自动生成Mybatis的Mapper文件详解
  2. jssdk 获取微信收货地址_微信收货地址共享开发接口讲解
  3. pca算法python实现_三种方法实现PCA算法(Python)
  4. 小程序组件报错Uncaught TypeError: Cannot read property 'name' of undefined
  5. php opcache 安装,php opcache安装和配置
  6. rebots css,我的robots.txt中涉及到.htaccss文件的奇怪https/http错误
  7. mysql8 高可用_mysql系列之8.mysql高可用 (mha4mysql)
  8. 使用SpringBoot+JPA报错Incorrect syntax near 'hibernate_sequence'
  9. php-fpm 进程数和 cpu,关于PHP的 PHP-FPM进程CPU 100%的分析和解决
  10. apt编译java_自动生成代码工具--APT