java - 来自 .properties 文件的消息不显示 UTF-8 字符

标签 java spring utf-8 intellij-idea thymeleaf

正如标题所说,我只能补充说,如果我在html文件中手动输入ąęó,就可以了。

View 解析器:

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

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

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

pom.xml:

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

html 输入示例:

<h2><p th:text="#{homepage.greeting}">Welcome</p></h2>

在html文件的标签内:

<meta charset="UTF-8"/>

在 IntelliJ Idea 中,我将项目编码设置为 UTF-8,属性文件的默认编码设置为 UTF-8 enter image description here

老实说,我不知道问题出在哪里。当我将语言环境更改为 pl 时,这是输出: enter image description here

抱歉,我还不能发布图片。 任何帮助将不胜感激。

在 web.xml 中试过这个过滤器,还是不行。

<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>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

最佳答案

好的,我想通了。这是交易:

我有以下代码:

<bean id="messageSource"
      class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages" />
</bean>

选项 1: 只需将其添加为另一个属性:

<property name="defaultEncoding" value="UTF-8"/>

选项 2: 改为:

<bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages"/>
    <property name="defaultEncoding" value="UTF-8"/>
</bean>

如果您将其更改为可重新加载,请记住还要将属性值从“messages”更改为“classpath:messages”。由于某种原因,如果我不更改它,它就找不到消息包。

关于java - 来自 .properties 文件的消息不显示 UTF-8 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26432479/

相关文章:

java - 如何以编程方式将Java中的bean添加到Spring中的app-context.xml中?

java - 是否可以在上下文配置中指定一个 @Component 类,以便自动解析它的依赖项?

python - 用utf-8编码将ElementTree直接写入zip

java - 各个 Java 字节码的执行时间有多大差异?

捕获多个集合的 Java 正则表达式模式

java - Spring Boot : Unable to read resources in lib/*. jar 使用 lib/*.jar 中的类

python - Windows 文件名在 Linux 中显示损坏的字符

python - 这个字符串的编码是什么?

Java后端: Guarantee two Facebook users are friends

java - 错误 : method does not override or implement a method from a supertype