在Controller间带POJO参数跳转

自己在做一个SSM项目的时候,需要在带一个POJO参数(简单类型也可以用)从ControllerA跳转到ControllerB,记录一下,后面要用的话可以查阅

ControllerA:

@RequestMapping(value="/StandardTestIPInput")
    public String StandardTestIPInput(String testServersIp, Model model) {
        model.addAttribute("correctIpList", correctIpList);
        return "redirect:/StandardTest/confirmServer";
    }

ControllerB:

@RequestMapping(value="/confirmServer", method=RequestMethod.GET)
    public void confirmServer(@RequestParam(value="correctIpList",required=true) ArrayList correctIpList) {
        System.out.println(correctIpList);
    }

2018-4-20 更新
在开发过程中有需求要在controller间跳转后再返回前端,但是不能再地址中带参数。研究了之后发现用redirectAttributes的addFlashAttribute()方法是可以实现的。之前在网上找文章看到用这个方法之后在接收的controller无法接收到参数,后来发现是用的注释有问题,把接收的参数注释为@ModelAttribute,方法见下:
ControllerA:

@RequestMapping(value="/StandardTestIPInput")
    public String StandardTestIPInput(String testServersIp, RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("correctIpList", correctIpList);
        return "redirect:/StandardTest/confirmServer";
    }

ControllerB:

@RequestMapping(value="/confirmServer", method=RequestMethod.GET)
    public void confirmServer(@ModelAttribute(value="correctIpList") ArrayList correctIpList) {
        System.out.println(correctIpList);
    }

你可能感兴趣的:(Java)