Spring + Hibernate Validator:更改默认消息(不工作)

时间:2014-07-02 09:59:04

标签: java spring hibernate spring-mvc hibernate-validator

所以我想让自定义默认消息正常工作,就像已经多次询问(并解决)了一样,但我仍然无法让它工作。

我的代码到目前为止:

应用程序,JAVA

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {

private static Class<Application> applicationClass = Application.class;

public static void main(String[] args) {
    SpringApplication.run(applicationClass, args);
}

@Override
protected SpringApplicationBuilder configure(
        SpringApplicationBuilder application) {
    return application.sources(applicationClass);
}

@Bean(name = "messageSource")
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
    source.setBasename("WEB-INF/messages/messages");
    source.setDefaultEncoding("UTF-8");

    return source;
}

@Bean(name = "validator")
public LocalValidatorFactoryBean validator() {
    LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
    validatorFactoryBean.setValidationMessageSource(messageSource());
    return validatorFactoryBean;
}



}

现在

messageSource.getMessage(result.getFieldError("projectName"), null)

将正常工作,我收到自定义错误消息。

但是

result.getFieldError("projectName").getDefaultMessage()

仍会输出标准默认消息。

我怎样才能让它发挥作用? :)

0 个答案:

没有答案