SPRINGMVC Redirect Request

Source: Internet
Author: User

Implementation of SPRINGMVC redirection parameters (from netizens)

In a controller in spring to the parameters to the page, as long as the configuration of the view parser, the parameters are added to the model, the page can be taken with an El expression. However, this is using the forward method, the browser's address bar is constant, if the browser F5 refresh, it will cause the form to repeat the case. So, we can use redirection to change the address bar of the browser to prevent the form from repeating the submission because of the refresh.

JSP file:

<%@PageLanguage="Java"Contenttype="Text/html; Charset=utf-8 "pageencoding="UTF-8"%><%@Taglibprefix=CUri="Http://java.sun.com/jsp/jstl/core"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" ><Html><Head><Metahttp-equiv="Content-type"Content="Text/html; Charset=utf-8 "><Title>login</Title></Head><Body><FormId="Form1"action="/demo/user/login"Method="POST" > Account:<Inputtype= "text" name=  "name"/></br> Password: <input type=" password " Span class= "hljs-attr" >name= "password"/></br><input type= "submit" value= "submit"/> </form></ body></HTML>    

Controller

Package Com.demo.controller;Import Java.util.Map;Import Org.springframework.stereotype.Controller;Import Org.springframework.ui.Model;Import org.springframework.web.bind.annotation.RequestMapping;Import Org.springframework.web.bind.annotation.RequestParam;/** *@author LPJ *@date July 10, 2016 */@Controller@RequestMapping ("/user")Publicclass democontroller { @RequestMapping ("/login ")  public String login (@RequestParam map<string, string> user, model model) {SYSTEM.OUT.PRINTLN ( "Users submit a form"); String username; if (User.get ( "name"). IsEmpty ()) {username = " Tom ";} else {username = user.get ( "name");} Model.addattribute ( "MSG", username);  Return "Home";//jump in this way, page refresh will repeat the form return  "redirect:/ Home.jsp ";}}                

Because the redirect is equivalent to 2 requests, the parameter cannot be passed in the model. In the example above, the page gets no msg parameter. To get the parameters, you can manually spell the URL and take the parameters back. 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. Change the controller code to the following:

Package Com.demo.controller;Import Java.util.Map;Import Org.springframework.stereotype.Controller;Import Org.springframework.ui.Model;Import Org.springframework.web.bind.annotation.ModelAttribute;Import org.springframework.web.bind.annotation.RequestMapping;Import Org.springframework.web.bind.annotation.RequestParam;Import org.springframework.web.servlet.mvc.support.RedirectAttributes;/** *@author LPJ *@date July 10, 2016 */@Controller@RequestMapping ("/user")PublicClassDemocontroller {@RequestMapping ("/login")Public String Login (@RequestParam map<string, string> user, model model) {Public StringLogin(@RequestParam map<string, string> user, redirectattributes model) {System.out.println ("User submits a form"); String username;if (User.get ( "Tom";} else {username = user.get ( "name");} Model.addflashattribute ( "MSG", username);  Return "Home";//jump in this way, page refresh will repeat the form return  "redirect:/user/ Tohome ";}  @RequestMapping ( "/tohome") public String home (@ModelAttribute ( "msg") string msg, model model) {SYSTEM.OUT.PRINTLN ( "Get redirected parameter msg:" + msg); Model.addattribute ( "msg", msg);  return            

Here we use @modelattribute annotations to get the data that was added before Addflashattribute, and then we can use it normally.

SPRINGMVC Redirect Request

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.