Skip to content

Commit ce5ddf7

Browse files
committed
springmvc转json数据
1 parent 4d9e177 commit ce5ddf7

File tree

7 files changed

+242
-2
lines changed

7 files changed

+242
-2
lines changed
Binary file not shown.
202 KB
Binary file not shown.
906 KB
Binary file not shown.

WebRoot/js/jquery-1.4.4.min.js

Lines changed: 167 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WebRoot/jsonTest.jsp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<%@ page language="java" contentType="text/html; charset=UTF-8"
2+
pageEncoding="UTF-8"%>
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4+
<html>
5+
<head>
6+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7+
<title>json交互测试</title>
8+
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
9+
<script type="text/javascript">
10+
//请求json,输出是json
11+
function requestJson(){
12+
13+
var jsonData = {
14+
"name" : "手机",
15+
"price" : "999"
16+
};
17+
$.ajax({
18+
type:'post',
19+
url:'${pageContext.request.contextPath }/requestJson.action',
20+
contentType:'application/json;charset=utf-8',
21+
//数据格式是json串,商品信息
22+
data:JSON.stringify(jsonData),
23+
success:function(data){//返回json结果
24+
alert(data.name);
25+
}
26+
27+
});
28+
29+
30+
}
31+
//请求key/value,输出是json
32+
function responseJson(){
33+
34+
$.ajax({
35+
type:'post',
36+
url:'${pageContext.request.contextPath }/responseJson.action',
37+
//请求是key/value这里不需要指定contentType,因为默认就 是key/value类型
38+
//contentType:'application/json;charset=utf-8',
39+
//数据格式是key/value,商品信息
40+
data:'name=手机&price=999',
41+
success:function(data){//返回json结果
42+
alert(data.name);
43+
}
44+
45+
});
46+
47+
}
48+
</script>
49+
</head>
50+
<body>
51+
<input type="button" onclick="requestJson()" value="请求json,输出是json"/>
52+
<input type="button" onclick="responseJson()" value="请求key/value,输出是json"/>
53+
</body>
54+
</html>

config/spring/springmvc.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
6969
实际开发时使用mvc:annotation-driven
7070
-->
71-
<mvc:annotation-driven conversion-service="conversionService"
72-
validator="validator"></mvc:annotation-driven>
71+
<mvc:annotation-driven conversion-service="conversionService" validator="validator">
72+
</mvc:annotation-driven>
7373

7474
<!-- 也不用配置具体的Controller了,直接使用这个进行扫描包即可,也可以扫描service -->
7575
<context:component-scan base-package="ssm.controller"></context:component-scan>

src/ssm/controller/ItemsController.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import org.springframework.validation.BindingResult;
1313
import org.springframework.validation.ObjectError;
1414
import org.springframework.validation.annotation.Validated;
15+
import org.springframework.web.bind.annotation.RequestBody;
1516
import org.springframework.web.bind.annotation.RequestMapping;
1617
import org.springframework.web.bind.annotation.RequestParam;
18+
import org.springframework.web.bind.annotation.ResponseBody;
1719
import org.springframework.web.multipart.MultipartFile;
1820
import org.springframework.web.servlet.ModelAndView;
1921

@@ -177,5 +179,22 @@ public String editItemsQueryResult(ItemsQueryVo itemsQueryVo)
177179
// 下面打个断点,进来后看看itemsQueryVo中的List<ItemsCustom>属性有没有正确接收参数
178180
return "/WEB-INF/jsp/success.jsp";
179181
}
182+
183+
//测试请求的是json串(商品信息),输出json(商品信息)
184+
//@RequestBody将请求的商品信息的json串转成itemsCustom对象
185+
//@ResponseBody将itemsCustom对象转成json输出
186+
@RequestMapping("/requestJson")
187+
public @ResponseBody ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom) {
188+
189+
return itemsCustom; //由于@ResponseBody注解,将itemsCustom转成json格式返回
190+
}
191+
192+
//测试请求的是key/value(商品信息),输出json(商品信息)
193+
//@ResponseBody将itemsCustom对象转成json输出
194+
@RequestMapping("/responseJson")
195+
public @ResponseBody ItemsCustom responseJson(ItemsCustom itemsCustom) {
196+
197+
return itemsCustom; //由于@ResponseBody注解,将itemsCustom转成json格式返回
198+
}
180199

181200
}

0 commit comments

Comments
 (0)