一、SpringMVC使用Ajax
导入有关jar包
编写处理器,使用@ResponseBody
@Controller
public class AjaxTestController {
@Autowired
private EmployeeService employeeService;
@ResponseBody //这个注解是将返回的数据放在响应体重,如果是对象,jackson包自动将对象转为json格式
@RequestMapping("/ajaxGetAll")
public Collection<Employee> ajaxGetAll(){
Collection<Employee> employeeCollection = employeeService.getAll();
return employeeCollection;
}
}
使用js绑定单击事件
<html>
<head>
<title>ajax获取员工信息</title>
<%
pageContext.setAttribute("ctp",request.getContextPath());
%>
<script type="text/javascript" src="/scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function () {
$("a:first").click(function () {
$.ajax({
url:"${ctp}/ajaxGetAll",
type:"GET",
success:function (data) {
$.each(data,function () {
var empInfo = this.lastName + "-->" + this.birth + "-->" + this.gender;
$("div").append(empInfo + "<br/>");
})
}
})
return false;
});
});
</script>
</head>
<body>
<a href="${ctp}/ajaxGetAll">ajax获取所有员工信息</a>
<div>
</div>
</body>
</html>
页面会返回json格式的数据
二、@RequestBody
注解获取请求体
页面提交表单数据
<html>
<head>
<title>Title</title>
<%
pageContext.setAttribute("ctp",request.getContextPath());
%>
</head>
<body>
<form action="${ctp}/testRequestBody" method="post" enctype="multipart/form-data">
<input name="username" value="zhangsan">
<input name="password" value="123">
<input type="file">
<input type="submit" value="提交">
</form>
</body>
</html>
处理器映射方法
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody String body){ //获取一个请求的请求体,还可以接受json数据
System.out.println("请求体:" + body);
return "success";
}
输出结果:
三、Ajax发送json数据给服务器
页面使用js提交一个对象的json字符串
<html>
<head>
<title>Title</title>
<script type="text/javascript" src="scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function () {
$("a:first").click(function () {
//点击发送Ajax请求,请求带的数据是json
var emp = {lastName: "张三", email: "zhangsan@qq.com", gender: 0};
//js对象转成字符串
var empStr = JSON.stringify(emp);
$.ajax({
url:"${ctp}/testRequestBody",
type:"POST",
data:empStr,
success:function (data) {
alert(data);
}
});
return false;
});
});
</script>
<%
pageContext.setAttribute("ctp",request.getContextPath());
%>
</head>
<body>
<form action="${ctp}/testRequestBody" method="post" enctype="multipart/form-data">
<input name="username" value="zhangsan">
<input name="password" value="123">
<input type="file" name="file">
<input type="submit" value="提交">
</form>
<a href="${ctp}/testRequestBody">Ajax方式json数据给服务器</a>
</body>
</html>
处理器把json格式请求体封装成一个对象
@RequestMapping("/testRequestBody")
public String testRequestBody(@RequestBody Employee employee){ //获取一个请求的请求体
System.out.println("请求体:" + employee);
return "success";
}
输出结果:
这样就可以使用@RequestBody
注解可以将Ajax发送的json数据封装成对象,发送给服务器。
@ResponseBody
注解是用来把对象转为json数据,返回给浏览器
@RequestBody
注解可以获取请求体内容,还可以接收json数据,封装成对象
四、HttpEntity
获取请求头
处理器获取请求头
@RequestMapping("/testHttpEntity")
public String testHttpEntity(HttpEntity<String> stringHttpEntity){
System.out.println("请求头:" + stringHttpEntity);
return "success";
}
输出结果:
请求头:<[host:"localhost:8080", connection:"keep-alive", sec-ch-ua:"" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"", sec-ch-ua-mobile:"?0", upgrade-insecure-requests:"1", user-agent:"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36", accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", sec-fetch-site:"none", sec-fetch-mode:"navigate", sec-fetch-user:"?1", sec-fetch-dest:"document", accept-encoding:"gzip, deflate, br", accept-language:"zh-CN,zh;q=0.9,en;q=0.8", cookie:"Idea-c84feff0=ba847224-8323-4f0a-a410-64d214bf9cb8; JSESSIONID=DD868CF6947C1B9E8339F2BB66C6697B"]>
五、ResponseEntity
返回响应数据及响应头
@ResponseBody
注解是将返回的数据放在响应体中,而并不是跳转页面
@ResponseBody
@RequestMapping("/testResponseBody")
public String testResponseBody(){
return "success";
}
ResponseEntity
可以返回响应体中内容的类型
@RequestMapping("/testResponseEntity")
public ResponseEntity<String> testResponseEntity(){
HttpStatus statusCode;
MultiValueMap<String, String> headers = new HttpHeaders();
String body = "<h1>SUCCESS</h1>"; //返回的数据放在响应体中
headers.add("Set-Cookie","username=zhangsan"); //自定义响应头
ResponseEntity<String> stringResponseEntity = new ResponseEntity<>(body, headers, HttpStatus.OK);
return stringResponseEntity;
}
响应结果: