- @RequestMapping(value = "/getForm")
- @ResponseBody
- public String getForm(String pid) {
- return "你好";
- }
返回结果“你好”
- @RequestMapping(value = "/getForm")
- @ResponseBody
- public List<String> getForm(String pid) {
- return new ArrayList<String>(){{
- add("你好")
- }};
- }
- <?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" xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
- <context:component-scan base-package="cn.netcluster.workflow.**" />
- <mvc:annotation-driven />
- <mvc:default-servlet-handler />
- <bean
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
- id="multipartResolver">
- <property name="defaultEncoding" value="UTF-8" />
- <property name="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的
- public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
解决的办法:
你自己重写一个StringHttpMessageConverter,使用你想要的字符集,并且使这个属性可注入
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="messageConverters">
- <util:list>
- <bean class="com.pcds.ecomm.website.syscustomization.ConfigurableStringHttpMessageConverter">
- <constructor-arg value="UTF-8"/>
- </bean>
- <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
- </util:list>
- </property>
- </bean>
另外一种直接放弃String,而是使用对象
我之前是这样写的,也没有用
- <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/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());
- }
- MediaType contentType = outputMessage.getHeaders().getContentType();
- Charset charset = contentType.getCharSet() != null ? contentType.getCharSet() : DEFAULT_CHARSET;
- FileCopyUtils.copy(s, new OutputStreamWriter(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
- public String getForm(HttpServletResponse response, String pid) {
- response.setContentType("text/html; charset=UTF-8");
- System.out.println(super.formService.getRenderedStartForm(pid)
- .toString());
- return "你好";
- }