SpringMVC防止表单重复提交

判断请求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;

/**

  • 一个用户 相同url 同时提交 相同数据 验证
  • @author Administrator

*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SameUrlData {

}

说明:
@Target是Java的元注解(指修饰注解的注解)之一。用于设定注解使用范围。
ElementType指定注解可使用范围的枚举集合
METHOD 注解使用范围:可用于方法上

@Retention是java当中的一个元注解,该元注解通常都是用于对软件的测试
RetentionPolicy.RUNTIME就说明了,@Task注解在程序运行时是可见的

================================================

写好拦截器
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;

/**

  • 一个用户 相同url 同时提交 相同数据 验证
  • 主要通过 session中保存到的url 和 请求参数。如果和上次相同,则是重复提交表单
  • @author Administrator

*/
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);
        }
    }
/**
 * 验证同一个url数据是否相同提交  ,相同返回true
 * @param httpServletRequest
 * @return
 */
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){ //如果上一个数据为null,表示还没有访问页面
		httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
		return false;
	}else{ //否则,已经访问过页面
	
		if(preUrlParams.toString().equals(nowUrlParams)){ //如果上次url+数据和本次url+数据相同,则表示城府添加数据
			return true;
		}else{ //如果上次 url+数据 和本次url加数据不同,则不是重复提交
			httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
			return false;
		}			
	}
}

}
配置spring mvc
mvc:interceptor
<mvc:mapping path="/**"/>

</mvc:interceptor>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值