您的位置:首页 > 编程语言 > Java开发

SpringMVC4.0以后版本返回json格式数据问题

2016-08-27 15:13 411 查看
1.创建创建web项目,加入SpringMVC的jar,我这里演示用spring-framework-4.2.3.RELEASE。jar包如下图所示:

2.配置web.xml如下:

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

<web-app version="2.5"

    xmlns="http://java.sun.com/xml/ns/javaee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    

    <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:applicationContext.xml</param-value>

        </init-param>

    </servlet>

    <servlet-mapping>

        <servlet-name>SpringMVC</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

</web-app>

3.配置applicationContext.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:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">   

      <context:component-scan base-package="cn.xugang.."></context:component-scan>

      <mvc:annotation-driven content-negotiation-manager="contentNegotiationManagerFactoryBean"/>

      <mvc:default-servlet-handler/>

      <bean class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean" id="contentNegotiationManagerFactoryBean">

              <property name="favorPathExtension" value="false"/>

              <property name="favorParameter" value="false"/>

              <property name="ignoreAcceptHeader" value="false"/>

              <property name="mediaTypes">

                  <map>  

                    <entry key="xml" value="application/xml"/>  

                    <entry key="json" value="application/json"/>  

                    <entry key="xls" value="application/vnd.ms-excel"/>  

                </map>

              </property>

      </bean>

      <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" id="mappingJacksonHttpMessageConverter">

              <property name="supportedMediaTypes">

                  <list>

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

                  </list>

              </property>

      </bean>

      <bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

              <property name="messageConverters">

                  <list>

                      <ref bean="mappingJacksonHttpMessageConverter"/>

                  </list>

              </property>

      </bean>

      

      

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

              <property name="prefix" value="/WEB-INF/jsp/"></property>

              <property name="suffix" value=".jsp"></property>

      </bean>

</beans>

4.配置创建controller,使用@Responsebody注解,指定返回内容。

package cn.xugang.cotroller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import cn.xugang.entity.User;

@Controller

@RequestMapping("/user")

public class UserCotroller {

    @RequestMapping(value="/hello.html",method=RequestMethod.GET )

    public @ResponseBody User hello(){

        User user=new User();

        user.setAge(19);

        user.setName("test");

        return user;

    }

    @RequestMapping(value="/test.html")

    public String test(){

        System.out.println("hello world");

        return "hello";

    }

}

5.发送异步请求。这个由于本人用了Etx.js,就直接用ext.js发送异步请求了。

Ext.onReady(function(){

    Ext.define('User',{

        extend:'Ext.data.Model',

        fields:['name','age'],

    });

    var store=Ext.create('Ext.data.Store',{

        model:'User',

        proxy:{

            type:'ajax',

            url:'/SpringMVC4/user/hello.html',

            reader:{

                type:'json',

                

            }

        }

        

    });

    

    store.load({

        callback:function(records,operation,success){

            if(success){

                var msg=[];

                store.each(function(user){

                    msg.push(user.get('name')+" "+user.get('age'));

                    

                });

                Ext.MessageBox.alert("提示",msg.join("<br>"));

            }

        }

        

        

    });

    var msg=[];

    store.each(function(user){

        msg.push(person.get('name')+" "+user.get('age'));

        

    });

    Ext.MessageBox.alert('提示',msg.join("<br>"));

    

});

6.结果演示

7.总结:最后简单说一下,SpringMVC4.0以上版本用上述这种方式成功返回json,如果是4.0以下,由于json的解析方式不同,这里也是一个坑,在网上收了很多资料,看了说得是云里雾里,完全无法使用,最后经过自己认真总结得出本文所述内容,4.0以下可使用如下jar包。

并于applicationContext.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:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd

        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1
4000
.xsd">   

      <context:component-scan base-package="cn.xugang.."></context:component-scan>

      <mvc:annotation-driven/>

      <mvc:default-servlet-handler/>

      <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" id="mappingJacksonHttpMessageConverter">

              <property name="supportedMediaTypes">

                  <list>

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

                  </list>

              </property>

      </bean>

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

              <property name="prefix" value="/WEB-INF/jsp/"></property>

              <property name="suffix" value=".jsp"></property>

      </bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: