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 wich 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;    
    }
Ars
  • 522
  • 3
  • 17

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
  • 13,439
  • 5
  • 35
  • 53
-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
  • 50
  • 6