项目结构
依赖pom.xml
<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.8.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.8.RELEASE</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.5</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>
web.xml
<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><!-- 配置前端控制器--><servlet><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 编码过滤器--><filter><filter-name>encodingFilter</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></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
</web-app>
addStudent.jsp
取得input 的输入值然后编写json数据,JSON.stringify(student) 将student 转化为json对象
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title><script src="/JQ/jquery.js"></script><script>$(function (){$(":button").click(function (){let student = {"id":$("[name=id]:text").val(),"name":$("[name=name]:text").val(),"age":$("[name=age]:text").val(),"subject":$("[name=subject]:text").val(),"address":$("[name=address]:text").val()};$.ajax({"url":"http://localhost:8080/testAjax","type":"POST","dataType":"json","contentType":"application/json","data":JSON.stringify(student),"success":function (data){console.log(data)}})})})</script>
</head>
<body>
<div><input type="text" name="id" placeholder="ID"><br><input type="text" name="name" placeholder="name"><br><input type="text" name="age" placeholder="age"><br><input type="text" name="subject" placeholder="subject"><br><input type="text" name="address" placeholder="address"><br><input type="button" value="添加"><br>
</div>
</body>
</html>
Student实体类,get set toString方法
StudentController代码
package com.tmg.controller;import com.tmg.domain.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.ArrayList;
import java.util.List;@Controller
public class StudentController {List<Student> students=new ArrayList<>();@ResponseBody@RequestMapping("testAjax")public List<Student> testAjax(@RequestBody Student student){students.add(student);return students;}@RequestMapping("hello")public String toIndex(){System.out.println("hello");return "addStudent";}
}
resources下spring.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.tmg"></context:component-scan><!-- 配置视图解析器--><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean><!-- 配置静态资源的处理器 --><mvc:default-servlet-handler></mvc:default-servlet-handler><!-- 配置注解驱动--><mvc:annotation-driven></mvc:annotation-driven></beans>