简体   繁体   中英

Spring mvc encoding and "???" symbols instead of utf-8 symbols in html

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

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

Do some changes in the database table change character set utf8 and utf8_general_ci show in the below screenshot. 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM