Flash Attributes in Spring MVC 3.1

本文介绍Spring 3.1中新增的闪存属性功能,该功能解决了POST/重定向/GET模式请求中出现的问题。文章通过示例详细讲解了如何使用闪存属性,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring 3.1 has added new feature called flash attributes. This is one of the most wanted feature from the spring developers. This solves the problem occured when POST/Redirect/Get pattern in the request. When user makes a request to the server via POST method, once the process is completed, the requested resourced will be served through either forward/redirect to the appropriate view. The problem with the forward is, when the user does F5 or Refresh the same page, the POST data is re-submitted because the request is same.

When we are using redirect, the forms are not re-submitted and only the final view is displayed. The real problem here is, when we invoke redirect, it loses all the request values which would be useful in the subsequent request. The developers used some workaround or@SessionAttributesto store the values. With the release of Spring 3.1, flash attributes solve that issue. Spring container stores the variables temporarily and destroys them after the request.



Are looking for the Spring beginner tutorials? Read our Getting Started Spring Tutorials .

FlashAttributesController.java

package javabeat.net;

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.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class FlashAttributesController {

  @RequestMapping(value="/flashexample")
  public String getResult(@ModelAttribute("user") UserDetails userDetails,
       final RedirectAttributes redirectAttributes){
    //Some logics goes here.

    //Before redirect set some values to Flash attributes
    redirectAttributes.addFlashAttribute("user", userDetails);
    redirectAttributes.addFlashAttribute("message", "Welcome to test page!!!");

    return "redirect:/flashredirect";
  }

  @RequestMapping(value="/flashredirect")
  public String getResultRedirect(@ModelAttribute UserDetails userDetails ){
    return "example";
  }

  @ModelAttribute("user")
  public UserDetails getUser(@RequestParam String userName){
    UserDetails details = new UserDetails();
    details.setUserName(userName);
    return details;
  }
}

example.jsp

<html>
<body>
  <h1>${message} for user ${user.userName}</h1>
</body>
</html>
  1. Add the RedirectAttributes in the controller method arguments.
  2. redirectAttributes.addFlashAttribute for adding any number of variables in the scope. Note that, this values are persisted for the very short time. Till the next request. we are not in the controll of where the variables are stored.
  3. While returning the view, use “redirect” for making the new requtest which is also defined in the same controller class.
  4. I have define @ModelAttribure for storing the values to a model object. The values are coming from the request parameter. This method is always invoked bfore the controller method.
  5. The above jsp file simply prints the message which is stored in the flash attributes.
  6. The above example is only for demonstrating the power of flash attributes, in real time the above example may not make sense because I have used model attributes to store the values, etc. only for making this example to run.

The spring configuration file is very much similar to any other examples used for the spring mvc application . However, don’t forget to add the following line of snippet in the configuration file.

<mvc:annotation-driven />

If you miss the above line, you may see the error like one below.

nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException:
Failed to invoke handler method [public java.lang.String javabeat.net.FlashAttributesController.
getResult(javabeat.net.UserDetails,org.springframework.web.servlet.mvc.support.
RedirectAttributes)]; nested exception is java.lang.IllegalStateException: Argument [RedirectAttributes] is of
type Model or Map but is not assignable from the actual model. You may need to switch newer MVC infrastructure
classes to use this argument.

If you run the application and access the web page using this URL:

http://localhost:8080/SpringExamples/flashexample?userName=TestUser

It will be reditec to this page:

http://localhost:8080/SpringExamples/flashredirect

Flash-Attributes-Spring-Mvc



Are looking for the Spring beginner tutorials? Read our Getting Started Spring Tutorials .

FlashAttributesController.java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值