我想为LocalValidatorFactoryBean使用Spring的ReloadableResourceBundleMessageSource,这样当我更新错误消息时,它应该反映而不需要重新启动服务器。我使用Spring 4.1.4,hibernate-validator 4.3.2.Final。 下面是代码的细节 -org.hibernate.validator.constraints不挑选重新加载的消息
的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>
模型 -
import org.hibernate.validator.constraints.NotBlank;
public class InputForm {
@NotBlank (message = "{required.string.blank}")
String requiredString;
控制器 -
@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(在/ WEB_INF /文件夹)
required.string.blank=Please enter the required string.
string.not.valid=Please enter a valid string.
fileapplication.properties(in/conf/folder。将覆盖上面的文件)
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
现在我面临的问题是,当我在它反映了在运行时fileapplication.properties更新“string.not.valid”,我看到了更新的消息。但是,当我更新fileapplication.properties中的“required.string.blank”时,它不会在运行时反映出来。 请注意,应用程序启动时,覆盖部分对于这两个消息都正常工作。但“重新加载”部分对于“required.string.blank”无法正常工作。