拦截器springmvc防止表单重复提交【3】3秒后自动跳回首页【重点明白如何跳转到各自需要的页面没有实现 但是有思路】
【1】定义异常类
【重点】:异常类有个多参数的构造函数public CmsException(String s, String... args),可以用来接受多个参数:如(“异常信息”,“几秒跳转”,“跳转url”)
package com.jspxcms.core.support; /**
* CmsException CMS异常
*
* @author liufang
*
*/
public class CmsException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String[] args; public CmsException() {
super();
} public CmsException(String s) {
super(s);
} public CmsException(String s, String... args) {
super(s);
this.args = args;
} public CmsException(String message, Throwable cause) {
super(message, cause);
} public CmsException(Throwable cause) {
super(cause);
} public String[] getArgs() {
return args;
} public void setArgs(String[] args) {
this.args = args;
} }
【2】web.xml中定义异常提示页面
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>jspxcms</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/conf/context-*.xml
/WEB-INF/conf/custom-component.xml
/WEB-INF/conf/core/core-context-dao.xml
/WEB-INF/conf/core/core-context-service.xml
/WEB-INF/conf/core/core-context-directive.xml
/WEB-INF/conf/ext/ext-context-dao.xml
/WEB-INF/conf/ext/ext-context-service.xml
/WEB-INF/conf/ext/ext-context-directive.xml
/WEB-INF/conf/plugin/**/context-*.xml
</param-value>
</context-param>
<!--
<filter>
<filter-name>urlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>statusEnabled</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>statusPath</param-name>
<param-value>/status.jspx</param-value>
</init-param>
</filter>
-->
<filter>
<filter-name>timerFilter</filter-name>
<filter-class>com.jspxcms.common.web.TimerFilter</filter-class>
</filter>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>openEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>jspDispatcherFilter</filter-name>
<filter-class>com.jspxcms.common.web.JspDispatcherFilter</filter-class>
<init-param>
<param-name>prefix</param-name>
<param-value>/jsp</param-value>
</init-param>
</filter> <!--
<filter-mapping>
<filter-name>urlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
-->
<filter-mapping>
<filter-name>timerFilter</filter-name>
<url-pattern>*.servlet</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>timerFilter</filter-name>
<url-pattern>/cmscp/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/cmscp/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>/cmscp/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/cmscp/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>timerFilter</filter-name>
<url-pattern>*.jspx</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.jspx</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>*.jspx</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>*.jspx</url-pattern>
</filter-mapping> <filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>timerFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping> <filter-mapping>
<filter-name>timerFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>jspDispatcherFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>background</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/conf/servlet-back.xml
/WEB-INF/conf/core/core-controller-back.xml
/WEB-INF/conf/ext/ext-controller-back.xml
/WEB-INF/conf/plugin/**/controller-back.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>foreground</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/conf/servlet-fore.xml
/WEB-INF/conf/core/core-controller-fore.xml
/WEB-INF/conf/ext/ext-controller-fore.xml
/WEB-INF/conf/plugin/**/controller-fore.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>keepSessionServlet</servlet-name>
<servlet-class>com.jspxcms.common.web.KeepSessionServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>captchaServlet</servlet-name>
<servlet-class>com.jspxcms.common.captcha.CaptchaServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>background</servlet-name>
<url-pattern>/cmscp/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>foreground</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>keepSessionServlet</servlet-name>
<url-pattern>/keep_session.servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>captchaServlet</servlet-name>
<url-pattern>/captcha.servlet</url-pattern>
</servlet-mapping> <session-config>
<session-timeout>20</session-timeout>
</session-config> <error-page>
<error-code>400</error-code>
<location>/errors/400.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/errors/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/errors/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/errors/500.jsp</location>
</error-page>
<error-page>
<exception-type>javax.validation.ConstraintViolationException</exception-type>
<location>/errors/400.jsp</location>
</error-page>
<!-- shiro中没有权限是抛出异常 -->
<error-page>
<exception-type>org.apache.shiro.authz.AuthorizationException</exception-type>
<location>/errors/403.jsp</location>
</error-page>
<error-page>
<exception-type>org.springframework.dao.DataIntegrityViolationException</exception-type>
<location>/errors/data_integrity_violation_exception.jsp</location>
</error-page>
<error-page>
<exception-type>com.jspxcms.core.support.DeleteException</exception-type>
<location>/errors/delete_exception.jsp</location>
</error-page>
<error-page>
<exception-type>com.jspxcms.core.support.CmsException</exception-type>
<location>/errors/cms_exception.jsp</location>
</error-page> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.shtml</welcome-file>
<welcome-file>index.shtm</welcome-file>
<welcome-file>index.jspx</welcome-file>
</welcome-file-list>
</web-app>
【3】产生异常的地方 此处为拦截器
【重点】相应的new CmsException("请不要重复提交申请!",“3”,“要分别跳转的url地址”);
package com.jspxcms.ext.interceptor; import java.lang.reflect.Method;
import java.util.UUID; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.jspxcms.core.support.CmsException; public class TokenInterceptor extends HandlerInterceptorAdapter {
private static final Logger LOG = Logger.getLogger(TokenInterceptor.class); @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
Token annotation = method.getAnnotation(Token.class);
if (annotation != null) {
boolean needSaveSession = annotation.save();
if (needSaveSession) {
request.getSession(true).setAttribute("token", UUID.randomUUID().toString());
}
boolean needRemoveSession = annotation.remove();
if (needRemoveSession) {
if (isRepeatSubmit(request)) {
LOG.warn("please don't repeat submit,url:"+ request.getServletPath());
throw new CmsException("请不要重复提交申请!");
// return false;
}
request.getSession(true).removeAttribute("token");
}
}
return true;
} else {
return super.preHandle(request, response, handler);
}
} private boolean isRepeatSubmit(HttpServletRequest request) {
String serverToken = (String) request.getSession(true).getAttribute("token");
if (serverToken == null) {
return true;
}
String clinetToken = request.getParameter("token");
if (clinetToken == null) {
return true;
}
if (!serverToken.equals(clinetToken)) {
return true;
}
return false;
}
}
【4】jsp页面
【5】页面效果
【后面补充】:测试的时候发现 href用的是/index.jspx 如果不行 根据时间情况改写
拦截器springmvc防止表单重复提交【3】3秒后自动跳回首页【重点明白如何跳转到各自需要的页面没有实现 但是有思路】的更多相关文章
- 拦截器springmvc防止表单重复提交【1】
[参考博客:http://www.cnblogs.com/hdwpdx/archive/2016/03/29/5333943.html] springmvc 用拦截器+token防止重复提交 首先,防 ...
- 拦截器springmvc防止表单重复提交【3】自己实际项目
1:[定义注解] package com.jspxcms.ext.interceptor; import java.lang.annotation.ElementType; import java.l ...
- 拦截器springmvc防止表单重复提交【2】
[参考博客:http://my.oschina.net/mushui/blog/143397] 原理:在新建页面中Session保存token随机码,当保存时验证,通过后删除,当再次点击保存时由于服务 ...
- springmvc防止表单重复提交demo
原理:在去某个页面直接生成一个随机数(这里使用的是UUID)并放入session中,用户提交表单时将这个随机数传入服务端与session中的值进行比较,如果不不存在或不相等,则认为是重复提交:如果相等 ...
- SpringMVC防止表单重复提交
最近公司上线,有同志进行攻击,表当防重复提交也没有弄,交给我 ,本人以前也没弄过,知道大概的思路,但是那样实在是太麻烦了,虽然后面试过使用过滤器加拦截器实现,不过还是有点小麻烦. 后来在网上搜索后发现 ...
- spring boot 学习(七)小工具篇:表单重复提交
注解 + 拦截器:解决表单重复提交 前言 学习 Spring Boot 中,我想将我在项目中添加几个我在 SpringMVC 框架中常用的工具类(主要都是涉及到 Spring AOP 部分知识).比如 ...
- 12、Struts2表单重复提交
什么是表单重复提交 表单的重复提交: 若刷新表单页面, 再提交表单不算重复提交. 在不刷新表单页面的前提下: 多次点击提交按钮 已经提交成功, 按 "回退" 之后, 再点击 &qu ...
- java struts2入门学习--防止表单重复提交.OGNL语言学习
一.知识点回顾 防止表单重复提交核心思想: 客户端和服务器端和写一个token,比较两个token的值相同,则非重复提交;不同,则是重复提交. 1.getSession三种方式比较: request. ...
- Struts2 处理表单重复提交
* 在表单页面中增加一个隐藏域:<s:token></s:token>(需要在表单内) * 创建一个struts.xml的配置文件,具体配置如下: ...
随机推荐
- spring @RequestMapping注解技巧
@RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 下面我们看看,@Request ...
- 记录一下我的mac的环境变量的配置参数
#配置jdk环境export JAVA_7_HOME=/Library/java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Homeexport JAV ...
- 转载:Mongodb start
Mongodb 操作 Start MongoDB The MongoDB instance stores its data files in the /var/lib/mongo and its lo ...
- Android ---------高德卫星地图绘制多个点和点的点击事件自定义弹窗
最近开发中,遇到一个多个点绘制,并实现点击事件,出现自定义窗口显示相关信息等功能,所以写了这篇博客. 从后台请求数据,得到多个经纬度,然后绘制在地图上,并实现点击,出现相关信息(自定义弹框实现) 先来 ...
- [洛谷U62364]三次函数极值
U62364 三次函数极值 题面 给定一个三次函数\(f(x)=a_3x^3+a_2x^2+a_1x+a_0\) 求其极值. 格式 输入包括一行四个整数\(a_3,a_2,a_1,a_0\) 输出包括 ...
- IOS-数据缓存
一.关于同一个URL的多次请求 有时候,对同一个URL请求多次,返回的数据可能都是一样的,比如服务器上的某张图片,无论下载多少次,返回的数据都是一样的. 上面的情况会造成以下问题 (1)用户流量的浪费 ...
- Ansible 小手册系列 十七(特性模块)
异步操作和轮询 --- # Requires ansible 1.8+ - name: 'YUM - fire and forget task' yum: name=docker-io state=i ...
- nodejs利用express操作mysql增删改查
如果不知道怎么连接数据库的请看http://www.cnblogs.com/complete94/p/6714757.html 我当大家都知道怎么连接数据库了,那么 我们开始吧 var express ...
- Spring入门3.AOP编程
Spring入门3.AOP编程 代码下载: 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 前面学习的知识是Spring在Java项目中的IoC或DJ,这 ...
- 二十四、DBMS_SQL
1.概述 1) 在整个程序的设计过程中,对游标的操作切不可有省略的部分,一旦省略其中某一步骤,则会程序编译过程既告失败,如在程序结尾处未对改游标进行关闭操作,则在再次调用过程时会出现错误. 2) db ...