自定义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数据的更多相关文章

  1. springmvc学习笔记(18)-json数据交互

    springmvc学习笔记(18)-json数据交互 标签: springmvc springmvc学习笔记18-json数据交互 springmvc进行json交互 环境准备 加入json转换的依赖 ...

  2. 【SpringMVC学习09】SpringMVC与前台的json数据交互

    json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析也比较方便,所以使用很普遍.在springmvc中,也支持对json数据的解析和转换,这篇文章主要总结一下springm ...

  3. (转)SpringMVC学习(十)——SpringMVC与前台的json数据交互

    http://blog.csdn.net/yerenyuan_pku/article/details/72514022 json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析 ...

  4. ajax传递json数据,springmvc后台就收json数据

    1.ajax数据的封装 var json = {"token":token};//封装json数据 $.ajax({ url:'', data:JSON.stringify(jso ...

  5. springmvc 使用jq传递json数据时出现415错误

    出现415错误是因为解析json时出现了错误,通过排查几点就能解决. 样例: <script> function requestByJson() { var datatest = {&qu ...

  6. SpringMVC札集(10)——SSM框架整合

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  7. SpringMVC札集(08)——文件上传

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  8. SpringMVC札集(05)——SpringMVC参数回显

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

  9. SpringMVC札集(01)——SpringMVC入门完整详细示例(上)

    自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onL ...

随机推荐

  1. 20145216史婧瑶《Java程序设计》第2周学习总结

    20145216 <Java程序设计>第2周学习总结 教材学习内容总结 第三章 基础语法 3.1 类型.变量与运算符 •类型 •基本类型 •整数:short(占2字节).int(占4字节) ...

  2. Apache-solr

    1.1. 下载 从Solr官方网站(http://lucene.apache.org/solr/ )下载Solr4.10.3,根据Solr的运行环境,Linux下需要下载lucene-4.10.3.t ...

  3. 学Git,用Git ③

    不知道我前面是否将git讲清楚了,这里再稍微总结一下git的一个重要功能用法,同时增加两个很实用的git使用技巧. 1.git"读档"与git"回退" 我发现我 ...

  4. Hive架构

    Hive组织数据包含四种层次:DataBase --> Table --> Partition --> Bucket,对应在HDFS上都是文件夹形式. 数据库和数据仓库的区别: 1) ...

  5. CSS控制滚动条的样式

    到今天(2018年10月25日)为止, 这还是chrome上的一个实验性特性: ::-webkit-scrollbar{width:4px;height:4px;} ::-webkit-scrollb ...

  6. composer安装教程 windows系统 | Linux系统 | mac系统

    如何安装 Composer 下载 Composer 安装前请务必确保已经正确安装了 PHP.打开命令行窗口并执行 php -v 查看是否正确输出版本号. 打开命令行并依次执行下列命令安装最新版本的 C ...

  7. LeetCode——Integer Break

    Question Given a positive integer n, break it into the sum of at least two positive integers and max ...

  8. 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛-A banana·

    2017-09-09 16:41:28 writer:pprp 题意很好理解就不说了,实现比较清晰,选择邻接表来做 但是我用的是链表来实现的,所以导致出现了很多问题,最后卡的最长时间的一个问题是 应该 ...

  9. 从U盘安装linux(前人踩坑后人乘凉)

    今天踩了一个大坑,网上的教程从u盘安装linux少了一个关键步骤导致我挣扎了两个小时 废话不多说,开始需要准备一些东西 1.从官网下载一个Ubuntu 10.04的镜像 2.一个大于等于1G的支持启动 ...

  10. HDU 1827 Summer Holiday

    http://acm.hdu.edu.cn/showproblem.php?pid=1827 题意: 听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家 ...