1

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.

Here are some screeshots.

passed with model

I passed string with model and it is working.

message for validation

This is validation message came from my message.properties file. Encoding error occurred.

postgres

I am trying to save some record to db and error occurred again.

Here is what I have done.

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" />

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() {
}
}

All my html files has this meta tag line for encoding.

    <meta http-equiv="Content-Type" content="text/html; charset=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;
}

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;

I created JAVA_OPTS environment variable for encoding.

JAVA_OPTS=-DuriEncoding=UTF-8 -Dfile.encoding=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>

I tried everything on the internet but still can't find a solution. Any idea about what I am missing would be great.

fatiherdem
  • 1,173
  • 6
  • 18
  • 35

3 Answers3

3

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

Community
  • 1
  • 1
dimedrol90
  • 276
  • 1
  • 4
  • 12
0

I had the very same issue with a validation message and all the code solutions were still not enough. In my case using Intellij as editor I checked Transaparent native-to-ascii conversion to solve the issue. Please see my complete answer here.

Community
  • 1
  • 1
Paizo
  • 3,986
  • 30
  • 45
0

You can use this trick of thymeleaf comment before process:

enter image description here

Paul Roub
  • 36,322
  • 27
  • 84
  • 93