应用上下文初始化时候注册CharacterEncodingFilter过滤器,防止表单提交中文乱码

master
luffy9412 7 years ago
parent eb3453240c
commit abb186c9d3
  1. 16
      src/main/java/org/pqh/config/WebConfig.java

@ -3,7 +3,8 @@ package org.pqh.config;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import javax.servlet.Filter;
import javax.servlet.*;
import java.util.EnumSet;
/**
* Created by reborn on 2017/7/28.
@ -13,12 +14,6 @@ public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitiali
private String encoding ="UTF-8";
@Override
protected Filter[] getServletFilters() {
return new Filter[]{new CharacterEncodingFilter(encoding)};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
@ -34,8 +29,13 @@ public class WebConfig extends AbstractAnnotationConfigDispatcherServletInitiali
return new Class[]{SpringConfig.class};
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
FilterRegistration.Dynamic dynamic=super.registerServletFilter(servletContext,new CharacterEncodingFilter(encoding));
dynamic.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*");
}
}

Loading…
Cancel
Save