springMvc文件:
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<context:component-scan base-package="com.lanou.test.*"></context:component-scan>
<!-- 开启MVC注解,可以写@RequestMapping -->
<context:annotation-config />
<!--设置配置方案 -->
<mvc:annotation-driven />
<!-- 处理静态文件css,js,html(springmvc不处理静态的) -->
<mvc:default-servlet-handler/>
<!-- 设置spring:页面转发的路径前缀prefix,后缀suffix -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" /><!-- 在根目录找 -->
<property name="suffix" value=".jsp" /><!-- 文件后缀为jsp -->
</bean>
</beans>
在控制层中:
// 6.1生成JSON数据格式(变量)
@RequestMapping("/json")
@ResponseBody//将所有的,Map,List集合转成json
public String json() {
return "success";
}
// 6.2生成JSON数据格式(对象)
@RequestMapping("/json2")
@ResponseBody
public Bookinfo json2() {
return bookinfoService.getBookinfoById();
}
// 6.3生成JSON数据格式(集合)
@RequestMapping("/json3")
@ResponseBody
public List<Bookinfo> json3(){
System.out.println(bookinfoService.getBookinfos());
return (ArrayList<Bookinfo>)bookinfoService.getBookinfos()
}
在html中:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSON的解析</title>
<!-- <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> -->
<script type="text/javascript" src="jquery-1.7.js"></script>
<script type="text/javascript">
$(function(){
// 事件绑定
$("#btnMessage").click(function(){
//alert("谁点我...");
$.ajax({
type:"POST",//请求到服务器的方式,此处为POST方法
url:"bookinfo/json.json",//请求的服务器地址,此处调用json3方法
dataType:"text",
success:function(data){//data为返回给客户端的JSON数据,存储在data这个变量里面
//alert("从服务器异步返回的数据变量是:"+data);
$("#msg").html("从服务器异步返回的数据变量是:"+data);
}
})
});
//对象
$("#btnMessage2").click(function(){
$.ajax({
type:"POST",//请求到服务器的方式,此处为POST方法
url:"bookinfo/json2.json", //请求的服务器地址,此处调用json3方法
success:function(data){ //data为返回给客户端的JSON数据,存储在data这个变量里面
$("#msg").html("从服务器异步返回的数据对象是:"+data.bookid+" "+data.bookname+" "+data.bookprice+" "+ data.bookimg);
}
});
});
//集合
var str = "";
$("#btnMessage3").click(function(){
$.ajax({
type:"POST",
url:"bookinfo/json3.json",
success:function(data){
$.each(data,function(index,bookinfo){
//alert("索引:"+index);
str =str + bookinfo.bookid+" "+bookinfo.bookname+" "+bookinfo.bookprice+" "+ bookinfo.bookimg+"<br/>";
});
$("#msg").html(str);
if (typeof(str) != "undefined")
{
str = "";
}
}
});
});
})
</script>
</head>
<body>
<input type="button" value="点击我得到JSON格式的消息变量" id ="btnMessage" />
<input type="button" value="点击我得到JSON格式的消息对象" id="btnMessage2" />
<input type="button" value="点击我得到JSON格式的消息集合" id="btnMessage3" />
<div id="msg">
</div>
</body>
</html>