简体   繁体   中英

Hibernate Custom Validator Messages Spring MVC

I am trying to customize messages for Hibernate validation annotations (NOTNULL , NOTEMPTY ,...) so that the application is able to read them from .proparties file.

I tried the solutions from the answer here: use custom validation in hibernate hibernate validator

but nothing works for me i still get the deafult messages (may not be null , may not be empty , ....).

Code sample :

application context.xml :

<bean name="validator"
    class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
    <property name="validationMessageSource">
        <ref bean="resourceBundleLocator" />
    </property>
</bean>

<bean name="resourceBundleLocator"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>messages</value>
        </list>
    </property>
</bean>

user.java :

@NotNull
private String name;

messages.proparties :

user.name.notNull = incorrect name

You need to mention the key name on your @NotNull annotation.

@NotNull(meassage="{user.name.notNull}")
private String name;

You can learn more from the link https://teamtreehouse.com/library/displaying-validation-messages

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM