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

2025-10-14 12:35:39
第1步: 需要的 JAR 和项目结构 如果你用 Maven 来做依赖管理,用下面的 dependencies 来添加 Spring 3.1 MVC 的支持。 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 org.sprin...

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

org.springframework

spring-webmvc

3.1.2.RELEASE

jstl

jstl

1.2

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

第2步: Spring 配置

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

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

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

value="org.springframework.web.servlet.view.JstlView" />

第3步: Spring Controller – RedirectAttributes

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

Customer.java

?

1

2

3

4

5

6

7

8

9

10

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

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;

@Controller

public 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。