Spring JSON 数据交互

1.对象结构

  对象结构以“{”开始,以“}”结束

2.数组结构

  数据结构以“[”开始,以“]”结束


3.Spring提供了一个 HttpMessageConverter<T>接口来完成JSON数据转换工作

  Spring为HttpMessageConverter<T> 接口提供了很多实现类,其中 MappingJackson2HttpMessageConverter是SpringMVC默认处理的JSON格式请求响应的实现类。该类可以利用Jackson开源包读写JSON数据,将Java对象转换为JSON对象和XML文档,同时也可以将JSON对象和XML文档转换成Java对象。


4.需要使用的Jackson开源包

   http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.8.8

 


5.导入JAR包

6.在web.xml文件中对SpringMVC的前端控制器等信息进行配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SpringJSON</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
        <!-- 配置前端控制器 -->
        <servlet-name>SpringJSON</servlet-name>
        <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
         </servlet-class>
        <!-- 初始化时加载配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:Spring-config.xml</param-value>
        </init-param>
         <!-- 表示容器在启动时立即加载servlet -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringJSON</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>


7.配置SpringMVC的核心文件 Springmvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 定义组件扫描器,指定需要扫描的包 -->
    <context:component-scan base-package="com.kangxg.controller"/>

    <!-- 配置注释驱动 -->
    <mvc:annotation-driven />
    <!-- 配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->

    <mvc:resources location="/" mapping="/**"/>
    <!-- 配置视图解析器 -->
    <bean  class ="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <!-- 设置前缀 -->
       <property name = "prefix"  value="/WEB-INF/jsp/"/>
       <!-- 设置后缀 -->
       <property name = "suffix"  value=".jsp"/>
    </bean>
</beans>

8.在WebContent目录下,创建页面文件index.jsp来测试JSON数据交互

 

<%@ 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>测试JSON交互e</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/jquery-1.9.1.min.js"></script>
<script type="text/javascript"  >
  function testJson()
  {
     var username = $("#username").val();
     var password = $("#password").val();
      $.ajax({
          url:"${pageContext.request.contextPath}/testJson",
          type:"post",
          data:JSON.stringify({username:username,password:password}),
          contentType:"application/json;charset=UTF-8",
          dataType:"JSON",
          success:function(data){
               if(data !=null){
                 alert("您输入的用户名为:"+data.username+"密码为:"+data.password);
               }
           }
         });  

  }

</script>
 
</head>
<body>
  <form >
    用户名:<input type ="text" name ="username" id = "username"><br/>
    密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:
    <input type = "password" name="password" id ="password"><br/>
    <input type ="button" value ="JSON交互" onclick ="testJson()"/>
 
  </form>
 
</body>
</html>

下载js文件地址

http://www.veryhuo.com/down/html/115961.html


9.debug




在 Springmvc-config.xml 文件中

<mvc:annotation-driven />

<mvc:resources location="/" mapping="/**"/>

可替换为

 
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    
  
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
          <property name = "messageConverters"  >
             <list>
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
             </list>
          </property>
    </bean>
 
    <mvc:default-servlet-handler/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值