SpringMVC返回JSON数据注意点
-SpringMVC返回JSON数据
–配置SpringMVC文件
–Controller.java配置注解
–界面接收Controller的方法返回数据
配置SpringMVC.xml文件
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<!-- 配置文件形式要配置的组件:Controller, handlerMapping(有默认规则),ViewResolver,interceptor -->
<!-- 扫描 com.ssm.springmvc这个包下面的所有类 ,声明这句开启了可以向bean中注入的方式赋值 -->
<context:component-scan base-package="com.ssm.user.web" />
<!-- 启动Springmvc注解驱动,在SpringMVC 3.1 以前配置这些就自动扫描所有的,3.1版本以后的配置就稍微麻烦些 -->
<!-- 自动扫描指定路径中的注解,如@ResposityBody -->
<mvc:annotation-driven />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置到指定目录一端路径 ,建议指定浅一点的目录 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- 文件的后缀名 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
扫描Package的class里对应的方法中写逻辑代码并输出封装好的JSON数据
JSP界面请求ajax方法,封装JSON数据
// 在SpringMVC 中使用@ResponseBody即可表示返回的是文本数据,可以把json数据封装返回到界面
@RequestMapping(value = "/ajax.do",method=RequestMethod.GET)
@ResponseBody
public String test_jQuery(HttpServletRequest request, HttpServletResponse response) throws IOException{
List<UserModel> userModels = new ArrayList<UserModel>();
userModels.add(new UserModel(1, "张三", 23, "男", new java.sql.Date(System.currentTimeMillis())));
userModels.add(new UserModel(2, "李四", 46, "男", new java.sql.Date(System.currentTimeMillis()+10000)));
String jsonString = com.alibaba.fastjson.JSON.toJSONString(userModels);
// SpingMVC 返回JSON数据类型
return jsonString;
}
在上面这些内容必须要注意三个点
1.@RequestMapping中的两个参数。
value表示请求对应的方法,在界面中的链接带上ajax.do对应的就找到这个方法,并执行该方法代码;
2.method这个参数可以不写,不写时候默认为method=RequestMethod.GET,这时请求方式是GET方式可以成功请求。但是如果写了method的参数,如果配置请求方式为method=RequestMethod.POST,那么这时候请求执行方式必须为POST方式。
3.在@ResponseBody加上这个注解就可以成功返回text文本信息以及JSON数据,如果不加这个注解同样是得不到结果,返回404、405。
正确配置参数,请求方式前后端一样时候请求成功
jQuery的Ajax请求方式并返回结果处理数据
$("#btn").click(function(){
$.ajax({
url:"ajax.do",
type:"post",
async:true,
dataType:"text",
success:function(data){
alert("json数据封装成功请求并返回");
// eval 把text数据转换成JSON对象
var obj = eval("("+data+")");
if(obj[1].id == 2)
alert(obj[1].name+"今年\n"+obj[1].age+"岁");
alert(obj[0].name+"\n"+obj[1].name);
/* if (infos != "" || infos != null)
window.location.href="toAjax.do"; */
if(confirm("确定?")){
$("#test2").attr("style","background:blue;width:500px;height:300px;font-size:28px;");
$("#test2").text("后台处理返回数据结果: \n"+data);
$("#imgDiv img").attr("src","pictures/03.jpg");
}else{
$("#imgDiv img").attr("src","pictures/04.jpg");
//window.location.href="toAdd.do";
}
},
error:function(xhr,errorThrown){
$("#test2").text("后台处理返回数据结果: 数据请求出现错误!");
alert("请求错误代码:"+xhr.status);
}
});
});
当然上面的ajax接收参数可以是多种,可以直接使用dataType:”json”,这样接收的数据就是json类型,可以不用解析处理成json数据,这样方便简洁。