响应数据和结果视图
返回值是String
<a href="user/testString">testString</a>
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/testString")
public String testString(Model model) {
System.out.println("testString执行了...");
//模拟从数据库中查询出user对象
User user = new User();
user.setUsername("阿颜");
user.setPassword("123");
user.setAge(24);
//model对象
model.addAttribute("user", user);
return "success";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>执行成功</h1>
${user.username}
${user.password}
${user.age}
</body>
返回值是void
<a href="user/testVoid">返回Void</a>
@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);
//重定向
//response.sendRedirect(request.getContextPath()+"/index.jsp");
//设置中文乱码
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
//直接会响应
response.getWriter().println("你好");
return;
}
返回值是ModelAndView对象
<a href="user/testModelAndView">返回ModelAndView</a>
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
//创建ModelAndView对象
ModelAndView mv = new ModelAndView();
System.out.println("testModelAndView执行了...");
//模拟从数据库中查询出user对象
User user = new User();
user.setUsername("阿超");
user.setPassword("123456");
user.setAge(24);
//把user对象存储到mv对象中,也会把user对象存入request对象
mv.addObject("user",user);
//跳转到哪个页面
mv.setViewName("success");
return mv;
}
使用关键字的方式进行转发或重定向
<a href="user/testForwardOrRedirect">testForwardOrRedirect</a>
@RequestMapping("/testForwardOrRedirect")
public String testForwardOrRedirect() {
System.out.println("testForwardOrRedirect执行了...");
//请求的转发
//return "forward:/WEB-INF/pages/success.jsp";
//重定向
return "redirect:/index.jsp";
}
ResponseBody响应json数据
<script src="js/jquery-3.5.1.min.js"></script>
<script>
//页面加载,绑定点击事件
$(function(){
$("#btn").click(function(){
//alert("hello btn")
$.ajax({
//编写json格式,设置属性和值
url:"user/testAjax",
contentType:"application/json;charset=UTF-8",
data:'{"username":"haha","password":"12345","age":23}',
dataType:"json",
type:"post",
success:function (data){
//data服务器端响应的json的数据,进行解析
}
})
});
});
</script>
导入依赖
<!-- json字符串与Javabean对象相互转换过程中,需要用到jackson的jar包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
@RequestMapping("/testAjax")
public @ResponseBody User testAjax(@RequestBody User user ) {
System.out.println("testAjax执行了...");
//客户端发送Ajax的请求,传的是json字符串,后端把json字符串封装到user对象中
System.out.println(user);
//做响应,模拟查询数据库
user.setUsername("ddd");
user.setAge(40);
//做响应
return user;
}
<script>
//页面加载,绑定点击事件
$(function(){
$("#btn").click(function(){
//alert("hello btn")
$.ajax({
//编写json格式,设置属性和值
url:"user/testAjax",
contentType:"application/json;charset=UTF-8",
data:'{"username":"haha","password":"12345","age":23}',
dataType:"json",
type:"post",
success:function (data){
//data服务器端响应的json的数据,进行解析
alert(data);
alert(data.username);
alert(data.password);
alert(data.age);
}
})
});
});
</script>