springmvc-乱码问题解决

1.通过手动创建过滤器进行解决:

public class EncodingFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {

}

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}

public void destroy() {

}
}
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.wyw.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2.通过SpringMVC给我们提供的一个过滤器,可以在web.xml中配置

<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
上一篇:SpringMVC第一天


下一篇:SpringMVC使用教程和原理