1

I'm doing small Spring Mvc project in InteIj idea. Mostly I'm using English so everything works fine. But when I try to use utf-8 (ru) characters on my website, I only get ???? symbols instead of text.

My html pages in IDE encoded to utf-8. Project encoding also set to utf-8. If I use logging or System.out.println ide prints in console utf-8 symbols. But when I send them in a model or just use plain text in html in utf-8 everything becomes ???

Html encoding also set to

I've tried to set filter which will encode all requests and responses to utf-8. Still same ???? symbols.

my html:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="UTF-8"/>
    <div th:replace="fragments/header :: header-css"></div>
</head>
<body>
<div th:replace="fragments/header :: header"/>
<div class="container">
    <header>
        <h1 align="center" th:text="${text}">
            ру текст (utf-8 ru text)
        </h1>
    </header>
</div>
</body>
</html>

my controller:

 @GetMapping
    public ModelAndView showCheckList() {
        String text = "тестовый текст";
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("text",text);
        modelAndView.setViewName("shop/CheckList");    
        return modelAndView;    
    }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Ars
  • 591
  • 1
  • 4
  • 19

2 Answers2

1

By seeing your code I can say that you are using thymeleaf as view technology.

Refer this thread.

Property characterEncoding should be explicitly set for templateResolver and ThymeleafViewResolver:

<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    ...
    <property name="characterEncoding" value="UTF-8"/>
    ...
</bean>

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

Or using annotation.

@Bean
    public ThymeleafViewResolver thymeleafViewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }
Alien
  • 15,141
  • 6
  • 37
  • 57
-1

Do some changes in the database table change character set utf8 and utf8_general_ci show in the below screenshot. enter image description here

iragond
  • 60
  • 6