1. The response to the return value of type String
@RequestMapping("/testString")
public String testString(Model model) {
System.out.println("testString方法执行了。。。");
//模拟从数据库中查询出User对象
User user = new User();
user.setUsername("开发");
user.setPassword("123");
user.setAge(30);
//model对象:通过model将user对象传入"user"key值中
model.addAttribute("user", user);
return "success";
}
2. The response value is a void return type
① written request forwarding program
//返回值是void类型时
@RequestMapping("/testVoid")
public void testVoid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("testVoid方法执行了。。。");
//编写请求转发的程序
//前面返回字符串("success")可以找到success.jsp直接跳页面,是因为开启了视图解析器
//而这里手动写转发的方法不会开启视图解析器,所以这里要写"/WEB-INF/pages/success.jsp"
request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response);
return;
}
② redirection
//返回值是void类型时
@RequestMapping("/testVoid2")
public void testVoid2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("testVoid2方法执行了。。。");
//重定向只能写成"/xxx.jsp"的格式
response.sendRedirect(request.getContextPath() + "/index.jsp");
return;
}
③ direct response
//返回值是void类型时
@RequestMapping("/testVoid3")
public void testVoid3(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("testVoid3方法执行了。。。");
//解决中文乱码
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//直接进行响应
response.getWriter().print("你好");
return;
}
3. The return value is the response type ModelAndView
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
//创建ModelAndView对象
ModelAndView mv = new ModelAndView();
System.out.println("testModelAndView方法执行了。。。");
//模拟从数据库中查询出User对象
User user = new User();
user.setUsername("开发");
user.setPassword("123");
user.setAge(30);
//把user对象存储到mv对象时,它也会把user对象存入到request对象中
mv.addObject("user", user);
//跳转到指定页面,此处使用了视图解析器找到success.jsp
mv.setViewName("success");
return mv;
}
4. Use of the response forwarding (Forward) and redirection (the redirect) for page Jump
@RequestMapping("/testForwardOrRedirect")
public String testForwardOrRedirect() {
System.out.println("testForwardOrRedirect方法执行了。。。");
//请求的转发 forward
//使用forward关键字表示这里采用的是请求转发的方式,不能再使用视图解析器
return "forward:/WEB-INF/pages/success.jsp";
}
@RequestMapping("/testForwardOrRedirect2")
public String testForwardOrRedirect2() {
System.out.println("testForwardOrRedirect2方法执行了。。。");
//重定向
//使用redirect关键字表示这里采用的是重定向的方式,不能使用视图解析器,且格式只能是/xxx.jsp
return "redirect:/index.jsp";
}
The static response filtering of data resources json
In front controller webapp - WEB-INF - web.xml in.
Here in resources - resources which are not set springmvc.xml interception.
resources - springmvc.xml:
<!--前端控制器哪些资源不拦截-->
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/images/**" location="/images/"/>
<mvc:resources mapping="/js/**" location="/js/"/>
response.jsp:
set here webapp - under static resources jquery.min.js not be intercepted js directory
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery.min.js"></script>
<script>
//页面加载,绑定单击事件
$(function () {
$("#btn").click(function () {
alert("hello btn");
})
})
</script>
</head>
<body>
<button id="btn">发送ajax请求</button>
</body>
</html>