org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
来源:互联网 发布:苹果8没网络 编辑:程序博客网 时间:2024/06/08 15:09
Ajax响应中文乱码 [SpringMVC使用@ResponseBody处理Ajax请求]
Spring3.0 MVC @ResponseBody 的作用是把返回值直接写到HTTP response body里。
Spring使用AnnotationMethodHandlerA有人跟踪@ResponseBody 的实现类发现其默认的编码是 iso-8859-1,
解决办法,在spring mvc的配置文件中手工配置bean:
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerA
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConvert
<property name = "supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
这样通过配置AnnotationMethodHandlerA
记住,需要把bean部分加入到<context:component-scan base-package="com.zlscw.mvc" />前面,
这样就可以在jquery中直接调用而不出现乱码了。
-------------------------------------------这篇文章说的很到位
近日用Spring3的MVC写东西,深感其之于Webwork/Struts2的便利,但是在通过@ResponseBody这个annotation输出一个json字符串的时候,发现页面上获得的json字符串中文字符出现了乱码的现象。通过firefox观察返回的字符串,中文部分全部变成了???????的形式,初步判定是返回时,spring处理@ResponseBody使用了错误的编码。
因为我在web.xml中已经配置了Spring的CharacterEncodingFilter,并且强制将request和response的编码都指定为utf-8,所以出现乱码的原因肯定是在Spring内部某处的逻辑了。
把log4j中关于spring的输出级别调为debug,通过访问出问题的地址,发现Spring在处理@ResponseBody这个annotation的时候,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerA
不看不要紧,一看吓一跳,里面竟然是这样定义其默认编码的:
1publicstaticfinalCharset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
顿时心生N种不爽:堂堂Spring,竟然还在其中用西欧字符集作为其默认编码,坑爹啊!(很多spring的类中,涉及编码的已经都是utf-8了,比如负责JSON视图的MappingJacksonHttpMessag
By default, this returns the first element of the supportedMediaTypes property, if any. Can be overridden in subclasses.
心想这下就简单了,你的DEFAULT_CHARSET不是final么?那我自己继承一个出来,按照我的需求定义为utf-8不就得了?代码如下:
01publicclassUTF8StringHttpMessageCon
然后,在spring的配置文件中添加如下bean声明,用自己写的类替换掉原有的StringHttpMessageConvert
1"org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerA
再看通过@ResponseBody返回的json字符串,终于中文都可以正常显示了。
-------------------------------------------下面有一些解释
但我们一般会在标注@ResponseBody的方法上返回String或byte[]类型的结果,期望的"Content-Type"的值应为"text/plain"或"application/octet-stream"。
这样导致了浏览器不能正确处理返回的内容。
实际上Spring在用HttpMessageConverter处理的过程中首先会判断response header中有没有写入"Content-Type",如果没有写入的话才会使用request header中"Accept"的第一个值。
但是由于Spring对HttpServletResponse进行了封装,实际上使用的是ServletServerHttpRespons
判断response header的过程中使用的是ServletServerHttpRespons
所以我们虽然可以在Controller的方法中加入对HttpServletResponse的引用,然后设置"Content-Type"的值,但是并不会起作用。
来处理@ResponseBody,该类再使用一些HttpMessageConverter来具体处理信息。
Chrome生成的值为application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,**
所以最后写入response中"Content-Type"的值为"application/xml"或"application/x-ms-application"。
-------------------------------------------------其实这个注解完全可以不用,直接使用response往输出流里面写。
使用jQuery ajax调用的返回json,中文乱码问题
Jquery :
$.ajax({
url: '/test/testAction.do?method=test',
type: 'POST',
dataType: 'json',
timeout: 5000,
async: false,
error: function(){
alert('获取数据失败!');
},
success: function(json){
jsObject = eval_r(json);
}
});
return jsObject;
JSONArray json = JSONArray.fromObject(SysList);//SysList是一个List
// 设置response的ContentType解决中文乱码
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print(json.toString());
return null;
- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
- 'org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0'
- 废弃的org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
- maven springmvc Error creating bean with name 'org.springframework.web.servlet.mvc.annotation
- org.springframework.web.servlet.mvc.support.RedirectAttributes
- spring mvc中org.springframework.web.servlet.DispatcherServlet类
- spring mvc中org.springframework.web.servlet.DispatcherServlet类
- org.springframework.web.servlet.DispatcherServlet
- org.springframework.web.servlet.DispatcherServlet
- org.springframework.web.servlet.ModelAndView
- org.springframework.web.servlet.DispatcherServlet
- [org.springframework.web.servlet.PageNotFound]
- org.springframework.web.servlet.DispatcherServlet
- [org.springframework.web.servlet.PageNotFound]
- SpringMVC注解-org.springframework.web.bind.annotation
- org.springframework.web.bind.annotation 注解详解
- org.springframework.web.bind.annotation.RequestMapping
- spring mvc 源代码笔记1--org.springframework.web.servlet.mvc.method
- web安全色
- Babelfish POJ
- 单变量线性回归中的梯度下降法求解代价函数的最小值
- Linux下安装Mongodb
- LeetCode
- org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
- Oracle 12c 使用普通用户的步骤
- PAT甲级 1037 -- 没有注释
- 【CDOJ1059】秋实大哥与小朋友
- MYSQL当权限更改生效时
- Android -- Service官方文档简译
- QT 为按钮添加事件
- PAT甲级 1038 -- 没有注释
- Ubuntu下配置Maven