-
自定义过滤器
- 自定义实现Filter接口
public class MyCharacterFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { //这里加入对编码的处理 servletRequest.setCharacterEncoding("utf-8"); //放行请求,这个规则和前面讲过的java web的过滤器一样 filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
- 配置web.xml
<filter> <filter-name>myCharacterFilter</filter-name> <filter-class>com.web.filter.MyCharacterFilter</filter-class> </filter> <filter-mapping> <filter-name>myCharacterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
-
springmvc中的过滤器处理中文
- 修改web.xml文件
<!--配置Spring提供的过滤器,解决中文乱码问题--> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>