春天 - 百里香 - 雄猫 ->无法正确处理 UTF-8 字符 [英] Spring - Thymeleaf - Tomcat -> can not process UTF-8 character correctly

查看:30
本文介绍了春天 - 百里香 - 雄猫 ->无法正确处理 UTF-8 字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Spring-MVC 网页中显示 UTF-8 字符.

I am trying to display UTF-8 character in Spring-MVC web page.

utf-8 值来自数据库.我已经将我的数据库转换为 utf-8.

utf-8 values are coming from the database. I have already converted my database into utf-8.

我编写的网络服务按预期显示值,但在网页中未显示正确值.

web services I have written is displaying the values as expected but, in the web page it is not showing the correct value.

以下是我的弹簧配置,请让我知道我做错了什么.

below is my spring configuration, please let me know what I am doing wrong.

我使用 thymeleaf 作为模板引擎:

I am using thymeleaf as a templating engine:

Thymeleafe 配置:

<bean id="templateResolver"
      class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".html"/>
    <property name="characterEncoding" value="UTF-8"/>
    <property name="templateMode" value="HTML5"/>
</bean>

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver"/>
</bean>

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine"/>
</bean>

我还在我的 web.xml 中添加了 CharecterEncodingFilter,如下所示:

I have also added CharecterEncodingFilter in my web.xml as below:

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

我也尝试在 tomcat 配置中更改 server.xml:

I have also tried changing the server.xml in tomcat configuration:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000" redirectPort="8443"
           URIEncoding="UTF-8" useBodyEncodingForURI="true"/>

但它没有按预期工作:它在给我看????代替实际值.奇怪的是,当我使用实际值的 Unicode 值时,它就起作用了.

but it is not working as expected: it is showing me ???? in place of the actual value. and the strange thing is when I use the Unicode value of the actual value then it is working.

谁能告诉我我做错了什么或遗漏了什么.

can anyone please let me know what I am doing wrong or missing.

使用 XML 更新 Ans 配置

<bean id="templateResolver"
      class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".html"/>
    <property name="templateMode" value="HTML5"/>
</bean>

<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver"/>
</bean>

<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="characterEncoding" value="UTF-8" />
    <property name="templateEngine" ref="templateEngine"/>
</bean>

我必须在 ThymeleafViewResolver 中设置 characterEncoding 而不是 ServletContextTemplateResolver

I have to set the characterEncoding in ThymeleafViewResolver instead of ServletContextTemplateResolver

推荐答案

您应该尝试将 utf 添加到 thymeleaf 视图解析器:

You should try to add utf to thymeleaf view resolver:

Bean
public ThymeleafViewResolver viewResolver() {
    ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
    thymeleafViewResolver.setTemplateEngine(templateEngine());
    thymeleafViewResolver.setCharacterEncoding("UTF-8");
    return thymeleafViewResolver;
}

这篇关于春天 - 百里香 - 雄猫 ->无法正确处理 UTF-8 字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