字符串
Controller方法发挥了字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。
@RequestMapping("/testReturnString") public String testReturnString() { System.out.println("AccountController 的 testReturnString 方法执行了。。。。"); //指定逻辑视图名,经过视图解析器解析为 jsp 物理路径:/WEB-INF/pages/success.jsp return "success"; }
Void
在Controller方法形参上可以定义request和response,使用request和response指定响应结果。
1、使用request转向页面
request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,
response);
2、也可以通过response页面重定向
response.sendRedirect("testRetrunString")
3、也可以通过response指定响应结果
response.setCharacterEncoding("utf-8"); response.setContentType("application/json;charset=utf-8"); response.getWriter().write("json 串");
ModelAndView
ModelAndView是SpringMVC为我们提供的一个对象,该对象也可以用作控制器方法的返回值。
// 添加模型到该对象当中 public ModelAndView addObject(String attributeName, Object attributeValue){ getModelMap().addAttribute(attributeName, attributeValue); return this; } // 设置逻辑视图名称,视图解析器会根据名称前往指定的视图 public void setViewName(@Nullable String viewName){ this.view = viewName; }
返回ModelAndView类型时,浏览器跳转只能是转发请求。