1.在response.jsp中加入一个a标签
<a href="user/testVoid">testVoid</a>
2.在UserController中加入testVoid方法
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/testVoid")
public void testVoid(Model model){
System.out.println("testVoid方法执行了...");
}
}
3.部署,失败,发现会自动找testvoid.jsp
4.解决:重新写testvoid方法
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception{
System.out.println("testVoid方法执行了...");
request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);
return;
}
}
5.再次部署,成功
6.重定向
@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception{
System.out.println("testVoid方法执行了...");
response.sendRedirect(request.getContextPath()+"/index.jsp");
return;
}
6.部署,成功
7.直接进行响应
@Controller
@RequestMapping("/user")
public class UserController {
/**
* 返回void
* 请求转发一次请求,不用编写项目的名称
*/
@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception{
System.out.println("testVoid方法执行了...");
//设置中文乱码
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//直接进行响应
response.getWriter().print("你好");
return;
}
}
8.部署