Spring Download Verification Not Allowed

I am having trouble receiving my confirmation message.

I searched and read over the Internet and SO for several hours, I want to associate a question with a marked answer Configure spring validation error

I have a MessageSource bean, and message.properties reads it correctly, since I also use it for plain text, which will be displayed using th:text="#{some.prop.name} , which works absolutely fine. It's just a validation error that will not work as it should.I am sure that this is a stupid error that I just forget ... The verification itself works reliably.

Constraint:

 @NotEmpty(message="{validation.mail.notEmpty}") @Email() private String mail; 

messages.properties:

 # Validation validation.mail.notEmpty=The mail must not be empty! 

Part of the template:

 <span th:if="${#fields.hasErrors('mail')}" th:errors="*{mail}"></span> 

Display Text:

 {validation.mail.notEmpty} 

I tried many variations, all without success.

 @NotEmpty(message="validation.mail.notEmpty") @NotEmpty(message="#{validation.mail.notEmpty}") 

Will show the exact value of the message line, indiscriminately.

 <span th:if="${#fields.hasErrors('mail')}" th:errors="${mail}"></span> <span th:if="${#fields.hasErrors('mail')}" th:errors="#{mail}"></span> <span th:if="${#fields.hasErrors('mail')}" th:errors="#{*{mail}}"></span> <span th:if="${#fields.hasErrors('mail')}" th:errors="#{__*{mail}__}"></span> 

will result in an error.


EDIT:

After debugging, I came across this:

Class: org.springframework.context.support.MessageSourceSupport

Method: formatMessage(String msg, Object[] args, Locale locale)

will be called using

formatMessage("{validation.mail.notEmpty}", null, locale /*German Locale*/)

And it will run in if (messageFormat == INVALID_MESSAGE_FORMAT) {

So ... my message format is incorrect. This is a way out of my volume / knowledge. Does anyone know what that means?

+7
java spring spring-boot thymeleaf
source share
1 answer

It looks like your LocalValidatorFactoryBean definition is missing from your application LocalValidatorFactoryBean . Below you can find an example of the Application class that defines two beans: LocalValidatorFactoryBean and MessageSource , which uses the messages.properties file.

 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; @SpringBootApplication public class Application { @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename("classpath:messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } @Bean public LocalValidatorFactoryBean validator() { LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean(); bean.setValidationMessageSource(messageSource()); return bean; } public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

LocalValidatorFactoryBean defining a LocalValidatorFactoryBean bean, you can use a special message to check, for example:

 @NotEmpty(message = "{validation.mail.notEmpty}") @Email private String email; 

and messages.properties :

 validation.mail.notEmpty=E-mail cannot be empty! 

and Thymeleaf template file with:

 <p th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Name Error</p> 

Application example

https://github.com/wololock/stackoverflow-answers/tree/master/45692179

I prepared a sample Spring Boot application that reflects your problem. Feel free to clone it and run it locally. It will display the translated validation message if the value submitted with the form does not match @NotEmpty and @Email .

WebMvcConfigurerAdapter Configuration

In the case of the WebMvcConfigurerAdapter extension WebMvcConfigurerAdapter you will need to provide a validator by overriding the getValidator() method from the parent class, for example:

 import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration @EnableWebMvc public class WebConfiguration extends WebMvcConfigurerAdapter { @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setBasename("classpath:messages"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } @Bean @Override public Validator getValidator() { LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean(); bean.setValidationMessageSource(messageSource()); return bean; } // other methods... } 

Otherwise, if you define a LocalValidatorFactoryBean bean, it will be overridden elsewhere and the effect will not.

Hope this helps.

+14
source share

All Articles