[英]Spring mvc encoding and "???" symbols instead of utf-8 symbols in html
我正在 InteIj idea 中做小型 Spring Mvc 项目。 我主要使用英语,所以一切正常。 但是当我尝试在我的网站上使用 utf-8 (ru) 字符时,我只得到 ???? 符号代替文字。
我在 IDE 中的 html 页面编码为 utf-8。 项目编码也设置为 utf-8。 如果我在控制台 utf-8 符号中使用日志记录或 System.out.println ide 打印。 但是,当我将它们发送到模型中或仅在 utf-8 中使用 html 中的纯文本时,一切都变成了???
Html 编码也设置为
我试图设置过滤器,它将所有请求和响应编码为 utf-8。 还是一样??? 符号。
我的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>
我的控制器:
@GetMapping
public ModelAndView showCheckList() {
String text = "тестовый текст";
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("text",text);
modelAndView.setViewName("shop/CheckList");
return modelAndView;
}
通过查看您的代码,我可以说您正在使用 thymeleaf 作为视图技术。
参考 这个线程。
应该为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>
或使用注释。
@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.