Nixit Patel Nixit Patel - 2 months ago 19
Java Question

Spring - Thymeleaf - Tomcat -> can not process UTF-8 character correctly

I am trying to display UTF-8 character in Spring-MVC web page.

utf-8 values are coming from the database. I have already converted my database into utf-8.

web services I have written is displaying the values as expected but, in the web page it is not showing the correct value.

below is my spring configuration, please let me know what I am doing wrong.

I am using thymeleaf as a templating engine:

Thymeleafe configuration:

<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".html"/>
<property name="characterEncoding" value="UTF-8" />
<property name="templateMode" value="HTML5"/>
</bean>

<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
</bean>

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
</bean>


I have also added CharecterEncodingFilter in my web.xml as below:

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


I have also tried changing the server.xml in tomcat configuration:

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" useBodyEncodingForURI="true"/>


but it is not working as expected:
it is showing me ???? in place of the actual value. and the strange thing is when I use the Unicode value of the actual value then it is working.

can anyone please let me know what I am doing wrong or missing.

Update Ans config using XML

<bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
</bean>

<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver"/>
</bean>

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="characterEncoding" value="UTF-8" />
<property name="templateEngine" ref="templateEngine"/>
</bean>


I have to set the characterEncoding in
ThymeleafViewResolver
instead of ServletContextTemplateResolver

Answer

You should try to add utf to thymeleaf view resolver:

Bean
public ThymeleafViewResolver viewResolver() {
    ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
    thymeleafViewResolver.setTemplateEngine(templateEngine());
    thymeleafViewResolver.setCharacterEncoding("UTF-8");
    return thymeleafViewResolver;
}
Comments