Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java .properties文件中的消息不显示UTF-8字符_Java_Spring_Utf 8_Intellij Idea_Thymeleaf - Fatal编程技术网

Java .properties文件中的消息不显示UTF-8字符

Java .properties文件中的消息不显示UTF-8字符,java,spring,utf-8,intellij-idea,thymeleaf,Java,Spring,Utf 8,Intellij Idea,Thymeleaf,正如标题所说,我只能补充说,如果我在html文件中手动输入ąęó,就可以了 视图解析程序: <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver"> <property name="prefix" value="/WEB-INF/templates/" /> <property name="suffix" val

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

视图解析程序:

<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>
UTF-8
html输入示例:

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

在html文件的标记内:

<meta charset="UTF-8"/>

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

我真的不知道问题出在哪里。当我将locale更改为pl时,这是输出:

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

在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>

编码滤波器
org.springframework.web.filter.CharacterEncodingFilter
编码
UTF-8
强制编码
真的
编码滤波器
/*

好吧,我想出来了。交易如下:

我有以下代码:

<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”。由于某些原因,如果我没有更改它,它将找不到消息包。

当您使用ResourceBundleMessageSource对象时,需要按如下方式设置默认编码:

@Bean
public MessageSource messageSource() { 
    ResourceBundleMessageSource resource = 
            new ResourceBundleMessageSource();
    resource.setBasename("messages");
    resource.setDefaultEncoding("utf-8");
    return resource;    
}

谢谢你添加图片。对我有用。谢谢对不起,这个答案和我的有什么不同?在我的答案的选项1中,我设置了默认编码。你可能是对的,我也认为这两种解决方案是相同的。