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



http://www.open-open.com/lib/view/open1397266120028.html



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

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



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

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

使用


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


<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<body>
<h1>${message}</h1>
    ${customer.lastname}, ${customer.firstname} added successfully..
</body>
</html>

猜你喜欢

转载自cai-bird.iteye.com/blog/2209422
今日推荐