JSP login page, display error message, refresh the page after the error still exists solution

Source: Internet
Author: User

In the login page, usually using the form form synchronous submission of the method to submit, that is, in the form form to write action, if the login failed, JSP through the JSTL expression to get error messages displayed on the page, but there is a problem is, even if you refresh the page, Error hints always exist, this is why;

Problem analysis, the form of the form is submitted by synchronous check, then the address of your browser address bar will become, you submit the form's action address, (of course, if you use Ajax asynchronous authentication, there is no problem); So when the data to the background check does not pass, the background will return to your login page

Also carry some information, so that the front desk can be used to get information through the expression, then you may go to refresh the browser, but the error message is still there, this is why? Because your address bar is also the address of your submission form, you actually submit the form to the background when you refresh it, so it appears

The page also shows the error message (solution: You can use Ajax to submit the form, so that the browser address bar does not change, you do not submit the form when you refresh again, but this is not the main solution of this article) so think about why not redirect it? Redirection can be used, but one problem is how does redirection carry data?

Good:

Spring 3.1 provides a very useful class: Redirectattributes. Using this class, we can upload parameters to the page with redirection, without having to spell the URL ourselves.

Replace the model in the above method parameter with Redirectattributes, and the parameters are automatically followed by the URL.

However, this page can not be obtained with El, but also to deal with, so, we also have a way, do not spell the URL, using El to get parameters, like normal forwarding.

or use redirectattributes, but this time without AddAttribute method, spring has prepared a new method for us, Addflashattribute ().

The principle of this method is to put it in session, and the session will remove the object immediately after jumping to the page. So when you refresh, this value will be lost.

 PackageCom.demo.controller; ImportJava.util.Map; ImportOrg.springframework.stereotype.Controller; ImportOrg.springframework.ui.Model; ImportOrg.springframework.web.bind.annotation.ModelAttribute; Importorg.springframework.web.bind.annotation.RequestMapping; ImportOrg.springframework.web.bind.annotation.RequestParam; Importorg.springframework.web.servlet.mvc.support.RedirectAttributes; @Controller @RequestMapping ("/USER")   Public classDemocontroller {@RequestMapping ("/login")  //Public String Login (@RequestParam map<string, string> user, model model) {     PublicString Login (@RequestParam map<string, string>user, redirectattributes model) {System.out.println ("User submitted a form");          String username; if(User.get ("name"). IsEmpty ()) {username= "Tom"; } Else{username= User.get ("name"); } model.addflashattribute ("MSG", username); //return "Home";//this way jumps, page refresh will repeat the form        return "Redirect:/user/tohome"; } @RequestMapping ( "/tohome")       PublicString Home (@ModelAttribute ("MSG") (String msg, model model) {SYSTEM.OUT.PRINTLN ("Get redirected to get the parameter msg:" +msg); Model.addattribute ("MSG", MSG); return"Home"; }  }  

Note that the red part, when the checksum fails, redirects to a handler, the handler gets the redirected parameters, saves and forwards the page, and the session is removed after jumping to the page because the parameters in the redirect are placed in the session. So you refresh this value will be lost, so the first time the handler return, will carry the error message, the browser address is the handler requestmapping, when the browser refresh, The handler has been unable to get the redirect parameter (null), so it can only return the page; This solves the problem perfectly.

The equivalent of validating the form, more than one step, but also reminds us, adding and deleting changes must pay attention to the use of the way, that is, the use of asynchronous operations, or synchronous operation to be redirected!

JSP login page, display error message, refresh the page after the error still exists solution

Related Article

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.