Spring MVC PRG, passing values to other operation

2019-08-29 18:56发布

I want to pass values to the redirected operation. i.e in my below code I want to pass userResult value to the method welcome, how can I pass it?

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

redirected code:

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

1条回答
太酷不给撩
2楼-- · 2019-08-29 19:21

If you use spring 3.1 or above, have a look at Spring MVC Flash Attribute

The feature is used to pass values between handler methods in redirect situations.

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){
    ......
}
查看更多
登录 后发表回答