Spring控制器获取请求/响应

com*_*tta 9 java spring spring-mvc

我如何获得可以设置的请求/响应?此外,在此方法的最后,我如何才能重定向到另一个页面?

@RequestMapping(value = "/dosomething", method = RequestMethod.GET)
public RETURNREDIRECTOBJ dosomething() throws IOException {
    ....
    return returnredirectpagejsp;
}
Run Code Online (Sandbox Code Playgroud)

Mat*_* B. 14

这个怎么样:

@RequestMapping(value = "/dosomething", method = RequestMethod.GET)
public ModelAndView dosomething(HttpServletRequest request, HttpServletResponse response)  throws IOException {
    // setup your Cookie here
    response.setCookie(cookie)
    ModelAndView mav = new ModelAndView();
    mav.setViewName("redirect:/other-page");

    return mav;
}
Run Code Online (Sandbox Code Playgroud)


Boz*_*zho 7

  1. 把它作为参数传递:public String doSomething(HttpServletRequest request).您可以单独传递请求和响应,也可以传递每个请求和响应.
  2. 返回String "redirect:/viewname"(最常没有.jsp后缀)

有关这两个问题,请查看文档 "15.3.2.3支持的处理程序方法参数和返回类型"部分