一、实验环境的搭建

1、Spring mvc jar。

导入spring mvc运行所需jar包。导入如下(有多余)

2、json的支持jar

3、加入jQuery。

选用jquery-3.0.0.min.js,放在WebRoot/JS文件夹

导入jQuery到jsp页面如下

4、web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<display-name>springmvcjson</display-name>

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:springmvc.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.action</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

<welcome-file>index.htm</welcome-file>

<welcome-file>index.jsp</welcome-file>

<welcome-file>default.html</welcome-file>

<welcome-file>default.htm</welcome-file>

<welcome-file>default.jsp</welcome-file>

</welcome-file-list>

</web-app>

5、springmvc.xml

classpath下

<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 ">

<!-- <bean name="/test01.action" class="com.xzw.json.controller.JsonTest"></bean> -->

<!-- View -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

<!-- 注解映射和适配器 -->

<mvc:annotation-driven ></mvc:annotation-driven>

<!-- 组件扫描 -->

<context:component-scan base-package="com.xzw.json.controller"></context:component-scan>

<!-- 使用@Autowired、@Required等注解

如不必设置<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>和

<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>等等

-->

<context:annotation-config />

</beans>

二、实验例子编写

1、请求和返回都是JSON

a).程序发起

index.jsp的一个按钮

b).js函数

function requestByJson() {

$.ajax({

type : 'post',

url : '${pageContext.request.contextPath}/jsonsource.action',

//设置contentType类型为json

contentType : 'application/json;charset=utf-8',

//json数据

data : '{"username":"reader001","password":"psw001"}',

//请求成功后的回调函数

success : function(data) {

alert(data.username);

}

});

}

c).Controller/Mapping

@RequestMapping("/jsonsource")

//@RequestBody 将json对象转成java对象

//@ResponseBody 表示返回的是json对象

public @ResponseBody User jsonSource(@RequestBody User user){

return user;

}

d).测试结果

查看浏览器的开发者工具信息

request

response

PS:User类有3个属性,id,username,password。

回调函数使用alert(data.username);

2、请求是key/value值,返回JSON

a).程序发起

b).js函数

//请求是key-value的值,返回的是json

function resquestByKV() {

$.ajax({

type : 'post',

url : '${pageContext.request.contextPath}/kvsource.action',

data : 'username=kvuser&password=kvpsw',

success : function(data) {

alert(data.username);

}

});

}

c).Controller/Mapping

参数没有@RequestBody。

d).测试结果

参考1。

三、提交表单数据,返回json结果。

1、实验准备和预测

设计两个form,将form1的数据提交后,做一定的处理后返回到form2。处理结果依据controller。

2、将数据作为json发出

a).JS函数

b).Controller

c).结果

实验过程,发现如果要传递json数据(java程序传出),由于json的字符串要在双引号中,要达到双引号的效果,引号较混乱。

3、将数据作为key/value的形式发出。

a).JS函数

b).Controller

c).结果

四、Spring mvc和ajax中文乱码问题

1、返回的java对象

如果是java对象作为json对象返回的话,不需要设置过滤器,spring的配置文件也没有设置字符编码,中文正常返回。估计是json的支持包或spring有编码的设置。

2、返回字符串

例子请求的是这个方法

a).设置filter字符编码

spring的字符过滤器org.springframework.web.filter.CharacterEncodingFilter

无效,是乱码。显示如下

b).解决方法一、@ResponseBody

加上produces。如produces="application/json; charset=utf-8"。

设置后没有乱码

c).解决方法二、mvc:annotation-driven

这个方法是针对所有。

在mvc:annotation-driven加上下面的StringHttpMessageConverter

主要是text/html;charset=UTF-8就可以,其他都不可以。避免乱码。

<mvc:annotation-driven>

<mvc:message-converters register-defaults="true">

<bean class="org.springframework.http.converter.StringHttpMessageConverter">

<property name="supportedMediaTypes">

<list>

<!--application/json和text/plain无法解决返回字符串的乱码  -->

<!-- <value>application/json;charset=UTF-8</value>

<value>text/plain;charset=UTF-8</value>

-->

<value>text/html;charset=UTF-8</value>

</list>

</property>

</bean>

</mvc:message-converters>

</mvc:annotation-driven>

发现:再去掉注解中的参数produces="application/json; charset=utf-8",然后测试。supportedMediaTypes加入text/html;charset=UTF-8能解决乱码。且java对象的json返回也没有出现乱码问题。

