我有一个 Spring MVC 应用程序,使用 Thymeleaf 作为视图技术。我在 HTML 中显示希伯来语文本时遇到困难。我有一个简单的 HTML 文件,其中包含一些希伯来语文本:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<title>Document</title>
</head>
<body>
<h1>שלום</h1>
</body>
</html>
当使用 JSP(使用 Spring MVC)提供此页面时,它看起来很好。当切换到 Thymeleaf 时,会出现问题。我尝试在 Thymeleaf 视图解析器上设置字符编码:
@Bean
public ThymeleafViewResolver viewResolver(){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
但这并没有帮助。
您可以尝试将
setForceTemplateMode
设置为 true
吗?
@Bean
public ThymeleafViewResolver viewResolver(){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setForceTemplateMode(true); // <- changed here!
return viewResolver;
}