寫注解
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 同時提交 相同數(shù)據(jù) 驗證
* @author Administrator
*
*/
@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;
/**
* 一個用戶 相同url 同時提交 相同數(shù)據(jù) 驗證
* 主要通過 session中保存到的url 和 請求參數(shù)矾端。如果和上次相同,則是重復(fù)提交表單
* @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))//如果重復(fù)相同數(shù)據(jù)
? ? ? ? ? ? return false;
? ? ? ? ? ? else
? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? return true;
? ? ? ? } else {
? ? ? ? ? ? return super.preHandle(request, response, handler);
? ? ? ? }
? ? }
/**
* 驗證同一個url數(shù)據(jù)是否相同提交? ,相同返回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)//如果上一個數(shù)據(jù)為null,表示還沒有訪問頁面
{
httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
return false;
}
else//否則柄驻,已經(jīng)訪問過頁面
{
if(preUrlParams.toString().equals(nowUrlParams))//如果上次url+數(shù)據(jù)和本次url+數(shù)據(jù)相同鬓照,則表示城府添加數(shù)據(jù)
{
return true;
}
else//如果上次 url+數(shù)據(jù) 和本次url加數(shù)據(jù)不同凄吏,則不是重復(fù)提交
{
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>