5.中文乱码问题
直奔主题,两种解决方案
使用自己写个的过滤器类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public class EncodeFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("utf-8");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}web.xml配置
1
2
3
4
5
6
7
8<filter>
<filter-name>EncodeFilter</filter-name>
<filter-class>com.lwj.filter.EncodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>使用Spring提供的过滤器
1
2
3
4
5
6
7
8
9
10
11
12<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>