如何获取我可以设置的请求/响应?此外,在此方法结束时,我如何才能重定向到另一个页面?
@RequestMapping(value = "/dosomething", method = RequestMethod.GET)
public RETURNREDIRECTOBJ dosomething() throws IOException {
....
return returnredirectpagejsp;
}
答案 0 :(得分:13)
这个怎么样:
@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;
}
答案 1 :(得分:7)
public String doSomething(HttpServletRequest request)
。您可以单独传递请求和响应,也可以传递每个请求。String
"redirect:/viewname"
(通常没有.jsp
后缀)对于这两个问题,请检查the documentation,“15.3.2.3支持的处理程序方法参数和返回类型”部分
答案 2 :(得分:4)
您也可以@Autowire
。例如:
@Autowired
private HttpServletRequest request;
虽然HttpServletRequest是请求范围的bean,但它不要求你的控制器是请求范围的,因为HttpServletRequest Spring会生成一个代理HttpServletRequest,它知道如何获取实际的请求实例。
答案 3 :(得分:0)
您也可以使用这种方式
@RequestMapping(value = "/url", method = RequestMethod.GET)
public String method(HttpServletRequest request, HttpServletResponse response){
Cookie newCookie = new Cookie("key", "value");
response.addCookie(newCookie);
return "redirect:/newurl";
}