项目目录结构
1.String
index.jsp
<a href="resp/testString">testString</a><br>
@RequestMapping("/testString")
public String testString(){
System.out.println("testString");
return "success";
// 下面两个并不通过视图解析器
// 我的success.jsp是直接放在webapp目录下的所以看起来两个目录相同
// 其实若将success.jsp放在xx目录下,forward:/xx/success.jsp,
// 而redirect路径不会发生变化
//return "forward:/success.jsp";
//return "redirect:/success.jsp";
}
2.void
index.jsp
<a href="resp/testVoid">testVoid</a><br>
@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("testVoid");
// 使用请求转发跳转
// request.getRequestDispatcher("/success.jsp").forward(request, response);
// 使用重定向跳转
response.sendRedirect(request.getContextPath()+"/success.jsp");
// 直接响应数据
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
response.getWriter().write("测试");
}
3.ModelAndView
success.jsp
注意mav.addObject的值存在request域中
<body>
<h3>success</h3>
${requestScope.temp}
</body>
index.jsp
<a href="resp/testModelAndView">testModelAndView</a><br>
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
System.out.println("testModelAndView");
ModelAndView mav = new ModelAndView();
// 走视图解析器
mav.setViewName("success");
mav.addObject("temp","xxx");
return mav;
}