有三种方法
第一种、返回ModelAndView
controller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。
第二种、返回void
在controller方法形参上可以定义request和response,使用request或response指定响应结果:
1、使用request转向页面,如下:
request.getRequestDispatcher(“页面路径”).forward(request, response);
2、也可以通过response页面重定向:
response.sendRedirect(“url”)
3、也可以通过response指定响应结果,例如响应json数据如下:
response.setCharacterEncoding(“utf-8”);
response.setContentType(“application/json;charset=utf-8”);
response.getWriter().write(“json串”);
第三种、返回字符串
// @Controller 返回字符串
@RequestMapping("/transferReturnString1")
public String transferReturnString1()throws Exception{
return "transfer"; //转发到 transfer.jsp
}
@RequestMapping("/transferReturnString2")
public String transferReturnString2()throws Exception{
return "redirect:/transfer.jsp"; //重定向到 transfer.jsp
}
@RequestMapping("/transferReturnString3")
public String transferReturnString3()throws Exception{
return "redirect:/money/transferJsonObject.do";//重定向到transferJsonObject.do
}
@RequestMapping("/transferReturnString4")
public String transferReturnString4()throws Exception{
return "forward:/money/transferJsonObject.do";//转发到transferJsonObject.do
}