Spring MVC Flash Attribute 的讲解与使用示例
admin
2023-07-30 18:21:48
0次

Spring MVC 3.1版本加了一个很有用的特性,Flash属性,它能解决一个长久以来缺少解决的问题,一个POST/Redirect/GET模式问题。

正常的MVC Web应用程序在每次提交都会POST数据到服务器。一个正常的Controller (被注解 @Controller标记)从请求获取数据和处理它 (保存或更新数据库)。一旦操作成功,用户就会被带到(forward)一个操作成功的页面。传统上来说,这样的POST/Forward/GET模式,有时候会导致多次提交问题. 例如用户按F5刷新页面,这时同样的数据会再提交一次。

为了解决这问题, POST/Redirect/GET 模式被用在MVC应用程序上. 一旦用户表单被提交成功, 我们重定向(Redirect)请求到另一个成功页面。这样能够令浏览器创建新的GET请求和加载新页面。这样用户按下F5,是直接GET请求而不是再提交一次表单。

Spring MVC Flash Attribute 的讲解与使用示例
Image credit: Wikipedia

Spring MVC Flash Attribute 的讲解与使用示例

Idiot_s_Sky

翻译于 3年前

5人顶

顶 翻译得不错哦!

虽然这一方法看起来很完美,并且解决了表单多次提交的问题,但是它又引入了一个获取请求参数和属性的难题. 通常当我们生成一次http重定向请求的时候,被存储到请求数据会丢失,使得下一次GET请求不可能访问到这次请求中的一些有用的信息.

Flash attributes 的到来就是为了处理这一情况. Flash attributes 为一个请求存储意图为另外一个请求所使用的属性提供了一条途径. Flash attributes 在对请求的重定向生效之前被临时存储(通常是在session)中,并且在重定向之后被立即移除.

Spring MVC Flash Attribute 的讲解与使用示例

为了这样做, Flash 特性使用了两个集合. FlashMap 被用来管理 flash attributes 而 FlashMapManager 则被用来存储,获取和管理 FlashMap 实体.

对于每一次请求一个 “input” flash map 会被创建,来存储来自任何之前请求的 flash attribute 还有一个 “output” flash map 会被创建,来存储任何我们存储在这个请求中的,之后的请求参数.

Spring MVC Flash Attribute 的讲解与使用示例

leoxu

翻译于 3年前

3人顶

顶 翻译得不错哦!

使用

要想在你的 Spring MVC 应用中使用 Flash attribute,要用 3.1 版本或以上。并且要在 spring-servlet.xml 文件中加入 mvc:annotation-driven。


这些都完成之后,Flash attribute 就会自动设为“开启”,以供使用了。只需在你的 Spring controller 方法中加入RedirectAttributes redirectAttributes。

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";
	}


addFlashAttribute 方法会自动向 output flash map 中添加给定的参数,并将它传递给后续的请求。

我们来看看一个使用 Flash attribute 来完成 POST/Redirect/GET 并传递一些信息的完整实例吧。

Flash Attribute 实例

下面的应用向用户显示一个表单。当用户填完数据,并提交表单之后,页面会重定向到另一个显示成功信息的页面。在这个重定向的新页面中,会显示用户刚才输入的信息。

Spring MVC Flash Attribute 的讲解与使用示例

戴仓薯

翻译于 3年前

4人顶

顶 翻译得不错哦!

第1步: 需要的 JAR 和项目结构

如果你用 Maven 来做依赖管理,用下面的 dependencies 来添加 Spring 3.1 MVC 的支持。

	
		
		
			org.springframework
			spring-webmvc
			3.1.2.RELEASE
		
		
		
			jstl
			jstl
			1.2
		
	


或者,你可以下载以下 JAR 文件,然后把它们放在 /WEB-INF/lib 文件夹下。

Spring MVC Flash Attribute 的讲解与使用示例

第2步: Spring 配置

要为 web 项目添加 Spring 支持,需要在 web.xml 中添加 DispatcherServlet 。
web.xml


	
	Spring MVC Flash attribute example
	
		spring
		
			org.springframework.web.servlet.DispatcherServlet		
		1
	
	    	default    	/index.html
		
	
		spring
		*.html
	


然后,spring-servlet 使用 mvc:annotation-driven 来支持 mvc ,并且会扫描项目中的 context:component-scan 标签。

spring-servlet.xml


		
	
	
		
		
		
	 
	
	
	 


第3步: Spring Controller – RedirectAttributes

Controller 的代码使用 Customer.java 对象作为 bean 来保存客户信息。

Customer.java

package net.viralpatel.spring;public class Customer {	private String firstname;	private String lastname;	private int age;	private String email;	//getter, setter methods}


CustomerController 类有3个方法。showForm 方法对应 URL /form ,用来显示 Add New Customer 表单。addCustomer 方法对应 URL /addcustomer ,用来处理 POST 请求。

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 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";
	}
}


注意我们在 addCustomer 方法中是如何使用 redirectAttributes 参数来添加 flash attribute 的。并且,我们是用 addFlashAttribute 方法来设置新的参数为 flash attribute。

Spring MVC Flash Attribute 的讲解与使用示例

戴仓薯

翻译于 3年前

4人顶

顶 翻译得不错哦!

第4步: View 层

add customer.JSP 文件显示一个 Add New Customer(添加新客户)表单。
add_customer.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
	

Add New Customer

Firstname  
Lastname  
Age  
Email  


show_customer.jsp 简单地显示客户的名和姓,以及用 flash attributes 设置的成功信息。

show_customer.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

${message}

${customer.lastname}, ${customer.firstname} added successfully..


Demo:

执行这个 web 项目即可。
URL: http://localhost:8080/SpringMVC_Flash_Attribute_Maven_example/form.html
Spring MVC Flash Attribute 的讲解与使用示例

Spring MVC Flash Attribute 的讲解与使用示例

本站所有代码来源请查看:×××/technology

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...