@EnableWebMvc
@ComponentScan(basePackages = { "com.lwx" })
public class MyWebConfig extends WafWebMvcConfigurerAdapter {
@Bean
public ResourceBundleMessageSource getResourceBundleMessageSource () {
ResourceBundleMessageSource source =new ResourceBundleMessageSource ();
LOG.info("ResourceBundleMessageSource load");
source.setUseCodeAsDefaultMessage(true);
source.setCacheSeconds(10);
source.setBasename("messages");
source.setDefaultEncoding("utf-8");
return source;
}
@Bean
public LocalValidatorFactoryBean getLocalValidatorFactoryBean() {
LOG.info("LocalValidatorFactoryBean load");
LocalValidatorFactoryBean factoryBean =new LocalValidatorFactoryBean();
factoryBean.setValidationMessageSource(getResourceBundleMessageSource());
return factoryBean;
}
}
@NotNull(message="{model.href.value.errormsg}")
private String href;
配置文件 messages.properties
model.href.value.errormsg=test
问题描述:直接通过ResourceBundleMessageSource 是可以取到model.href.value.errormsg对应的值,但是实际中如果触发了NotNull标签,发现message实际为model.href.value.errormsg而不是test
不要在配置一个validator
@Override
public Validator getValidator() {
return getLocalValidatorFactoryBean();
}