SpringMVC 之@RequestBody 接收Json数组对象

  1. 前台使用ajax技术,传递json字符串到后台;
  2. 后台使用Spring MVC注解@RequestBody 接受前台传递的json字符串,并返回新的json字符串到前台;
  3. 前台接受后台传递过来的json数据,并显示。
技术分享

2. 前台界面和js

技术分享
 
  
    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>

技术分享

3. 后台java代码


技术分享
http://localhost:8080/SpringMVCTest/test/index.action 请求后,返回index.jsp界面,如下图:
技术分享 
点击后会请求jsonDataReq
技术分享
这是person.java类
技术分享


4. 配置


web.xml

技术分享
 
  
    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

技术分享

applicationContext.xml

技术分享

5. 结果



技术分享

技术分享
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值