Ich versuche, Spring's ReloadableResourceBundleMessageSource für LocalValidatorFactoryBean zu verwenden, so dass, wenn ich eine Fehlermeldung aktualisieren sollte, ohne dass der Server neu gestartet werden muss. Ich benutze Spring 4.1.4, Hibernate-Validator 4.3.2.Final. Im Folgenden sind die Codedetails -org.hibernate.validator.constraints nicht Kommissionierung reloaded Nachrichten
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 will override the below properties file-->
<value>/WEB-INF/application</value>
</list>
</property>
<property name="cacheSeconds" value="10"></property> <!-- Will check for refresh every 10 seconds -->
</bean>
<bean name="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="messageSource"/>
</property>
</bean>
Modell -
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";
}
// Do some backend validation with String
result.reject("string.not.valid",
"String is Invalid");
model.addAttribute("formModel", inputForm);
return "userInput";
}
application.properties (in/WEB_INF/Ordner)
required.string.blank=Please enter the required string.
string.not.valid=Please enter a valid string.
fileapplication.properties (in/conf/Ordner. Außer Kraft setzen über Datei)
required.string.blank=You did not enter the required string. #Does not reflect when I change here
string.not.valid=You did not enter a valid string. #Reflects when I change here
nun das Problem, das ich bin vor ist, wenn ich „string.not.valid“ in fileapplication.properties aktualisieren es zur Laufzeit reflektiert, und ich sehe die aktualisierte Meldung. Aber wenn ich ein Update „required.string.blank“ in fileapplication.properties es nicht zur Laufzeit widerspiegelt. Beachten Sie, dass das übergeordnete Teil für beide Nachrichten bei der Anwendung funktioniert prima starten. Aber der "Nachladen" Teil funktioniert nicht für "required.string.blank".