spring-boot - 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
解决方案
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;
}
}
推荐阅读
- python - Python:如何检查文件夹中的文件夹?
- django - Django-wkhtmltopdf 在服务器上崩溃
- cucumber - 功能文件没有在 Selenium junit 上按定义的顺序运行?
- mysql - 查询在 mssql 中有效,但在 MYSQL 中无效
- matlab - 代码正确时的Matlab透明度违规错误
- unix - sed 多次替换第二列中的模式
- excel - Excel VBA循环通过带有选择案例语句的组合框
- javascript - 如何检查何时在 Word Online 中将注释标记为完成?Word 插件的 Javascript API
- apache-zookeeper - 无法使用 Zookeeper 启动 Flink HA 集群
- java - 使用 Tomcat 发布站点