代码方式配置springmvc的字符集过滤器
之前一直用的xml方式配置
<filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> <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>
一直不知道代码怎么配置,现在有了
// 继承AbstractAnnotationConfigDispatcherServletInitializer @Override public void onStartup(ServletContext servletContext) throws ServletException { String filterName = "myfilter"; FilterRegistration.Dynamic filterRegistration = servletContext.addFilter(filterName, new MyFilter()); filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/*"); //父类的方法一定要调用,否则getRootConfigClasses与getServletConfigClasses等方法都会失效 super.onStartup(servletContext); }
相关推荐
JudeJoo 2020-08-21
meleto 2020-08-15
玫瑰小妖 2020-07-18
dlutbob 2020-07-04
zmysna 2020-07-04
小鱿鱼 2020-06-26
MicroBoy 2020-06-17
zhongliwen 2020-06-13
zhongliwen 2020-06-13
qingjiuquan 2020-06-07
牧场SZShepherd 2020-06-03
zhangdy0 2020-05-31
spring艳 2020-05-30
方志朋 2020-05-30
咻pur慢 2020-05-27
mxcsdn 2020-05-27
牧场SZShepherd 2020-05-18
HappyHeng 2020-05-16