[英]Spring mvc encoding and "???" symbols instead of utf-8 symbols in html
I'm doing small Spring Mvc project in InteIj idea.我正在 InteIj idea 中做小型 Spring Mvc 项目。 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 ????
但是当我尝试在我的网站上使用 utf-8 (ru) 字符时,我只得到 ???? symbols instead of text.
符号代替文字。
My html pages in IDE encoded to utf-8.我在 IDE 中的 html 页面编码为 utf-8。 Project encoding also set to utf-8.
项目编码也设置为 utf-8。 If I use logging or System.out.println ide prints in console utf-8 symbols.
如果我在控制台 utf-8 符号中使用日志记录或 System.out.println ide 打印。 But when I send them in a model or just use plain text in html in utf-8 everything becomes ???
但是,当我将它们发送到模型中或仅在 utf-8 中使用 html 中的纯文本时,一切都变成了???
Html encoding also set to Html 编码也设置为
I've tried to set filter which will encode all requests and responses to utf-8.我试图设置过滤器,它将所有请求和响应编码为 utf-8。 Still same ????
还是一样??? symbols.
符号。
my html:我的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.通过查看您的代码,我可以说您正在使用 thymeleaf 作为视图技术。
Property characterEncoding
should be explicitly set for templateResolver
and ThymeleafViewResolver
:应该为
templateResolver
和ThymeleafViewResolver
显式设置属性characterEncoding
:
<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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.