如何使用org.springframework.web.filter.CharacterEncodi

2019-07-04 01:17发布

我需要一些帮助。

我把我的下面片断代码web.xml

<filter>
    <filter-name>encodingFilter</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>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

在我server.xml

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

我的JSP页面编码为UTF-8和我的MySQL表编码为utf8_general_ci

我的问题是,每当我保存ñ它变成?

当我试图手动保存ñ在mysql终端的正确保存。 我怀疑问题出在我的服务器或我的计划之内。 请帮忙。

Answer 1:

我这个成功尝试在web.xml

<filter>
    <filter-name>encodingFilter</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>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>


Answer 2:

要在春季工作启动时,您可以使用

@Bean
public FilterRegistrationBean filterRegistrationBean() {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");

    FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(filter);
    registrationBean.addUrlPatterns("/*");
    return registrationBean;
}

另一响应



Answer 3:

请确保您有下面的代码片段在你的JSP

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" language="java" %>

并确保该encodingFilter在第一过滤器web.xml文件



Answer 4:

我的解决方案,使用Spring(3.2.x中)AnnotationConfigWebApplicationContext,基于Spring框架参考:

public class StudentApplicationConfig extends AbstractDispatcherServletInitializer {

@Override
protected WebApplicationContext createServletApplicationContext() {

    AnnotationConfigWebApplicationContext dispatcherContext
            = new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(DispatcherConfig.class);
    return dispatcherContext;
}

@Override
protected String[] getServletMappings() {

    return new String[] { "/" };
}

@Override
protected WebApplicationContext createRootApplicationContext() {

    AnnotationConfigWebApplicationContext appContext
            = new AnnotationConfigWebApplicationContext();
    return appContext;
}

@Override
protected Filter[] getServletFilters() {
    Filter[] filters;

    CharacterEncodingFilter encFilter;
    HiddenHttpMethodFilter httpMethodFilter = new HiddenHttpMethodFilter();

    encFilter = new CharacterEncodingFilter();

    encFilter.setEncoding("UTF-8");
    encFilter.setForceEncoding(true);

    filters = new Filter[] {httpMethodFilter, encFilter};
    return filters;
}


Answer 5:

这可能是有点晚,但it's更好地配置在Tomcat中的conf / web.xml文件(或在web.xml中的项目)

<filter>
    <filter-name>SetCharacterEncoding</filter-name>
    <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>SetCharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

见http://wiki.apache.org/tomcat/FAQ/CharacterEncoding或tomcat's 的web.xml

或者这样的车:

public class StartEmbeddedJetty{

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    ServletContextHandler sch = new ServletContextHandler();
    sch.addFilter(CharacterEncodingFilter .class, "/*", EnumSet.of(DispatcherType.REQUEST));
      ...
    server.start();
    server.join();
}

public static class CharacterEncodingFilter implements Filter {
     @Override
     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        // set encoding to utf-8
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
     }

     @Override
     public void init(FilterConfig arg0) throws ServletException {
        /* empty */
     }

     @Override
    public void destroy() {
        /* empty */
    }
   }

}



文章来源: how to use org.springframework.web.filter.CharacterEncodingFilter to correct character encoding?