Spring custom validation message -
custom validation messages don't work. have domain class:
... @roojpaactiverecord public class letter { @notnull(message="{validation.number_empty}") @size(min=1,max=20, message="{validation.number_size}") @column(length=20, nullable=false) private string number; ... }
/web-inf/i18n/messages.properties:
#validation validation.number_size=number must more 1 , less 20 symbols ...
i want validate fields domain class during form submitting. validation works, output message:
{validation.number_size}
instead of expected string: number must more 1 , less 20 symbols in other places of project use messages properties files success.
/web-inf/spring/webmvc-config.xml
<bean class="org.springframework.context.support.reloadableresourcebundlemessagesource" id="messagesource" p:basenames="web-inf/i18n/messages,web-inf/i18n/application" p:fallbacktosystemlocale="false"> </bean>
also, i’ve tried , without success:
<bean class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> <property name="locations"> <list> <value>classpath:web-inf/i18n/messages</value> </list> </property> <property name="ignoreunresolvableplaceholders" value="true"/> <property name="ignoreresourcenotfound" value="true"/> </bean>
you need additional config customize error messages using messagesource.
try following:
/web-inf/spring/webmvc-config.xml:
... <bean id="validator" class="org.springframework.validation.beanvalidation.localvalidatorfactorybean"> <property name="validationmessagesource" ref="messagesource" /> </bean>
in controller class:
... import org.springframework.validation.validator; ... @autowired private validator validator; @initbinder protected void initbinder(webdatabinder binder) { binder.setvalidator(validator); }
see this section of reference manual details.
Comments
Post a Comment