【问题标题】:Spring MVC PRG, passing values to other operationSpring MVC PRG,将值传递给其他操作
【发布时间】:2013-08-04 01:56:47
【问题描述】:

我想将值传递给重定向操作。即在下面的代码中,我想将 userResult 值传递给方法 welcome,我该如何传递它?

        String userResult = getUserDetails(url);
        System.out.println("Result-->"+userResult);
        if(userResult.contains("<user>")){
            return "redirect:welcome";
        }

重定向代码:

@RequestMapping(value = "/welcome")
    public String welcome(Model model){
        Element element = getOutputDetails(userResult);// get userResult values  here

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    如果你使用spring 3.1或更高版本,看看Spring MVC Flash Attribute

    该功能用于在重定向情况下在处理程序方法之间传递值。

    import org.springframework.web.servlet.mvc.support.RedirectAttributes;
    

    //...

    @RequestMapping...
    public String login(@ModelAttribute....,
            final RedirectAttributes redirectAttributes) {
        String userResult = getUserDetails(url);
        System.out.println("Result-->"+userResult);
        if(userResult.contains("<user>")){
            redirectAttributes.addFlashAttribute("userResult", userResult);
            return "redirect:welcome";
        }
        ......
    }
    
    @RequestMapping(value = "/welcome")
    public String welcome((@ModelAttribute("userResult") String  userResult){
        ......
    }
    

    【讨论】:

    • 如果你包括为什么应该使用 Flash 属性,我会赞成。仅仅添加一个链接是不够的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 2014-07-04
    • 2013-08-07
    相关资源
    最近更新 更多