I try to get working localization with custom hibernate messages but I can't make it work. My class that is validated looks like this:
@Entity
@Table(name = "USERS")
public class UserEntity extends AbstractBaseEntity implements UserDetails {
@NotNull
@Column(unique = true, nullable = false)
@Size(min = 5, max = 30)
private String username;
This is part of my spring configuration:
<!-- Localization of hibernate messages during validation!-->
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validation" />
</bean>
<bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="validationMessageSource"/>
</property>
</bean>
In resources I have two validations files: validation_en.properties and validation_pl.properties Here is example entry:
NotNull.UserEntity.username=Username can't be empty!
And when I am displaying validation errors I see standard message "may not be null" instead of my custom and localised one. Wham I am doing wrong? Thanks in advance for help, Best Regards
You need to do is @NotNull(message = "{error.username.required}")
@Entity
@Table(name = "USERS")
public class UserEntity extends AbstractBaseEntity implements UserDetails {
@NotNull(message = "{error.username.required}")
@Column(unique = true, nullable = false)
@Size(min = 5, max = 30)
private String username;
In validation_en.properties
properties file in your resource folder
error.username.required=Username can't be empty!
In spring configuration:
<mvc:annotation-driven validator="validator">
</mvc:annotation-driven>
<!-- Localization of hibernate messages during validation!-->
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validation" />
</bean>
<bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="validationMessageSource"/>
</property>
</bean>
Also leave a comment if this does not work for you. So I can help you further.
For anyone who has also problems to get the validator messages localized. Maybe this helps.
My property files for localization are located in src/main/resources/lang/
and are called validation.properties
The @EnableWebMvc
imports the Spring MVC configuration from org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport
and you can then override individual methods. (for more details see the API doc )
Add this to your Java Spring configuration file and your validation messages should get localized correctly:
@Configuration
@EnableWebMvc
@ComponentScan
public class MvcWebConfig extends WebMvcConfigurerAdapter {
// other configurations may be here ...
@Bean( name = "validationMessageSource" )
public ReloadableResourceBundleMessageSource validationMessageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:lang/validation");
messageSource.setCacheSeconds(10); // reload messages every 10 seconds
return messageSource;
}
@Override
public Validator getValidator() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.setValidationMessageSource((MessageSource) validationMessageSource());
return validator;
}
}
I think, that validationMessageSource
does not find the files:
When you pace the files in the src/main.webapp/WEB-INF
folder then the Remove the precursory /
( WEB-INF/validation
instead /WEB-INF/validation
):
When you place the files in the resource folder, then use the classpath
prefix: classpath:validation
for example:
<bean id="validationMessageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:validation" />
</bean>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.