在我的消息属性中,我定义了许多文本,它们都可以工作...但是我不知道为什么。
这是我关注的模板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();
}
Messages.properties
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
答案 0 :(得分:0)
DateFormatter.java 该文件也在项目中。它包含
最终字符串格式= this.messageSource.getMessage(“ date.format”, null,区域设置);
我对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;
}
}