@responseBody对象传输

来源:互联网 发布:平板电脑监控软件 编辑:程序博客网 时间:2024/05/22 12:46

    遇到这个问题也是一波三折,按网上提供的资料部署,对象一直都传不到前台。找不到"着力点".很是纠结,一直以为是jackson加的包的问题(版本或者其他)。

    由于框架配置的xml里加载的库是我手动加的,所以也一时没想到库加得够不够。后来经调试查出就是这个问题原因。

    @这是之前的sping-mvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    @ 后来sping-mvc.xml改成如下:(成功)

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


其他必要配置:

@js请求:

$.ajax({type: "get",url: "category/model_select.json",dataType:"json",success: function(json){ alert(json.username);},error: function(){}});

@后台处理:

<span style="white-space:pre"></span>@RequestMapping("/model_select.json")@ResponseBodypublic  User modelSelect(HttpServletRequest request){            User user=new User();    user.setId(1);    user.setUsername("cai");    user.setPassword("dimin");    return user ;}

@需要的jar包:

jackson-core-asl-1.9.10.jar
jackson-mapper-asl-1.9.10.jar


(非必要jar包和配置)

jackson-core-lgpl-1.9.6.jar

jackson-mapper-lgpl-1.9.6.jar

<!-- 配置国际化资源文件路径 --><bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basename"><!-- 定义消息资源文件的相对路径 --><value>messages/message</value></property></bean><!-- 基于Cookie的本地化解析器 --> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">   <property name="cookieMaxAge" value="604800"/>   <property name="defaultLocale" value="zh_CN"/>   <property name="cookieName" value="Language"></property> </bean>




0 0
原创粉丝点击