我正在尝试将messages.properties文件插入到我的@Valid
注释验证中。我无法获取@Valid
注释以连接到messages.properties
文件。
这是一个弹簧启动配置。 messages.properties
文件位于resources文件夹中,我已将其添加到配置文件
@Configuration
public class config extends WebMvcConfigurerAdapter {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
控制器我希望看到这些消息(在我开始尝试让它们显示在Thymeleaf模板中之前)就行了
RequestMapping(value = "sampleurl", method = RequestMethod.POST)
public String requestSamplePost(@ModelAttribute @Valid SampleForm sampleForm, BindingResult bindingResult, Model model) {
//hoping to see the message from the messages file here
System.out.println("Binding Result: " + bindingResult.toString());
...
pojo有这个内容
public class SampleForm {
@NotEmpty ()
@Email()
private String email;
…
messages.properties文件有这种内容
NotEmpty.SampleForm.email=THIS IS BESPOKE MESSAGE 1
Email.SampleForm.email=THIS IS BESPOKE MESSAGE 2
我得到的输出是
Field error in object 'sampleForm' on field 'email': rejected value [jj];
codes [Email.sampleForm.email,Email.email,Email.java.lang.String,Email];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable:
codes [sampleForm.email,email]; arguments [];
default message [email],[Ljavax.validation.constraints.Pattern$Flag;@6679140b,^[^{}|]*$];
default message [not a well-formed email address]
任何人都可以