SpringMVC札集(07)——JSON数据
自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理
探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制
Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南
有时浏览器发送给服务器的请求数据是JSON格式;有时服务器返回给浏览器的数据是JSON格式。那么,SpringMVC是怎么支持JSON格式的呢?在SpringMVC可用@RequestBody和@ResponseBody这两个注解来处理JSON数据。
@RequestBody注解用于读取http请求的内容,通过SpringMVC提供的HttpMessageConverter接口将读到的内容转换为Object并绑定到controller方法的参数上。
@ResponseBody注解用于将Controller的方法返回的对象通过HttpMessageConverter接口转换为指定格式的数据(如:json,xml等)后再通过Response响应给客户端
在本篇博客中,我们举两个例子:
第一个例子:请求数据的格式为JSON,返回数据的格式也为JSON
第二个例子:请求数据的格式为Object,返回数据的格式为JSON
具体实现,请往下看。
第一步:配置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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 配置自动扫描 -->
<context:component-scan base-package="cn.com"></context:component-scan>
<!-- 配置注解开发所需的处理器映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置注解开发所需的处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
请注意,在处理器适配器中配置消息转换器messageConverters
第二步:编写jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>测试SpringMVC对于JSON数据的处理</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.js">
</script>
<script type="text/javascript">
//第一个例子:请求数据的格式为JSON,返回数据的格式也为JSON
function requestJsonData(){
var userJSON = JSON.stringify({id : "9527",username : "周星星",sex : "男",address : "香港"});
$.ajax({
type : 'POST',
url : '${pageContext.request.contextPath}/json/requestJson.do',
contentType : 'application/json;charset=utf-8',
dataType: 'json',
data : userJSON,
success : function(data) {
alert(data.username + "," + data.id + "," + data.sex + "," + data.address);
}
})
}
//第二个例子:请求数据的格式为Object,返回数据的格式为JSON
function requestObjectData() {
$.ajax({
type : 'POST',
url : '${pageContext.request.contextPath}/json/requestObject.do',
contentType :'application/x-www-form-urlencoded;charset=utf-8',
dataType: 'json',
data : 'id=9527&username=周星星&sex=男&address=香港',
success : function(data) {
alert(data.username + "," + data.id + "," + data.sex + ","+ data.address);
}
})
}
</script>
</head>
<body>
<br>
<input type="button" onclick="requestJsonData()" value="第一个例子:请求数据的格式为JSON,返回数据的格式也为JSON">
<br>
<br>
<input type="button" onclick="requestObjectData()" value="第二个例子:请求数据的格式为Object,返回数据的格式为JSON">
</body>
</html>
- 在AJAX中利用dataTye表示服务器返回的数据类型
- 在AJAX中利用contentType表示发送的数据的类型
- 在AJAX中利用data表示发起请求时携带至服务器的数据
- 在AJAX中利用success : function(data) {}表示请求的回调,其中data表示服务器返回的数据
在第一个例子中:请求数据的格式为JSON,服务器返回的数据的格式也为JSON。
在第二个例子中:请求数据的格式为Object,服务器返回的数据的格式也为JSON。
运行后效果如下:
第三步:实现Controller
/**
* @author 原创作者:谷哥的小弟
* @blog 博客地址:http://blog.csdn.net/lfdfhl
* @time 创建时间:2017年7月30日 上午8:38:26
* @info 描述信息:SpringMVC对于JSON数据的处理
*/
package cn.com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.com.domain.User;
@Controller
@RequestMapping("/json")
public class AnnotationController {
//第一个例子:请求数据的格式为JSON,返回数据的格式也为JSON
@RequestMapping("requestJson")
public @ResponseBody User requestJson(@RequestBody User user){
System.out.println(user);
//模拟服务端返回的数据
User responseUser=user;
return responseUser;
}
//第二个例子:请求数据的格式为Object,返回数据的格式为JSON
@RequestMapping("requestObject")
public @ResponseBody User requestObject(User user){
System.out.println(user);
//模拟服务端返回的数据
User responseUser=user;
return responseUser;
}
}
在第一个例子中:利用@RequestBody注解将页面传递过来的参数转换为User类对象user;@ResponseBody注解会将方法返回的User类对象responseUser转换为JSON数据返回至浏览器
在第二个例子中:@ResponseBody注解会将方法返回的User类对象responseUser转换为JSON数据返回至浏览器
SpringMVC札集(07)——JSON数据的更多相关文章
- springmvc学习笔记(18)-json数据交互
springmvc学习笔记(18)-json数据交互 标签: springmvc springmvc学习笔记18-json数据交互 springmvc进行json交互 环境准备 加入json转换的依赖 ...
- 【SpringMVC学习09】SpringMVC与前台的json数据交互
json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析也比较方便,所以使用很普遍.在springmvc中,也支持对json数据的解析和转换,这篇文章主要总结一下springm ...
- (转)SpringMVC学习(十)——SpringMVC与前台的json数据交互
http://blog.csdn.net/yerenyuan_pku/article/details/72514022 json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析 ...
- ajax传递json数据,springmvc后台就收json数据
1.ajax数据的封装 var json = {"token":token};//封装json数据 $.ajax({ url:'', data:JSON.stringify(jso ...
- springmvc 使用jq传递json数据时出现415错误
出现415错误是因为解析json时出现了错误,通过排查几点就能解决. 样例: <script> function requestByJson() { var datatest = {&qu ...
- SpringMVC札集(10)——SSM框架整合
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(08)——文件上传
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(05)——SpringMVC参数回显
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
- SpringMVC札集(01)——SpringMVC入门完整详细示例(上)
自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...
随机推荐
- Activiti工作流与spring集成
一.前言 前面Activiti工作流的学习,说明了Activiti的基本应用,在我们开发中可以根据实际的业务参考Activiti的API去更好的理解以及巩固.我们实际的开发中我们基本上都使用sprin ...
- (转)C#自制Web 服务器开发:用C#开发自己的Web服务器
当输入:127.0.0.1:5050 GET / HTTP/1.1 Accept: text/html, application/xhtml+xml, */* Accept-Language: zh- ...
- ubuntu下apt-get的配置文件是哪个
答:在/etc/apt/apt.conf 这个配置文件里可以指定使用代理,如: Acquire::https::proxy "http://myproxy.com:8080/";
- Commons Configuration之一简介
转载自(https://my.oschina.net/u/2000201/blog/486327) 1 简介 Commons Configuration软件类库提供通用配置接口,使Java应用程 ...
- com.mysql.jdbc.MysqlDataTruncation:Data Truncation:Data too long for column '字段name' at row 1
1.问题描述: 在mysql插入数据的时候报错:Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long fo ...
- 【bzoj1369】[Baltic2003]Gem(树形dp+结论)
题目传送门:bzoj1369 这题其实有个结论:节点数为n的树,对其染色使相邻节点颜色不同,且总颜色权值最小,所需的颜色数量是$ O(\log n) $的. 所以我们就可以愉快的dp了:$ f[i][ ...
- Xampp mysql启动
因为最近项目要用到php,需要集成Xampp环境,但是并没有接触过php,从官网下载了Xampp后,基本上就是傻瓜式安装了, 完成安装界面如下: 点击Apache的start可以正常启动,点击MYSQ ...
- db2 函数、存储过程示例
1.函数 --drop function getMaxDate; create FUNCTION getMaxDate (y int, m int ) returns date begin DECLA ...
- base64 原理
Base64编码之所以称为Base64,是因为其使用64个字符来对任意数据进行编码,同理有Base32.Base16编码.标准Base64编码使用的64个字符为: 这64个字符是各种字符编码(比如AS ...
- HtmlAgilityPach基本使用方法
//过滤html标签 static void InnerText() { HtmlWeb htmlWeb = new HtmlWeb(); HtmlDocument doc = htmlWeb.Loa ...