java - Thymeleaf 和 Spring Boot 不从本地化文件读取属性 - 显示问号而不是本地化字符串

标签 java spring spring-boot localization thymeleaf

当尝试本地化静态字符串时,消息会显示在问号“??”周围

例如??ticket.type_en_US??

<p th:text="#{ticket.type}">Blah</p>
  • 我正在使用 SpringBoot 1.3.6.RELEASE
  • thymeleaf :3.0.0.RELEASE
  • thymeleaf-spring4 Artifact

我已在 application.properties 中配置了消息的基本名称

messages.properties 和 messages_en_US.properties 的内容是:

ticket.type=BUGS!!!!

配置:

spring.messages.basename=messages

启动时的输出:

2016-07-19 08:38:28.673 DEBUG 5175 --- [ main] ationConfigEmbeddedWebApplicationContext : Using MessageSource [org.springframework.context.support.ResourceBundleMessageSource: basenames=[messages]]

我还尝试以编程方式使用下面代码中的 MessageResource。我将 messages.properties 文件放在与 application.properties 文件相同的文件夹中。 src/main/resources/

@Configuration
@ComponentScan("controller") 
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{

    private ApplicationContext applicationContext;

    @Autowired
    private MessageSource messageSource;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

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

    @Bean
    public TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        engine.setMessageSource(messageSource);
        return engine;
    }

    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("/WEB-INF/templates/");
        resolver.setTemplateMode(TemplateMode.HTML);
        return resolver;
    }
}

为了完整起见,这是我的应用程序配置(与其他应用程序配置一样,我必须排除 Thymeleaf 类):

@SpringBootApplication(exclude={org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class})
public class Application {

    public static void main(String[] args) {

        ApplicationContext ctx = SpringApplication.run(Application.class, args);

    }

}

我还通过在我的 REST 端点调用之一上打印内容来验证是否正在加载消息包:

@Autowired
    private MessageSource messageSource;

    @GET
    @Produces("application/json")
    public List<MyData> getData() {
        System.out.println("HERE 1 in Conversions");

        System.out.println(messageSource.getMessage("ticket.type", null, Locale.US));

        return getTheData();
    }

这会打印出以下内容,所以我知道 spring-boot 正在加载资源包,但 Thymeleaf 没有以某种方式获取它们:

BUGS!!!!

这是我的完整 HTML 页面,可能有问题:

<html xmlns:th="http://www.thymeleaf.org">
<head>

    <title>Kitchen Sink</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
          th:href="@{/webjars/bootstrap/3.3.4/css/bootstrap.min.css}"
          rel="stylesheet" media="screen" />

    <script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"
            th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>

    <link href="../static/css/mike.css"
          th:href="@{css/mike.css}" rel="stylesheet" media="screen"/>
</head>

<body>
<div class="container">
    <div class="jumbotron">

        <h1>Hello</h1>

        <h2>Welcome to the Kitchen Sink!</h2>

        <p th:text="#{ticket.type}">Blah</p>

        <p th:text="#{test.type}">dssfgf</p>

      </div>

</body>

</html>

最佳答案

好吧,我明白了。感谢 @M.Deinum 指出我应该让 spring-boot 和 Thymeleaf 做他们应该做的事情。

我必须设置:

engine.setMessageSource(messageSource);

并添加:

@Bean 

其他 2 个函数。这允许将 MessageSource 传递到引擎并正确解析属性。

我将使用正确的来源更新上面的问题,以便人们可以将其用作引用

关于java - Thymeleaf 和 Spring Boot 不从本地化文件读取属性 - 显示问号而不是本地化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38459269/

相关文章:

java - JPA 示例 Spring Boot 应用程序抛出 BeanCreationException

c# - 大屏多点触控交互应用设计

java - 如何在java中查看数据库JDBC ResultSet里面有什么

java - 从适配器中选择 Android

java - Controller 在一种环境中接受 null 作为请求主体,但在另一种环境中拒绝

Java Spring从html表单获取数据

java - 在测试类中使用@PostConstruct会导致它被多次调用

java - 打印两个节点之间的路径路由

java - Spring @Component 未正确读取 .properties 字段

java - 如何在 Spring Boot 应用程序中配置自定义数据库连接超时?