如何使用org.springframework.web.filter.CharacterEncodingFilter来纠正字符编码?

时间:2012-11-22 01:38:10

标签: spring hibernate character-encoding tomcat6

我需要一些帮助。

我将代码段放在我的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终端中手动保存ñ时,它正确保存。我怀疑问题出在我的服务器或我的程序中。请帮忙。

5 个答案:

答案 0 :(得分:22)

我在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>

答案 1 :(得分:7)

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

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

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

another response

答案 2 :(得分:5)

确保jsp中有以下代码段

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

并确保encodingFilterweb.xml文件

中的第一个过滤器

答案 3 :(得分:2)

我的解决方案,使用Spring(3.2.x)AnnotationConfigWebApplicationContext,基于Spring Framework Reference:

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;
}

答案 4 :(得分:2)

它可能有点迟,但最好在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的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 */
    }
   }

}