springmvc 防止重复提交
来源:互联网 发布:c语言图形输出 编辑:程序博客网 时间:2024/05/22 20:29
http://blog.csdn.net/u013378306/article/details/52944780
第二种方法(判断请求url和数据是否和上一次相同)
推荐,非常简单,页面不需要任何传入,只需要在验证的controller方法上写上自定义注解即可
写好自定义注解
- package com.thinkgem.jeesite.common.repeat_form_validator;
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
-
-
-
-
-
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface SameUrlData {
-
-
- }
写好拦截器
- package com.thinkgem.jeesite.common.repeat_form_validator;
-
- import java.lang.reflect.Method;
- import java.util.HashMap;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.method.HandlerMethod;
- import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
-
- import com.thinkgem.jeesite.common.mapper.JsonMapper;
-
-
-
-
-
-
-
- public class SameUrlDataInterceptor extends HandlerInterceptorAdapter{
-
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- if (handler instanceof HandlerMethod) {
- HandlerMethod handlerMethod = (HandlerMethod) handler;
- Method method = handlerMethod.getMethod();
- SameUrlData annotation = method.getAnnotation(SameUrlData.class);
- if (annotation != null) {
- if(repeatDataValidator(request))
- return false;
- else
- return true;
- }
- return true;
- } else {
- return super.preHandle(request, response, handler);
- }
- }
-
-
-
-
-
- public boolean repeatDataValidator(HttpServletRequest httpServletRequest)
- {
- String params=JsonMapper.toJsonString(httpServletRequest.getParameterMap());
- String url=httpServletRequest.getRequestURI();
- Map<String,String> map=new HashMap<String,String>();
- map.put(url, params);
- String nowUrlParams=map.toString();
-
- Object preUrlParams=httpServletRequest.getSession().getAttribute("repeatData");
- if(preUrlParams==null)
- {
- httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
- return false;
- }
- else
- {
- if(preUrlParams.toString().equals(nowUrlParams))
- {
-
- return true;
- }
- else
- {
- httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
- return false;
- }
-
- }
- }
-
- }
配置spring mvc
- <mvc:interceptor>
- <mvc:mapping path="/**"/>
- <bean class="com.thinkgem.jeesite.common.repeat_form_validator.SameUrlDataInterceptor"/>
- </mvc:interceptor>
0 0