自定义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. Oh My Fish! 让你的 Shell 漂亮起来

    安装 Oh My Fish 安装 omf 很简单.你要做的只是在你的 Fish shell 中运行下面的命令. curl -L https://get.oh-my.fish | fish 一旦安装完成 ...

  2. 转:java 重定向和转发的区别

    response.sendredirect("http://www.foo.com/path/error.html"); 重定向和转发有一个重要的不同:当使用转发时,JSP容器将使 ...

  3. Spark(一)介绍

    随着对spark的业务更深入,对spark的了解也越多,然而目前还处于知道的越多,不知道的更多阶段,当然这也是成长最快的阶段.这篇文章用作总结最近收集及理解的spark相关概念及其关系. 名词 dri ...

  4. ubuntu安装python MySQLdb模块

    本文讲述了python安装mysql-python的方法.分享给大家供大家参考,具体如下: ubuntu 系统下进行的操作 首先安装了pip工具 ? 1 sudo apt-get install py ...

  5. c# 调用webapi 传参 特殊字符的问题

    最近在做对接数据接口,遇到一些问题,在C#后台写请求webapi的接口,但是传递过程中参数如果有特殊字符,传入过去之后又问题. 需要转换一下,通过System.Web.HttpUtility.UrlE ...

  6. 【python】argparse学习(转)

    点击这里成为作者 · 更新于 2018-11-14 21:00:36 argparse argparse 是 Python 内置的一个用于命令项选项与参数解析的模块,通过在程序中定义好我们需要的参数, ...

  7. C#(Wpf)实现小键盘

    花了一天时间小键盘基本功能已完成,先看看效果图吧! 默认: Shift: Caps Lock: Button style <Style x:Key="KeyButton" T ...

  8. Python学习札记(一) 初始python

    参考: 廖雪峰教程:Python简介 笔记 1.C语言是可以用来编写操作系统的贴近硬件的语言,所以,C语言适合开发那些追求运行速度.充分发挥硬件性能的程序.而Python是用来编写应用程序的高级编程语 ...

  9. UVa 11149 矩阵的幂(矩阵倍增法模板题)

    https://vjudge.net/problem/UVA-11149 题意: 输入一个n×n矩阵A,计算A+A^2+A^3+...A^k的值. 思路: 矩阵倍增法. 处理方法如下,一直化简下去直到 ...

  10. LA 5135 井下矿工(点—双连通分量模板题)

    https://vjudge.net/problem/UVALive-5135 题意:在一个无向图上选择尽量少的点涂黑,使得任意删除一个点后,每个连通分量至少有一个黑点. 思路: 首先dfs遍历求出割 ...