utf8与Thymeleaf的字符集
问题描述:
与Thymeleaf一起使用Spring时,我的西里尔文字符在页面上显示为?????
。utf8与Thymeleaf的字符集
使用
@RequestMapping(value = "/login", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
,因为它是这里建议:https://stackoverflow.com/a/11866822/1479414这里:https://stackoverflow.com/a/12023816/1479414没有帮助。
如何解决这个问题?
答
答案可以发现here:
物业characterEncoding
应templateResolver
和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>
答
我觉得在thymeleaf HTML页面使用的是日:与HTML元素的文本,日:文本只是显示正常文本,
如果你想使用特殊的章程与你的thymeleaf html页面,所以只需要改变例如
日:utext = “$ {yourcontroller_var}”
或
日:utext = “#{properties_var}”
例如
<div th:utext="${user.name}"> Test! </div> // for your controller variable
And
<div th:utext="#{message.username}"> UserName! </div> // for your properties variable
没有其他配置使用thymeleaf html页面使用特殊字符。
希望你解决您的问题
你错了,当页面有静态文本和文件是'UTF-8'编码这不会解决问题。 – Andremoniy
@Andremoniy什么可以解决这个问题? –
https://stackoverflow.com/a/36397227/1479414 – Andremoniy