0

我想将值传递给重定向操作。即在我下面的代码中,我想将 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
4

1 回答 1

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){
    ......
}
于 2013-08-04T05:26:49.553 回答