java乱码问题完全解决方案

java乱码问题完全解决方案

处理步骤如下:

1. 统一使用utf-8编码;

2. tomcat 的Connector中的配置 URIEncoding='utf-8'

3. jps页面需要在最上面加指令:

<%@page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

4. 使用在web.xml中使用org.springframework.web.filter.CharacterEncodingFilter

<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>

5 .对返回@ResponseBody的, 它会使用StringHttpMessageConverter,这其默认编码是utf-8,所以需要把配置。这个有Charset为参数的构造函数是在后面某个版本才出现的。所以最好对每个版本看源码确认下。

@Bean

//@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)

public StringHttpMessageConverter stringHttpMessageConverter() {

StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(

Charset.forName(defaultEncoding));

stringHttpMessageConverter.setWriteAcceptCharset(false);

return stringHttpMessageConverter;

}

你可能感兴趣的:(java,jsp,encoding)