When the SpringMVC table is submitted repeatedly, the springmvc table is submitted.
Recently, the company was launched and some comrades attacked me. I did not know the general idea when I submitted the table repeatedly, but it was too troublesome, although I used the filter and interceptor in the interview later, it was a little troublesome.
After searching on the internet, I found a good solution: annotation and interceptor are completed.
First, create the annotation token:
@ Target (ElementType. METHOD)
@ Retention (RetentionPolicy. RUNTIME) sets the scope of the annotation.
Public @ interface Token {
Boolean save () default false;
Boolean remove () default false;
}
Create a token Interceptor:
Public class TokenInterceptor 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 ();
Token annotation = method. getAnnotation (Token. class );
If (annotation! = Null ){
Boolean needSaveSession = annotation. save ();
If (needSaveSession ){
Request. getSession (false). setAttribute ("token", UUID. randomUUID (). toString ());
}
Boolean needRemoveSession = annotation. remove ();
If (needRemoveSession ){
If (isRepeatSubmit (request )){
Return false;
}
Request. getSession (false). removeAttribute ("token ");
}
}
Return true;
} Else {
Return super. preHandle (request, response, handler );
}
}
Private boolean isRepeatSubmit (HttpServletRequest request ){
String serverToken = (String) request. getSession (false). 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;
}
}
Configure the interceptor in the configuration file: note that when configuring the annotation processor, You need to configure the request ing method processor and the request ing processing adapter. If you use
<Bean
Class = "org. springframework. web. servlet. mvc. annotation. DefaultAnnotationHandlerMapping"/> the method processor cannot be found by default.
<Bean class = "org. springframework. web. servlet. mvc. method. annotation. RequestMappingHandlerMapping">
<Property name = "interceptors">
<List>
<Bean class = "com. rbao. east. interceptor. TokenInterceptor"/>
</List>
</Property>
</Bean>
<Bean class = "org. springframework. web. servlet. mvc. method. annotation. RequestMappingHandlerAdapter"/>
Then add @ token (save = true) to the method to generate the Token)
Add @ Token (remove = true) to the method for verification)
Add a hidden field <input type = "hidden" name = "token" value = "$ {token}"/>
This article comes from:
Http://blog.icoolxue.com/submitted-by-spring-mvc-to-prevent-data-duplication/
And
Http://chenzhou123520.iteye.com/blog/1702563
Integration
Spring is too 'passionate. There are good articles to recommend
If you have any good solutions, I hope you can recommend them to learn from each other. Thank you ~~~