title: Spring MVC with Thymeleaf and Tomcat 8 UTF-8 Encoding Issue
body: <p>I have some problem about encoding. According to <a href="http://wiki.apache.org/tomcat/FAQ/CharacterEncoding#Q8" rel="nofollow noreferrer">this referance</a> 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.</p>
  
  <p>Here are some screeshots. </p>
  
  <p><a href="https://i.stack.imgur.com/j095l.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j095l.jpg" alt="passed with model"></a></p>
  
  <p><strong>I passed string with model and it is working.</strong></p>
  
  <p><a href="https://i.stack.imgur.com/KfnyM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KfnyM.jpg" alt="message for validation"></a></p>
  
  <p><strong>This is validation message came from my message.properties file. Encoding error occurred.</strong></p>
  
  <p><a href="https://i.stack.imgur.com/ZQ4gP.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZQ4gP.jpg" alt="postgres"></a></p>
  
  <p><strong>I am trying to save some record to db and error occurred again.</strong> </p>
  
  <p>Here is what I have done. </p>
  
  <p>I edited Tomcat's <strong>server.xml</strong> config and added UTF-8 encoding.</p>
  
  <p><strong>server.xml</strong></p>
  
  <pre><code>  &lt;Connector port="8080" protocol="HTTP/1.1"
             connectionTimeout="20000"
             useBodyEncodingForURI="true"
             URIEncoding="UTF-8"
             redirectPort="8443" /&gt;
  </code></pre>
  
  <p>I created <strong>CharacterEncodingFilter</strong> in my config folder.</p>
  
  <p><strong>CharacterEncodingFilter.java</strong></p>
  
  <pre><code>@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() {
  }
  }
  </code></pre>
  
  <p>All my html files has this meta tag line for encoding.</p>
  
  <pre><code>    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  </code></pre>
  
  <p>My Thymeleaf configs are also set to UTF-8 encoding.</p>
  
  <pre><code>@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;
  }
  </code></pre>
  
  <p>I created my database with UTF-8 encoding like this.</p>
  
  <pre><code>postgres=# CREATE USER unicodeuser WITH PASSWORD 'mypass123'; 
  postgres=# CREATE DATABASE unicode WITH ENCODING 'UTF8' OWNER unicodeuser; 
  postgres=# GRANT ALL ON DATABASE unicode TO unicodeuser;
  </code></pre>
  
  <p>I created JAVA_OPTS environment variable for encoding.</p>
  
  <pre><code>JAVA_OPTS=-DuriEncoding=UTF-8 -Dfile.encoding=UTF-8
  </code></pre>
  
  <p>I also changed my <strong>pom.xml</strong> files for UTF-8 encoding.</p>
  
  <pre><code>&lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  &lt;/properties&gt;
  ...
  &lt;plugin&gt;
      &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
      &lt;artifactId&gt;maven-resources-plugin&lt;/artifactId&gt;
      &lt;version&gt;2.7&lt;/version&gt;
      &lt;configuration&gt;
          &lt;encoding&gt;UTF-8&lt;/encoding&gt;
      &lt;/configuration&gt;
   &lt;/plugin&gt;
  </code></pre>
  
  <p>I tried everything on the internet but still can't find a solution. Any idea about what I am missing would be great.</p>