Spring mvc,jQuery和JSON数据交互的更多相关文章

  1. 1.4(Spring MVC学习笔记)JSON数据交互与RESTful支持

    一.JSON数据交互 1.1JSON简介 JSON(JavaScript Object Notation)是一种数据交换格式. 1.2JSON对象结构 {}代表一个对象,{}中写入数据信息,通常为ke ...

  2. Spring MVC 前后端 Json 方式交互和处理

    众所周知,在mvc中,数据是在各个层次之间进行流转是一个不争的事实. 而这种流转,也就会面临一些困境,这些困境,是由于数据在不同世界中的表现形式不同而造成的.   数据在页面上是一个扁平的,不带数据类 ...

  3. Spring MVC如何进行JSON数据的传输与接受

    本篇文章写给刚接触SpingMVC的同道中人,虽然笔者本身水平也不高,但聊胜于无吧,希望可以给某些人带来帮助 笔者同时再次说明,运行本例时,需注意一些配置文件和网页脚本的路径,因为笔者的文件路径与读者 ...

  4. Spring MVC中返回JSON数据的几种方式

    我们都知道Spring MVC 的Controller方法中默认可以返回ModeAndView 和String 类型,返回的这两种类型数据是被DispatcherServlet拿来给到视图解析器进行继 ...

  5. spring MVC之返回JSON数据(Spring3.0 MVC)

    方式一:使用ModelAndView的contentType是"application/json" 方式二:返回String的            contentType是&qu ...

  6. Spring MVC中传递json数据时显示415错误解决方法

    在ajax中设置 ContentType为'application/json;charset=utf-8' 传递的data类型必须是json字符串类型:{“key”:"value" ...

  7. Spring MVC之JSON数据交互和RESTful的支持

    1.JSON概述 1.1 什么是JSON JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式.它是基于JavaScript的一个子集,使用了C.C ...

  8. Spring mvc 前后台通过json交互【转】

    原文转自:https://www.cnblogs.com/zhaojiankai/p/8184596.html 本节内容: @RequestBody @ResponseBody 请求json,响应js ...

  9. json数据交互

    springmvc 的json数据交互 - 哎幽的成长 - CSDN博客--和老师讲课相同http://blog.csdn.net/u012373815/article/details/4720818 ...

随机推荐

  1. The 2018 Nobel prizesThe Nobel prize for economics is awarded for work on the climate and economic growth

    The 2018 Nobel prizesThe Nobel prize for economics is awarded for work on the climate and economic g ...

  2. 迷你MVVM框架 avalonjs 1.3.8发布

    avalon1.3.8主要是在ms-repeat. ms-each. ms-with等循环绑定上做重大性能优化,其次是对一些绑定了事件的指令添加了roolback,让其CG回收更顺畅. 重构ms-re ...

  3. 使用css技术代替传统的frame技术

    http://www.dynamicdrive.com/style/layouts/item/css-left-frame-layout/ <!--Force IE6 into quirks m ...

  4. bat cmd dos 通过拖拽参数 上传 截取拖拽上传文件名

    echo off setlocal enabledelayedexpansion :: L 小写 for /l %%i in (1,1,10000) do ( :con set /p a= selec ...

  5. HTTP Response Code 中文详解

      引自:https://blog.csdn.net/lplj717/article/details/70053560   1xx - 信息提示这些状态代码表示临时的响应.客户端在收到常规响应之前,应 ...

  6. 利用R求分位数及画出箱型图

    1)数据集 data<-c(75.0,64.0,47.4,66.9,62.2,62.2,58.7,63.5,66.6,64.0,57.0,69.0,56.9,50.0,72.0) 默认是四分位: ...

  7. rabbitmq web管理界面 用户管理

    安装最新版本的rabbitmq(3.3.1),并启用management plugin后,使用默认的账号guest登陆管理控制台,却提示登陆失败. 翻看官方的release文档后,得知由于账号gues ...

  8. SSH框架整合的其它方式

    --------------------siwuxie095 SSH 框架整合的其它方式 1.主要是整合 Spring 框架和 Hibernate 框架时,可以不写 Hibernate 核心配置文件: ...

  9. sqlite小知识

    删除数据时,由于缓存关系,数据了文件大小不会一下子减小,可以通过执行vacuum;或新建表时使用自动整理大小来实现. sqlite的大小理论上可以达到140T. 暂时,使用C的api,只能使用不是.开 ...

  10. IIS 访问不了,IIS有问题,IIS右击浏览没反应

    查看是否是下面的问题 web服务扩展中,下面三项设置为 “允许”  CGI扩展  ISAPI扩展  Active Server Pages