SpringMVC 之@RequestBody 接收Json数组对象

时间:2024-01-05 17:30:38

1. 摘要


程序流程:
  1. 前台使用ajax技术,传递json字符串到后台;
  2. 后台使用Spring MVC注解@RequestBody 接受前台传递的json字符串,并返回新的json字符串到前台;
  3. 前台接受后台传递过来的json数据,并显示。
SpringMVC 之@RequestBody 接收Json数组对象
2. 前台界面和js

SpringMVC 之@RequestBody 接收Json数组对象
  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  5. response.setHeader("Pragma", "no-cache");
  6. response.setHeader("Cache-Control", "no-cache");
  7. response.setDateHeader("Expires", 0);
  8. %>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html>
  11. <head>
  12. <title>SpringMVC+ajax+json</title>
  13. <script type="text/javascript">var basePath = "<%=basePath%>";</script>
  14. <%-- <link rel="stylesheet" type="text/css" href="<%=basePath%>js/easyui/demo.css"> --%>
  15. <script type="text/javascript" src="<%=basePath%>js/JQuery/jquery.min.js"></script>
  16. <script type="text/javascript" src="<%=basePath%>js/test/ajaxAndJson.js"></script>
  17. </head>
  18. <body>
  19. <div style="padding:5px 0;">
  20. <p>SpringMVC @RequestBody 接收Json数组对象</p>
  21. <a href="#" class="easyui-linkbutton" onclick="loadData()" data-options="iconCls:'icon-add'">@RequestBody 接收Json数组对象</a>
  22. </div>
  23. </body>
  24. </html>

SpringMVC 之@RequestBody 接收Json数组对象

3. 后台java代码


SpringMVC 之@RequestBody 接收Json数组对象
http://localhost:8080/SpringMVCTest/test/index.action 请求后,返回index.jsp界面,如下图:
SpringMVC 之@RequestBody 接收Json数组对象 
点击后会请求jsonDataReq
SpringMVC 之@RequestBody 接收Json数组对象
这是person.java类
SpringMVC 之@RequestBody 接收Json数组对象
 

4. 配置


web.xml

SpringMVC 之@RequestBody 接收Json数组对象
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  4. <!-- 从类路径下加载Spring配置文件,classpath关键字特指从类路径下加载 -->
  5. <context-param>
  6. <param-name>contextConfigLocation</param-name>
  7. <param-value>classpath:applicationContext.xml</param-value>
  8. </context-param>
  9. <!-- 负责启动Spring容器的监听器 -->
  10. <listener><listener-class>org.springframework.web.context.ContextLoaderListener
  11. </listener-class></listener>
  12. <!-- 配置Spring MVC,其对应的配置文件为:servlet-name-servlet.xml, 本项目的为:spring-servlet.xml -->
  13. <servlet>
  14. <servlet-name>spring</servlet-name>
  15. <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
  16. <load-on-startup>3</load-on-startup>
  17. </servlet>
  18. <servlet-mapping>
  19. <servlet-name>spring</servlet-name>
  20. <url-pattern>*.action</url-pattern>
  21. </servlet-mapping>
  22. </web-app>

spring-servlet.xml

SpringMVC 之@RequestBody 接收Json数组对象

applicationContext.xml

SpringMVC 之@RequestBody 接收Json数组对象

5. 结果


SpringMVC 之@RequestBody 接收Json数组对象
SpringMVC 之@RequestBody 接收Json数组对象