首页 > 解决方案 > Thymeleaf th:text="#{date.format}" 工作但在 messages.properties 中没有条目

问题描述

在我的消息属性中,我定义了许多文本,它们都有效……但我不知道为什么。

这是我关心的模板 div:

<div>
                <label for="datePlanted">
                    <span th:text="#{seedstarter.datePlanted}">Date planted</span>
                    (<span th:text="#{date.format}">YYYY/MM/DD</span>)
                </label>
                <input type="text" th:field="*{datePlanted}" th:errorclass="fieldError"/>
            </div>

但是我的消息属性中没有 date.format 条目!这里发生了什么?

我唯一的线索是在我的 WebConfig 中:

@Configuration
@EnableWebMvc
@ComponentScan
public class SpringWebConfig
        extends WebMvcConfigurerAdapter implements ApplicationContextAware {
.
.
.
@Override
    public void addFormatters(final FormatterRegistry registry) {
        super.addFormatters(registry);
        registry.addFormatter(varietyFormatter());
        registry.addFormatter(dateFormatter());
    }

    @Bean
    public VarietyFormatter varietyFormatter() {
        return new VarietyFormatter();
    }

    @Bean
    public DateFormatter dateFormatter() {
        return new DateFormatter();
    }

消息属性

title.application=Spring Thyme Seed-Starter Manager
title.list=Seed Starter List
title.new=Add new Seed Starter

date.format=MM/dd/yyyy
bool.true=yes
bool.false=no

seedstarter.data=Seed Starter data
seedstarter.create=Add Seed Starter
seedstarter.row.add=Add row
seedstarter.row.remove=Remove row

seedstarter.datePlanted=Date planted
seedstarter.covered=Covered
seedstarter.type=Type
seedstarter.features=Features
seedstarter.rows=Rows

seedstarter.type.WOOD=Wood
seedstarter.type.PLASTIC=Plastic

seedstarter.feature.SEEDSTARTER_SPECIFIC_SUBSTRATE=Seed starter-specific substrate
seedstarter.feature.FERTILIZER=Fertilizer used
seedstarter.feature.PH_CORRECTOR=PH Corrector used

seedstarter.rows.head.rownum=Row
seedstarter.rows.head.variety=Variety
seedstarter.rows.head.seedsPerCell=Seeds per cell

typeMismatch.datePlanted=Date has an incorrect format (see pattern)
typeMismatch.seedsPerCell=Seeds per cell must be an integer number

标签: spring-bootthymeleaf

解决方案


DateFormatter.java 这个文件也在项目中。它包含

最终字符串格式 = this.messageSource.getMessage("date.format", null, locale);

我对 MessageSource 了解不多,除了它是一个 Spring 库,它有 3 个返回字符串并采用“字符串代码”、默认消息、语言环境或 MessageSourceResolvable 的方法。我很难理解这是如何处理细节的,但我看到 DateFormatter 处理这样的视图......

DateFormatter.java

 public class DateFormatter implements Formatter<Date> {

    @Autowired
    private MessageSource messageSource;


    public DateFormatter() {
        super();
    }

    public Date parse(final String text, final Locale locale) throws ParseException {
        final SimpleDateFormat dateFormat = createDateFormat(locale);
        return dateFormat.parse(text);
    }

    public String print(final Date object, final Locale locale) {
        final SimpleDateFormat dateFormat = createDateFormat(locale);
        return dateFormat.format(object);
    }

    private SimpleDateFormat createDateFormat(final Locale locale) {
        final String format = this.messageSource.getMessage("date.format", null, locale);
        final SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        dateFormat.setLenient(false);
        return dateFormat;
    }

}

推荐阅读