struts2 自带的 token防止表单重复提交拦截器
在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>
struts2 自带的 token防止表单重复提交拦截器的更多相关文章
- AOP+Token防止表单重复提交
表单重复提交: 由于用户误操作,多次点击表单提交按钮 由于网速等原因造成页面卡顿,用户重复刷新提交页面 避免表单重复提交的方式: 1.页面上的按钮做防重复点击操作 2.在数据库中可以做唯一约束 3.利 ...
- PHP简单利用token防止表单重复提交
<?php /* * PHP简单利用token防止表单重复提交 * 此处理方法纯粹是为了给初学者参考 */ session_start(); function set_token() { $_S ...
- PHP生成token防止表单重复提交
.提交按钮置disabled 当用户提交后,立即把按钮置为不可用状态.这种用js来实现. 提交前代码如下: $() { $exec="insert into student (user_ ...
- PHP简单利用token防止表单重复提交(转)
<?php/* * PHP简单利用token防止表单重复提交 */function set_token() { $_SESSION['token'] = md5(microtime(true)) ...
- php通过token验证表单重复提交
PHP防止重复提交表单 2016-11-08 轻松学PHP 我们提交表单的时候,不能忽视的一个限制是防止用户重复提交表单,因为有可能用户连续点击了提交按钮或者是攻击者恶意提交数据,那么我们在提交数据后 ...
- PHP使用token防止表单重复提交的方法
本文实例讲述了PHP使用token防止表单重复提交的方法.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...
- Struts2笔记——利用token防止表单重复提交
在一些项目中经常会让用户提交表单,当用户点击按钮提交后,如果再次浏览器刷新,这就会造成表单重复提交,若是提交的内容上传至服务器并请求数据库保存,重复提交的表单可能会导致错误,然后跳转到错误界面,这是一 ...
- Struts2第十三篇【防止表单重复提交】
回顾防止表单重复提交 当我们学习Session的时候已经通过Session来编写了一个防止表单重复提交的小程序了,我们来回顾一下我们当时是怎么做的: 在Servlet上生成独一无二的token,保存在 ...
- token防止表单重复提交
出现表单重复提交的三种情况: 一.服务器响应缓慢,用户多次点击提交按钮. 二.提交成功后刷新页面. 三.提交成功后返回表单页面再次点击提交. package com.jalja.token; impo ...
随机推荐
- Servlet 实现网页计数器
创建CounterSerlet, 使用getServletContext,ServletContext 从request.getSession().getServletContext();获得 @We ...
- arcgis desktop can not connect to previous version of license manager
解决方法: license manager以 管理员身份运行.
- datacolumn 表达式 除数为0
dt.columns.add("avgp",typeof(decimal),"IIF(qty=0,0,price/qty)")
- Android之WebViewClient与WebChromeClient的区别
Android之WebViewClient与WebChromeClient的区别 2012-05-05 0个评论 收藏 我要投稿 ANDROID应用开发的时候可能会用到WE ...
- bzoj1941 Hide and Seek
Description 小猪iPig在PKU刚上完了无聊的猪性代数课,天资聪慧的iPig被这门对他来说无比简单的课弄得非常寂寞,为了消除寂寞感,他决定和他的好朋友giPi(鸡皮)玩一个更加寂寞的游戏- ...
- jmeter监控服务器的方法
先下载Jmeter资源监控插件,我的百度云jmeter视频里面有说. 地址如下: JMeterPlugins-Standard-1.3.1.zip 下载 https://jmeter-plugins ...
- [UE4]多播代理实例
.h DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FLoginErrorEvent, FString, ErrorMessage); UPROPERTY(B ...
- CA双向认证的时候,如果一开始下载的证书就有问题的,怎么保证以后的交易没有问题?
研究HTTPS协议的时候,发现网站的CA认证,比如建行,比如支付宝,需要首先下载数字证书, 当然有些其他的双向认证,比如之前做过的港航和JP MORGAN进行交互的时候,证书是私下发送的,不需要去公网 ...
- springMVC 是单例还是的多例的?
曾经面试的时候有面试官问我spring的controller是单例还是多例,结果我傻逼的回答当然是多例,要不然controller类中的非静态变量如何保证是线程安全的,这样想起似乎是对的,但是不知道( ...
- 使用eclipse在linux下开发C/C++
一直在Linux下开发,苦于没有IDE,一般都是自己编写Makefile,然后在windows下用文本编辑器ftp打开文件编辑,然后在linux下完成编译.调试代码也只能是命令行用gdb进行调试,相当 ...