spring 如何使用 org.springframework.web.filter.CharacterEncodingFilter 来纠正字符编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13504732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use org.springframework.web.filter.CharacterEncodingFilter to correct character encoding?
提问by TheOnlyIdiot
I need some help.
我需要帮助。
I placed the code snippet below in my web.xml.
我将下面的代码片段放在我的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>
and in my server.xml:
在我的server.xml:
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>
My jsp pages are encoded as UTF-8and my mysql table is encoded as utf8_general_ci.
我的 jsp 页面被编码为UTF-8,我的 mysql 表被编码为utf8_general_ci.
My problem is that whenever I save a ?it becomes ?.
我的问题是,每当我保存?它时,它就会变成?.
When I tried to manually save ?in the mysql terminal its saving properly. I suspect the problem lies within my server or my program. Please help.
当我尝试?在 mysql 终端中手动保存时,它正确保存。我怀疑问题出在我的服务器或我的程序中。请帮忙。
回答by Hanske1967
I tried successfully with this in web.xml!
我在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>
回答by palhares
To work in spring boot you can use
要在 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;
}
回答by DDK
Make sure you have the following snippet in your jsp
确保您的jsp中有以下代码段
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" language="java" %>
and also make sure that the encodingFilteris the first filter in web.xmlfile
并确保这encodingFilter是web.xml文件中的第一个过滤器
回答by szaroblekitny
My solution, using Spring (3.2.x) AnnotationConfigWebApplicationContext, based on Spring Framework Reference:
我的解决方案,使用 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;
}
回答by Vitali Heinrich
it might be little be late, but it′s better to configure it in tomcat conf/web.xml (or for project in web.xml)
可能有点晚了,但最好在 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>
See http://wiki.apache.org/tomcat/FAQ/CharacterEncodingor tomcat′s web.xml
参见http://wiki.apache.org/tomcat/FAQ/CharacterEncoding或 tomcat 的web.xml
Or for jetty like this:
或者像这样的码头:
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 */
}
}
}
}

