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.

我的问题是,每当我保存 añ时,它就会变成?.

当我尝试ñ在 mysql 终端中手动保存时,它的保存正确。我怀疑问题出在我的服务器或程序中。请帮忙。

4

5 回答 5

25

我成功地尝试了这个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>
于 2013-04-13T20:35:30.880 回答
8

要在 Spring Boot 中工作,您可以使用

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

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

另一个回应

于 2015-07-02T18:16:46.980 回答
5

确保您的 jsp 中有以下代码段

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

并确保它是文件encodingFilter中的第一个过滤器web.xml

于 2012-11-22T06:06:39.803 回答
4

我的解决方案,使用 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;
}
于 2014-05-09T16:24:28.617 回答
3

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

}

于 2016-01-21T07:53:15.567 回答