- @RequestMapping(value="/getForm")
- @ResponseBody
- publicStringgetForm(Stringpid){
- return"你好";
- }
返回结果“你好”
- @RequestMapping(value="/getForm")
- @ResponseBody
- publicList<String>getForm(Stringpid){
- returnnewArrayList<String>(){{
- add("你好")
- }};
- }
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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"xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
- <context:component-scanbase-package="cn.netcluster.workflow.**"/>
- <mvc:annotation-driven/>
- <mvc:default-servlet-handler/>
- <bean
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
- id="multipartResolver">
- <propertyname="defaultEncoding"value="UTF-8"/>
- <propertyname="uploadTempDir"value="resources/attach/temp"/>
- </bean>
- </beans>
使用的是Spring3.1
问题补充:
xiaoZ5919 写道
你遇到的是第一个方法直接返回String会乱码,而返回list的那个不会乱码是吧?
这可以说是spring mvc的一个bug,spring MVC有一系列HttpMessageConverter去处理用@ResponseBody注解的返回值,如返回list则使用MappingJacksonHttpMessageConverter,返回string,则使用StringHttpMessageConverter,这个convert使用的是字符集是iso-8859-1,而且是final的
解决的办法:
你自己重写一个StringHttpMessageConverter,使用你想要的字符集,并且使这个属性可注入
另外一种直接放弃String,而是使用对象
这可以说是spring mvc的一个bug,spring MVC有一系列HttpMessageConverter去处理用@ResponseBody注解的返回值,如返回list则使用MappingJacksonHttpMessageConverter,返回string,则使用StringHttpMessageConverter,这个convert使用的是字符集是iso-8859-1,而且是final的
- publicstaticfinalCharsetDEFAULT_CHARSET=Charset.forName("ISO-8859-1");
解决的办法:
你自己重写一个StringHttpMessageConverter,使用你想要的字符集,并且使这个属性可注入
- <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <propertyname="messageConverters">
- <util:list>
- <beanclass="com.pcds.ecomm.website.syscustomization.ConfigurableStringHttpMessageConverter">
- <constructor-argvalue="UTF-8"/>
- </bean>
- <beanclass="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
- </util:list>
- </property>
- </bean>
另外一种直接放弃String,而是使用对象
我之前是这样写的,也没有用
- <bean
- class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <propertyname="messageConverters">
- <list>
- <bean
- class="org.springframework.http.converter.StringHttpMessageConverter">
- <propertyname="supportedMediaTypes">
- <list>
- <value>text/plain;charset=UTF-8</value>
- </list>
- </property>
- </bean>
- <bean
- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
- </list>
- </property>
- </bean>
问题补充:
xiaoZ5919 写道
- if(writeAcceptCharset){
- outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
- }
- MediaTypecontentType=outputMessage.getHeaders().getContentType();
- Charsetcharset=contentType.getCharSet()!=null?contentType.getCharSet():DEFAULT_CHARSET;
- FileCopyUtils.copy(s,newOutputStreamWriter(outputMessage.getBody(),charset));
兄弟我一直都设置了这个
- <filter>
- <filter-name>CharacterEncodingFilter</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
问题补充:
xiaoZ5919 写道
兄弟 也许我没有说清楚,
需要设置的是response.setContentType("text/html; charset=UTF-8")
而不是response.setCharacterEncoding(this.encoding);
你看看我贴的代码就知道了
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
那个filter里面设置的是
response.setCharacterEncoding(this.encoding);
不过我觉得这不是一个好的办法
最好的办法还得是重写一个StringHttpMessageConverter,使其能灵活配置charset
需要设置的是response.setContentType("text/html; charset=UTF-8")
而不是response.setCharacterEncoding(this.encoding);
你看看我贴的代码就知道了
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
那个filter里面设置的是
response.setCharacterEncoding(this.encoding);
不过我觉得这不是一个好的办法
最好的办法还得是重写一个StringHttpMessageConverter,使其能灵活配置charset
xiaoZ5919 写道
兄弟 也许我没有说清楚,
需要设置的是response.setContentType("text/html; charset=UTF-8")
而不是response.setCharacterEncoding(this.encoding);
你看看我贴的代码就知道了
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
那个filter里面设置的是
response.setCharacterEncoding(this.encoding);
不过我觉得这不是一个好的办法
最好的办法还得是重写一个StringHttpMessageConverter,使其能灵活配置charset
需要设置的是response.setContentType("text/html; charset=UTF-8")
而不是response.setCharacterEncoding(this.encoding);
你看看我贴的代码就知道了
Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
那个filter里面设置的是
response.setCharacterEncoding(this.encoding);
不过我觉得这不是一个好的办法
最好的办法还得是重写一个StringHttpMessageConverter,使其能灵活配置charset
兄弟还是不行哦
- @RequestMapping(value="/getForm")
- @ResponseBody
- publicStringgetForm(HttpServletResponseresponse,Stringpid){
- response.setContentType("text/html;charset=UTF-8");
- System.out.println(super.formService.getRenderedStartForm(pid)
- .toString());
- return"你好";
- }