作者:ssslinppp
1. 摘要
程序流程:
- 前台使用ajax技术,传递json字符串到后台;
- 后台使用Spring MVC注解@RequestBody 接受前台传递的json字符串,并返回新的json字符串到前台;
- 前台接受后台传递过来的json数据,并显示。
2. 前台界面和js
<%&#64; page language&#61;"java" pageEncoding&#61;"UTF-8"%>
<%
String path &#61; request.getContextPath();
String basePath &#61; request.getScheme() &#43; "://" &#43; request.getServerName() &#43; ":" &#43; request.getServerPort() &#43; path &#43; "/";
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
%>
<%-- <link rel&#61;"stylesheet" type&#61;"text/css" href&#61;"<%&#61;basePath%>js/easyui/demo.css"> --%>
- style&#61;"padding:5px 0;">
SpringMVC &#64;RequestBody 接收Json数组对象
href&#61;"#" class&#61;"easyui-linkbutton" onclick&#61;"loadData()" data-options&#61;"iconCls:&#39;icon-add&#39;">&#64;RequestBody 接收Json数组对象
3. 后台java代码
http://localhost:8080/SpringMVCTest/test/index.action 请求后&#xff0c;返回index.jsp界面&#xff0c;如下图&#xff1a;
点击后会请求jsonDataReq
这是person.java类
4. 配置
web.xml
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
version&#61;"2.5" xmlns&#61;"http://java.sun.com/xml/ns/javaee" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation&#61;"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListenerspring org.springframework.web.servlet.DispatcherServlet 3 spring *.action
spring-servlet.xml
applicationContext.xml
5. 结果
6. 其他
淘宝&#xff1a;http://shop110473970.taobao.com/?spm&#61;a230r.7195193.1997079397.42.AvYpGW
http://shop125186102.taobao.com/?spm&#61;a1z10.1-c.0.0.SsuajD
来自为知笔记(Wiz)