spring mvc - org.hibernate.validator.constraints not picking reloaded messages -
i trying use spring's reloadableresourcebundlemessagesource localvalidatorfactorybean when update error message should reflect without requiring server restarted. using spring 4.1.4, hibernate-validator 4.3.2.final. below code details -
context.xml -
<mvc:annotation-driven validator="validator" /> <bean id="messagesource" class="org.springframework.context.support.reloadableresourcebundlemessagesource"> <property name="basenames"> <list> <value>file:../conf/fileapplication</value> <!-- messages here override below properties file--> <value>/web-inf/application</value> </list> </property> <property name="cacheseconds" value="10"></property> <!-- check refresh every 10 seconds --> </bean> <bean name="validator" class="org.springframework.validation.beanvalidation.localvalidatorfactorybean"> <property name="validationmessagesource"> <ref bean="messagesource"/> </property> </bean>
model -
import org.hibernate.validator.constraints.notblank; public class inputform { @notblank ( message = "{required.string.blank}") string requiredstring;
controller -
@requestmapping(value = "/check/string", method = requestmethod.post) public string checkstring( @modelattribute("formmodel") @valid inputform inputform , bindingresult result, model model, httpservletresponse response, httpservletrequest request) { if (result.haserrors()) { model.addattribute("formmodel", inputform); return "userinput"; } // backend validation string result.reject("string.not.valid", "string invalid"); model.addattribute("formmodel", inputform); return "userinput"; }
application.properties (in /web_inf/ folder)
required.string.blank=please enter required string. string.not.valid=please enter valid string.
fileapplication.properties (in /conf/ folder. override above file)
required.string.blank=you did not enter required string. #does not reflect when change here string.not.valid=you did not enter valid string. #reflects when change here
now problem facing is, when update "string.not.valid" in fileapplication.properties reflects @ runtime , see updated message. when update "required.string.blank" in fileapplication.properties not reflect @ runtime. note overriding part working fine both messages upon application start up. "reloading" part not working fine "required.string.blank".
this figured out based on research - need create our own messageinterpolator , add dependency validator instead of message source. because when add messagesource dependency, cached default validator , message reloads spring won't take effect in validator's cached instance of messagesource.
below details:
in context.xml, add custom messageinterpolator dependency localvalidatorfactorybean instead of messagesource:
<mvc:annotation-driven validator="validator" /> <bean id="messagesource" class="org.springframework.context.support.reloadableresourcebundlemessagesource"> <property name="basenames"> <list> <value>file:../conf/fileapplication</value> <!-- messages here override below properties file--> <value>/web-inf/application</value> </list> </property> <property name="cacheseconds" value="10"></property> <!-- check refresh every 10 seconds --> </bean> <bean name="validator" class="org.springframework.validation.beanvalidation.localvalidatorfactorybean"> <property name="messageinterpolator"> <ref bean="messageinterpolator"/> </property> </bean> <bean name="messageinterpolator" class="com.my.org.support.mycustomresourcebundlemessageinterpolator"> <constructor-arg ref="messagesource" /> </bean>
create custom messageinterpolator extending hibernate's org.hibernate.validator.messageinterpolation.resourcebundlemessageinterpolator.
public class mycustomresourcebundlemessageinterpolatorextends resourcebundlemessageinterpolator { public mycustomresourcebundlemessageinterpolator(messagesource messagesource) { // passing false second argument // in super() constructor avoids messages being cached. super(new messagesourceresourcebundlelocator(messagesource), false); } }
model, controller , properties file can same in question.
Comments
Post a Comment