目录
本章学习源码Github地址:https://github.com/GuiZhouAndroid/MySpringMVCAllProject/tree/master/SpringMVCDemo03_Ajax
一、前端代码
点击Button按钮,触发点击事件,才此事件绑定在getlist()函数上。函数使用发送Ajax请求后端接口,获取学生集合的JSON数组,并遍历拼接展示在浏览器上。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<body>
<button onclick="getlist()">点击获取学生集合</button>
<div id="list">正在请求数据...</div>
<script type="text/javascript">
function getlist() {
$.ajax({
url: '${pageContext.request.contextPath}/list.do',
dataType: 'json',
success: function (result) {
let s = "";
$.each(result, function (item, stu) {
s += stu.name + "----" + stu.age + "</br>"
})
$("#list").html(s);
}
})
}
</script>
</body>
</html>
二、学生实体类
public class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
三、控制层
创建四个学生对象,放入List集合,Spring借助Jackson的ObjectMapper进行自动转换将List集合转为Json数组响应到Ajax。
@Controller
public class AjaxController {
@RequestMapping("/list.do")
@ResponseBody //用来解析Ajax的请求,使用此注解,必须在spirngmvc.xml中配置注解驱动
public List<Student> listStudent() {
List<Student> students = new ArrayList<>();
students.add(new Student("张三", 20));
students.add(new Student("李四", 30));
students.add(new Student("王五", 40));
students.add(new Student("赵六", 50));
return students;//SpirngMVC框架借助jackson会将此集合转为json数组,最终响应到ajax的success(result))形参中
}
}
四、springmvc.xml
(1)基于注解开发,添加包扫描
(2)使用注解@ResponseBody响应JSON格式返回,必须在springmvc.xml配置文件中添加注解驱动支持。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.dhrj.java.zsitking.controller"/>
<!-- 处理Ajax请求不用添加视图解析器,添加注解驱动专门为Ajax的请求提供服务 -->
<mvc:annotation-driven/>
</beans>
五、web.xml
(1)添加中文乱码过滤器
(2)注册SpringMVC中央控制器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
四、结果展示
配置Tomcat 9,访问接口。
五、总结
仅自己学习记录,如有错误,敬请谅解~,谢谢~~~