When the SpringMVC table is submitted repeatedly, the springmvc table is submitted.

Source: Internet
Author: User

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 ~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.