- 浏览: 442832 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (108)
- Ibatis (0)
- ZK (13)
- 设计模式 (3)
- Java编程 (21)
- 正则表达式 (2)
- 开发小续 (2)
- 杂谈 (1)
- 数据库 (6)
- Hibernate (6)
- Struts 2 (2)
- Spring (2)
- 版本控制 (4)
- Subversion (1)
- Myeclipse (1)
- EL (1)
- jsp (1)
- CAS (1)
- SSO (1)
- Restful Web Service (3)
- jersey (2)
- jersey与spring (1)
- web service框架 (1)
- 系统分析 (1)
- Subversion windows service (1)
- tomcat (1)
- PermGen Space (1)
- 编程心得 (1)
- google map api (1)
- springmvc (1)
- ajax (1)
- Hibernate QBC (1)
- oracle oracle10g (1)
- RBAC 权限设计 (1)
- springsecurity 3 (1)
- JPA (1)
- JPA j2ee (1)
- maven java (1)
- Jquery框架 UI框架 (1)
- printcss (1)
- java reflect 反射 (1)
- Jquery框架 UI框架 3scschool (1)
- collabnet Subversion (1)
- linux redhat (1)
- mysql (3)
- spring spring-data-jpa (1)
- Jquery AjaxForm (1)
- Jquery Map (1)
- linux (3)
- linux nginx openssl zlib pcre (1)
- linux jdk (1)
- 物联网技术 嵌入式 C# (1)
- oracle 数据库 (1)
- oracle (5)
- maven (1)
- java 定时器 (1)
- swing (1)
- java (2)
- 短信接口 (1)
- DBUtils (1)
- JDBC (1)
- javascript json (1)
- shiro (1)
- 权限系统 (1)
- 嵌入式 (1)
- 树莓派 (1)
- python (1)
- Github (1)
- 代码托管 (1)
- 产品设计 (2)
- 产品需求 (2)
- BRD (1)
- MRD (1)
- PRD (1)
- DRD (1)
- FRD (1)
- GIS (1)
- SOA (1)
- 网站资源 (1)
- 骆驼 (1)
- 黑客技术资源 (1)
- 网络安全资源 (1)
- 个人修炼 (1)
- Angular2 (1)
- Nodejs Krakenjs (1)
最新评论
-
GeorgeFeng:
学习啦,感谢
RBAC用户权限管理数据库设计 -
徜徉の小溪:
yuanliangding 写道请求进来时,创建的对象都不是由 ...
基于RESTful Web Service 的Jersey框架与Spring完美整合 -
yuanliangding:
请求进来时,创建的对象都不是由Spring弄出来的。。
基于RESTful Web Service 的Jersey框架与Spring完美整合 -
Ripin_Yan:
我已经按照你的xml这个顺序配置了,结果还是中文乱码。是还有什 ...
SpringMVC使用注解@ResponseBody返回json中文乱码 -
王静娜:
作者的实体图画的很好很有条理,但是个人觉得用户组对应的是资源, ...
RBAC用户权限管理数据库设计
SpringMVC使用注解@ResponseBody返回json中文乱码
---------------------------------------
在这里我不得支持一下SpringMVC是一个不错的框架,用了springmvc这么长时间 我犯了个错误,说来惭愧.
在项目中编码格式是UTF-8 在使用Ajax 请求 在SpringMVC中返回Json 数据 我用到了@ResponseBody 注解 将表示该方法的返回结果直接写入HTTP response body中。
然后再数据库中取出的中文通过Spring的response返回结果中文乱码.
方法一
原因:
response在响应的时候 生成的response中"Content-Type"的值不正确。(默认的是ISO-8859-1)
然后使用Spring使用AnnotationMethodHandlerAdapter来处理@ResponseBody,该类再使用一些HttpMessageConverter来具体处理信息。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property name="messageConverters"> <list> <bean class = "org.springframework.http.converter.StringHttpMessageConverter"> <property name = "supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <context:component-scan base-package="com.zdkj.limp.controller"></context:component-scan> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp"></property> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </beans>
以上是我就纠正回来的正确代码 以免误导大家 我贴出了正确的代码
下面那段是错误的代码
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.zdkj.limp.controller"></context:component-scan> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/" /> <property name="suffix" value=".jsp"></property> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > <property name="messageConverters"> <list> <bean class = "org.springframework.http.converter.StringHttpMessageConverter"> <property name = "supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> </beans>
大家看看有什么区别
我找了半天都没找出原因 后来发现顺序不对
程序执行的时候先注解扫描包 没发现 org.springframework.http.converter.StringHttpMessageConverter
最后才执行org.springframework.http.converter.StringHttpMessageConverter当执行的时就不再去扫描了
所以最终结果是顺序放错了
----------------------------------------------------------------------------
附加的一个案例如果大家再碰到这种乱码问题 没办法 使用以下代码
方法 二
使用java中的urlEncoding 对字符进行编码 然后再到客户端js中进行urlDecoding进行解码 也可以解决乱码问题 但我不建议使用这种方法 治标不治本
服务器端 java代码如下:
// URL编码 protected String urlEncode(String src) { try { return URLEncoder.encode(src, "UTF-8"); } catch (Exception ex) { return src; } }
客户端 js中 代码如下:
<script type="text/javascript">
if (!misc) { var misc = {}; } if (!misc.utils) { misc.utils = {}; } misc.utils.urlencode2Utf8 = function(ptr) { for ( var x in ptr) { if (typeof (ptr[x]) == 'object') { misc.utils.urlencode2Utf8(ptr[x]); } else if (typeof (ptr[x]) == 'string') { try { ptr[x] = decodeURIComponent(ptr[x]); } catch (e) { } } } }; </script>
发表评论
-
Bean在Spring容器中的生命周期
2013-12-31 16:01 1533Bean在Spring容器中的生命周期 ... -
开源项目 企业框架,学习资料
2013-05-13 16:10 1534Spring mvc+hibernate+freemar ... -
maven 通用配置
2013-04-27 10:02 2979在搭建maven 项目的时候,我们很 ... -
CAS 单点登录client,server端分析
2012-04-09 13:14 12678本篇文章是对JA-SIG CAS(v3.3)的初步调研总 ... -
EL表达式 functions String处理函数
2012-03-27 14:56 421101.uri="http://java.sun.co ... -
Hibernate hbm.xml中查询数据作为虚拟列Formula使用
2011-12-26 17:09 6203Hibernate 中的Formula 攻 ... -
Dom4jUtils 工具类让你更加方便操作XML的MarkerIce.Jar
2011-09-29 11:24 1609最近我闲着无聊,看到一篇文章关于XMl的操作.对于XMl操作相 ... -
getHibernateTemplate.load() 和get()之间的区别
2011-09-08 15:19 8704今天我在hibernate 进行 getHibernateTe ... -
排序算法java版,速度排行:冒泡排序、简单选择排序、直接插入排序、折半插入排序、希尔排序、堆排序、归并排序、快速排序
2011-05-24 09:16 1583先推荐一篇关于排序算法的文章:http://www.cpp ... -
struts2整合JSON,JQuery 实现ajax功能
2011-05-17 10:23 24084月 我使用到了 struts2 整合JQuer ... -
在spring来管理实例对象prototype和singleton的选择.针对action如何使用
2011-04-22 15:54 1335默认情况下,从bean工厂所取得的实例为Singleton(b ... -
Apache Commons工具集简介
2011-03-22 14:33 1243Apache Commons包含了很多开源的工具,用于解 ... -
java使用定时器,定时发送邮件
2011-02-22 16:01 1659java使用定时器,定时发送邮件 ... -
JSP 分页标签库
2010-12-31 16:11 3000好了,该说一下怎么用 ... -
struts2类型转换机制
2010-12-31 14:17 1617struts2类型转换机制 package com. ... -
java中web.xml的配置信息
2010-11-18 09:49 3715<?xml version="1.0&q ... -
forward,include,redirect的区别(转)
2010-08-27 13:54 1312forward,include,redirect是 ... -
一个Java程序员应该掌握的10项技能[转]
2010-06-12 10:53 12531、语法:必须比较 ... -
(转)Java开发者的十大戒律
2010-06-09 17:27 1157有许多标准和实 ... -
Java 线程同步示例
2010-06-08 23:22 1365文章用实例代码展示了Java中多线程访问共享资源 时线程同 ...
相关推荐
--处理 @ResponseBody 中文乱码问题 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <!-- Support...
主要介绍了SpringMVC中解决@ResponseBody注解返回中文乱码问题, 小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Springmvc 4.x利用@ResponseBody返回Json数据的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
SpringMVC使用@ResponseBody.rar
Spring MVC下彻底解决@ResponseBody转义html特殊字符问题,解决包括尖角号(),单引号('),双引号("),按位与(&),反斜杠(\)。
本篇文章主要介绍了springMvc注解之@ResponseBody和@RequestBody详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
NULL 博文链接:https://bijian1013.iteye.com/blog/2310240
NULL 博文链接:https://bijian1013.iteye.com/blog/2310236
springmvc-demo08-返回JSON数据.zip
springMVC返回json数据需要的两个架包,加入springMVC使用@ResponseBody才不会报错。
springmvc+国际化i18N+springmvc验证+jetbrick-template使用+@responsebody+谷歌guava: 1)围绕springmvc做的国际化 2)围绕springmvc做的验证 3)使用的jetbrick-template模板引擎 ……
如果传输的是多层嵌套json对象,这个时候会就会出现数据丢失问题 @ResponseBody很好的解决了这个问题,它会把前台传输过来的json转化为后台对应的对象
1)spring MVC 中@ResponseBody需要的所有JAR包 2)性能还不错的模板引擎jetbrick-template-2.x 2.x的所需的所有jar包 3)日志jar包:slf4j和logback 所有 4)阿里 druid 连接池jar包 5)mysql数据库链接驱动jar包 6...
主要介绍了SpringMVC restful 注解之@RequestBody进行json与object转换的相关资料,需要的朋友可以参考下
NULL 博文链接:https://coocaobo.iteye.com/blog/2410629
springmvc常用注解标签详解,总结,常用注解,标签详解,springmvc
springmvc进行Jason数据封装转化时可以即使用也是@responsebody对数据进行封装的时候需要的工具类
本篇文章主要介绍了详解SpringMVC注解@initbinder解决类型转换问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
springMVC使用注解方式进行页面跳转