问题描述
我在编码方面有一些问题.根据此引用,我必须做所有这些事情,而且我做到了.但是在这些之后,我的编码问题仍然存在.我正在使用Spring MVC,Spring Security,Thymeleaf,Tomcat 8和Maven 3.
I have some problem about encoding. According to this referance I have to do all of this and I did. But my encoding problem still remain after those. I am using Spring MVC, Spring Security, Thymeleaf, Tomcat 8 and Maven 3.
这里有一些镜头.
我传递了带有模型的字符串,并且可以正常工作.
这是验证消息,来自我的message.properties文件.发生编码错误.
我正在尝试将一些记录保存到db,并且再次发生错误.
这就是我所做的.
我编辑了Tomcat的 server.xml 配置并添加了UTF-8编码.
I edited Tomcat's server.xml config and added UTF-8 encoding.
server.xml
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
useBodyEncodingForURI="true"
URIEncoding="UTF-8"
redirectPort="8443" />
我在配置文件夹中创建了 CharacterEncodingFilter .
I created CharacterEncodingFilter in my config folder.
CharacterEncodingFilter.java
@WebFilter(urlPatterns = {"/*"})
public class CharacterEncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig)
throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setContentType("text/html; charset=UTF-8");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
我所有的html文件都有此meta标签行用于编码.
All my html files has this meta tag line for encoding.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
我的Thymeleaf配置也设置为UTF-8编码.
My Thymeleaf configs are also set to UTF-8 encoding.
@Bean
@Description("Thymeleaf template resolver serving HTML 5")
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/WEB-INF/html/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("LEGACYHTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setCacheable(false);
return templateResolver;
}
@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
@Bean
@Description("Thymeleaf view resolver")
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setContentType("text/html;charset=UTF-8");
viewResolver.setCharacterEncoding("utf-8");
return viewResolver;
}
我用这样的UTF-8编码创建了数据库.
I created my database with UTF-8 encoding like this.
postgres=# CREATE USER unicodeuser WITH PASSWORD 'mypass123';
postgres=# CREATE DATABASE unicode WITH ENCODING 'UTF8' OWNER unicodeuser;
postgres=# GRANT ALL ON DATABASE unicode TO unicodeuser;
我创建了JAVA_OPTS环境变量进行编码.
I created JAVA_OPTS environment variable for encoding.
JAVA_OPTS=-DuriEncoding=UTF-8 -Dfile.encoding=UTF-8
我也将 pom.xml 文件更改为UTF-8编码.
I also changed my pom.xml files for UTF-8 encoding.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
我尝试了Internet上的所有内容,但仍然找不到解决方案.关于我所缺少的任何想法都会很棒.
I tried everything on the internet but still can't find a solution. Any idea about what I am missing would be great.
推荐答案
fatiherdem,在所有情况下,您都不关注Spring Security.尝试 https://stackoverflow.com/a/23051264/4380830 答案.我拥有与您相同的技术,而这个答案对我有用
fatiherdem, in all your cases you dont pay attention to Spring Security. Try that https://stackoverflow.com/a/23051264/4380830 answer. I have the same stack of technology with you and this answer work for me
这篇关于带有Thymeleaf和Tomcat 8 UTF-8编码的Spring MVC问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!