如何在springMVC中从其他控制器中的控制器发送参数?

时间:2018-08-15 11:25:54

标签: java spring spring-mvc

在post方法中,我确实生成了一个对象,然后尝试将其发送到另一个Controller:

@PostMapping("/main")
    public ModelAndView makesQuery(
            @RequestParam String query
    ){
//   create new explorer if
        Explorer explorer = explorerService.createByQuery(query);

//   determine status of explorer
        StatusEnum q = StatusEnum.getEnum(explorer.getStatus().getStatusName());

        String http = q == StatusEnum.FINISHED ?
                "redirect:/show" : "redirect:/parsing";

        ModelAndView modelAndView = new ModelAndView(http);
        modelAndView.addObject("explorer", explorer);

        return modelAndView;
    }

在下一个方法中,我尝试获取对象资源管理器:

@GetMapping("/parsing")
public ModelAndView makesQuery(
        @ModelAttribute Explorer explorer
) {
    ModelAndView modelAndView = new ModelAndView("search");
    modelAndView.addObject("explorer", explorer);
    return modelAndView;
}

但是有下一个错误:

  

错误解析模板“正在解析”,模板可能不存在或可能   任何已配置的模板解析器都无法访问


当我将Explorer的注释从@ModelAttribute更改为@RequestParam时:

@GetMapping("/parsing")
public ModelAndView makesQuery(
        @RequestParam Explorer explorer
) {......

我在浏览器中确实有错误:

  

此应用程序没有针对/ error的显式映射,因此您看到了   作为后备。

     

2018年8月15日星期三EEST 2018发生意外错误(type = Bad   请求,状态= 400)。不是必需的资源管理器参数“ explorer”   礼物

PS search.html文件的主体没有任何逻辑-仅字符串“ test”。

1 个答案:

答案 0 :(得分:1)

您可以将探索对象保存在会话中。

HttpSession会话= request.getSession(true); session.setAttribute(“ explorer”,资源管理器)

并获得

@GetMapping(“ / parsing”) 公共ModelAndView makeQuery(HttpServletRequest请求..) HttpSession会话= request.getSession(); 探索Explore = session.getAttribute(“ exolorer”)