Spring Web Flow LocalValidatorFactoryBean(JSR-349)无法正常工作

时间:2014-10-06 19:39:15

标签: spring-mvc spring-webflow spring-3 spring-webflow-2 spring-4

我正在使用Spring 4.0.7和Spring Web Flow 2.4.0

我有以下内容(我正在使用JSR 349):

对于实体:

@Min(value=1, message="{person.age.min}",groups={PersonRegistrationCheck.class})
@Max(value=115, message="{person.age.max}",groups={PersonRegistrationCheck.class})
@NotNull(message="{field.null}",groups={PersonRegistrationCheck.class})
@Column(name="age", nullable=false, length=3)
@XmlElement
public Integer getAge() {
    return age;
}

ValidationMessages.properties档案

person.age.max = Invalid data '${validatedValue}', the maximum value allowed is {value}
person.age.min = Invalid data '${validatedValue}', the minimum value allowed is {value}

在某些@Configuration

@Bean
public LocalValidatorFactoryBean localValidatorFactoryBean(ReloadableResourceBundleMessageSource messageSource){

    LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
    localValidatorFactoryBean.setValidationMessageSource(messageSource);

    MessageSourceResourceBundleLocator msrbl = new MessageSourceResourceBundleLocator(messageSource); 
    ResourceBundleMessageInterpolator rbmi = new ResourceBundleMessageInterpolator(msrbl);
    localValidatorFactoryBean.setMessageInterpolator(rbmi);

    return localValidatorFactoryBean;
}

通过Spring MVC,它可以完美地运行:

<tr>
    <td><spring:message code="person.form.age"/></td>
    <td><form:input path="age" size="40"/></td>
    <td><form:errors path="age" cssClass="error"/></td>
</tr>

如果我把无效值设为500我

  

无效数据500,允许的最大值为115

我可以看到无效数据和允许的最大值。

直到这里一切都好......

注意必须对LocalValidatorFactoryBean

Spring MVC进行特殊注入

问题在于Spring Web Flow。

注意必要LocalValidatorFactoryBean

Spring Web Flow进行特殊注入

这里是它的配​​置:

@Autowired
private LocalValidatorFactoryBean localValidatorFactoryBean;

@Bean
public FlowBuilderServices flowBuilderServices() {
    return getFlowBuilderServicesBuilder()
            .setViewFactoryCreator(mvcViewFactoryCreator())
            .setConversionService(conversionService())              
            .setValidator(localValidatorFactoryBean)
            .setDevelopmentMode(true)   
            .build();
}

根据spring-webflow-samples / WebFlowConfig

,上面几乎几乎相同

在流程定义中

<view-state id="start" 
        view="person.flow.form.register" 
        model="person"
        validation-hints="'com.manuel.jordan...PersonRegistrationCheck'"  >
    <transition on="submit" to="address" bind="true" validate="true" >
        <evaluate expression="personAction.savePerson(flowRequestContext)" />
    </transition>
    <transition on="cancel" to="end" bind="false" validate="false"/>
</view-state>

再次出现在.jsp文件中

<tr>
    <td><spring:message code="person.form.age"/></td>
    <td><form:input path="age" size="40"/></td>
    <td><form:errors path="age" cssClass="error"/></td>
</tr>

注意:它是其他或新的.jsp文件,它适用于Spring Web Flow。           因此,Spring MVCSWF之间需要处理value。           它用于测试目的

好的问题:

再次如果我将无效值设为500我

  

无效数据$ validatedValue,允许的最大值为值

观察:我们无法看到,无效数据(保留为$ validatedValue)和Spring MVC仍然是静态的。

因此:

  • Invalid data 500, the maximum value allowed is 115显示:SWF
  • Invalid data $validatedValue, the maximum value allowed is value显示:@Bean public LocalValidatorFactoryBean localValidatorFactoryBean(ReloadableResourceBundleMessageSource messageSource){ LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean(); localValidatorFactoryBean.setValidationMessageSource(messageSource); //MessageSourceResourceBundleLocator msrbl = new MessageSourceResourceBundleLocator(messageSource); //ResourceBundleMessageInterpolator rbmi = new ResourceBundleMessageInterpolator(msrbl); //localValidatorFactoryBean.setMessageInterpolator(rbmi); return localValidatorFactoryBean; }

注意:即使使用以下内容,也都会出错。

{{1}}

有什么问题或缺失?

1 个答案:

答案 0 :(得分:0)

使用booking-mvc示例我可以按预期确认所有工作。我尝试了ValidationMessages.properties和默认的LocalValidatorFactoryBean(即没有自定义插补器,只依赖于Hibernate验证器)以及“/WEB-INF/messages.properties”和一个指向ResourceBundleMessageSource的自定义ResourceBundleMessageInterpolator。

以上几点说明:

  • LocalValidatorFactoryBean上的setValidationMessageSource和setMessageInterpolator是互斥的。 javadoc清楚地说明了这一点。

  • ValidationMessages.properties是默认的(JSR-303)bean验证机制。设置自定义MessageInterpolator后,不再使用ValidationMessages.properties。

  • 支持$ {validatedValue}似乎是Hibernate验证器版本5+可用的东西,即它在4.3中不可用。

  • 在Web Flow中
  • 我们还会查看flow目录中的messages.properties。需要确保没有任何错误代码覆盖JSR-303已解决的违规消息。

考虑到这一切似乎一切正常。