The Spring MVC 3.1 version adds a very useful feature, the Flash attribute, which solves a problem that has long been missing a solution, a post/redirect/get pattern problem.
The normal MVC Web application will post data to the server each time it commits. A normal controller (annotated @Controller tag) obtains data from the request and processes it (saves or updates the database). Once the operation is successful, the user is taken to a page where the operation was successful (forward). Traditionally, such post/forward/get patterns sometimes lead to multiple submission problems. For example, when the user presses F5 to refresh the page, the same data is submitted again.
To solve this problem, the post/redirect/get pattern is used on the MVC application. Once the user form is submitted successfully, we redirect (Redirect) The request to another success page. This allows the browser to create new get requests and load new pages. This allows the user to press F5, which is a direct GET request instead of submitting the form again.
650) this.width=650; "src=" Http://static.oschina.net/uploads/img/201404/09152515_CQRM.png "alt=" Post/redirect/get "height=" 534 "width=" 653 "style=" Border:none;width:auto;height:auto; "/>
Image Credit:wikipedia
650) this.width=650; "class=" image "src=" https://static.oschina.net/uploads/user/438/876523_100.png?t= 1395915512000 "alt=" Idiot_s_sky "title=" Idiot_s_sky "style=" Border:none;color:rgb (204,204,204); width:70px;height : 70px; "/>
Idiot_s_sky
Translated over 3 years ago
5 Person Top
Top Well translated!
Although this approach looks perfect and solves the problem of multiple submissions of forms, it introduces a challenge to get request parameters and properties. Typically, when we generate an HTTP redirect request, the data that is stored in the request is lost, making it impossible for the next get request to access some of the useful information in the request.
The arrival of Flash attributes is to deal with this situation. Flash attributes provides a way for a request store intent to use properties for another request. Flash attributes is temporarily stored (usually in session) before the redirection of the request takes effect, and is immediately removed after redirection.
650) this.width=650; "src=" Http://static.oschina.net/uploads/img/201404/09152522_g8UD.png "alt=" Spring-mvc-flash-map-manager "height=" 191 "width=" 329 "style=" Border:none;width:auto;height:auto; "/>
To do this, the Flash feature uses two collections. Flashmap is used to manage flash attributes and Flashmapmanager is used to store, acquire, and manage flashmap entities.
For each request an "input" Flash map will be created to store the Flash attribute from any previous request and an "output" Flash map will be created to store any memory we store in this The request parameters in the request, after.
650) this.width=650; "class=" image "src=" https://static.oschina.net/uploads/user/116/233834_100.jpg?t= 1378539263000 "alt=" Leoxu "title=" Leoxu "style=" Border:none;color:rgb (204,204,204); width:70px;height:70px; "/>
Leoxu
Translated over 3 years ago
3 Person Top
Top Well translated!
Use
To use Flash attribute in your Spring MVC application, you should use version 3.1 or more . and to add Mvc:annotation-driven to the Spring-servlet.xml file.
<mvc:annotation-driven/>
Once these are complete, the Flash attribute is automatically set to "on" for use. Simply add Redirectattributes redirectattributes to your Spring controller method.
Import org.springframework.web.servlet.mvc.support.redirectattributes;//... @RequestMapping (value= "Addcustomer", method=requestmethod.post) public String Addcustomer (@ModelAttribute ("customer") customer Customer,final Redirectattributes redirectattributes) {//...redirectattributes.addflashattribute ("message", "Successfully added.."); /...return "Redirect:some_other_request_name";}
The Addflashattribute method automatically adds the given parameter to the output Flash map and passes it to the subsequent request.
Let's take a look at a complete example of using Flash attribute to complete post/redirect/get and pass some information.
Flash Attribute Instances
The following app displays a form to the user. When the user fills out the data and submits the form, the page is redirected to another page that displays success information. In this new redirected page, the information that the user has just entered is displayed.
650) this.width=650; "class=" image "src=" https://static.oschina.net/uploads/user/723/1446426_100.jpg?t= 1391158447000 "alt=" dai Cang Potato "title=" dai Cang Potato "style=" Border:none;color:rgb (204,204,204); width:70px;height:70px;/>
Dai Cang Potato
Translated over 3 years ago
4 Person Top
Top Well translated!
1th Step: Required JAR and project structure
If you use Maven for dependency management, use the following dependencies to add support for Spring 3.1 MVC.
<dependencies><!--Spring 3.1 MVC--><dependency><groupid>org.springframework</groupid ><artifactid>spring-webmvc</artifactid><version>3.1.2.release</version></ dependency><!--JSTL for C:tag--><dependency><groupid>jstl</groupid><artifactid> Jstl</artifactid><version>1.2</version></dependency></dependencies>
Alternatively, you can download the following JAR files and place them under the /web-inf/lib folder.
650) this.width=650; "src=" Http://static.oschina.net/uploads/img/201404/09152522_GJOb.png "alt=" Spring-mvc-flash-attribute-jar-files "height=" 211 "width=" 266 "style=" Border:none;width:auto;height:auto; "/>
2nd Step: Spring Configuration
To add Spring support for a Web project, you need to add a dispatcherservlet to the XML.
Xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns=" Http://java.sun.com/xml/ns/javaee " xmlns:web=" Http://java.sun.com/xml/ns/javaee /web-app_2_5.xsd "xsi:schemalocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ Web-app_2_5.xsd "id=" webapp_id " version=" 2.5 "><display-name>Spring MVC Flash attribute example</display-name><servlet><servlet-name>spring</servlet-name>< Servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class><load-on-startup >1</load-on-startup></servlet><servlet-mapping> <servlet-name >default</servlet-name> <url-pattern>/index.html</url-pattern>< /servlet-mapping><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>* . html</url-pattern>≪/servlet-mapping></web-app>
Spring-servlet then uses Mvc:annotation-driven to support MVC, and it scans the Context:component-scan tags in the project.
Spring-servlet.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http// Www.springframework.org/schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:context=" http ://www.springframework.org/schema/context "xmlns:mvc=" Http://www.springframework.org/schema/mvc "xsi: Schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/ schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http:// Www.springframework.org/schema/mvc/spring-mvc.xsd "><bean id=" Jspviewresolver "class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" Viewclass "value= "Org.springframework.web.servlet.view.JstlView" /><property name= "prefix" value= "/web-inf /jsp/" /><property name=" suffix " value=". JSP "&nbsP;/> </bean><context:component-scan base-package= "Net.viralpatel.controller" /> <mvc:annotation-driven /> </beans>
3rd Step: Spring controller–redirectattributes
The Controller's code uses the Customer.java object as a bean to hold the customer information.
Customer.java
Package Net.viralpatel.spring;public class Customer {private string firstname;private string lastname;private int age; Private String Email;//getter, setter methods}
The Customercontroller class has 3 methods. The ShowForm method corresponds to the URL /form , which is used to display the ADD New Customer form. The Addcustomer method corresponds to the URL /addcustomer , which is used to process the POST request.
Customercontroller.java
package net.viralpatel.controller;import net.viralpatel.spring.customer;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.servlet.mvc.support.RedirectAttributes, @Controllerpublic class customercontroller {@RequestMapping (value= "ShowForm", method=requestmethod.get) public string showform (@ModelAttribute ("Customer") customer customer) {return "Add_customer";} @RequestMapping (value= "Addcustomer", method=requestmethod.post) Public string addcustomer (@ Modelattribute ("Customer") customer customer,final redirectattributes redirectattributes) {redirectattributes.addflashattribute ("Customer", customer); Redirectattributes.addflashattribute (" Message "," Added&nbsP;successfully. "); return "redirect:showcustomer.html";} @RequestMapping (value= "Showcustomer", method=requestmethod.get) Public string showcustomer (@ Modelattribute ("Customer") customer customer) {system.out.println ("Cust:" + Customer.getfirstname ());return "Show_customer";}}
Note how we use the Redirectattributes parameter to add flash attribute in the Addcustomer method. Also, we are using the Addflashattribute method to set new parameters for Flash attribute.
650) this.width=650; "class=" image "src=" https://static.oschina.net/uploads/user/723/1446426_100.jpg?t= 1391158447000 "alt=" dai Cang Potato "title=" dai Cang Potato "style=" Border:none;color:rgb (204,204,204); width:70px;height:70px;/>
Dai Cang Potato
Translated over 3 years ago
4 Person Top
Top Well translated!
4th Step: View Layer
Add Customer. The JSP file displays an Add new customer form.
add_customer.jsp
<% @taglib uri= "http://www.springframework.org/tags/form" prefix= "form"%>
Show_customer.jsp simply displays the customer's first and last name, as well as the success information set with the Flash attributes.
show_customer.jsp
<% @taglib uri= "http://www.springframework.org/tags/form" prefix= "form"%>
Demo: execute this web project.
url:http://localhost:8080/springmvc_flash_attribute_maven_example/ form.html
650) this.width=650; "Src=" Http://static.oschina.net/uploads/img/201404/09152523_MdYa.png " alt= "Spring-mvc-flash-attribute-demo-form" height= "303" width= "569" style= "border:none;width:auto;height:auto;"/
650) this.width=650; "src=" Http://static.oschina.net/uploads/img/201404/09152523_2zNi.png "alt=" Spring-mvc-flash-attribute-demo-success "height=" 212 "width=" 630 "style=" border:none;width:auto;height:auto; "/ >
All code sources for this site see: minglisoft.cn/technology
Spring MVC Flash Attribute Tutorial and usage examples