【问题标题】:Spring Web Flow LocalValidatorFactoryBean (JSR-349) does not work how is expectedSpring Web Flow LocalValidatorFactoryBean(JSR-349)无法按预期工作
【发布时间】:2014-10-06 19:39:14
【问题描述】:

我正在使用 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

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

直到这里一切都好..

注意:对于Spring MVC不需要进行关于LocalValidatorFactoryBean的特殊注入

问题在于 Spring Web Flow。

注意有必要Spring Web Flow做一个关于LocalValidatorFactoryBean的特殊注入

这里是它的配​​置:

@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 MVC 工作,另一个通过SWF 工作。 用于测试目的

好的问题:

再次,如果我输入一个无效值,我将如何得到 500

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

观察:我们看不到,无效数据(保留在 $validatedValue 中)和 value 仍然是静态的。

因此:

  • 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;
}

出了什么问题或遗漏了什么?

【问题讨论】:

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


    【解决方案1】:

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

    以上几点说明:

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

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

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

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

    考虑到这些,一切似乎都很好。

    【讨论】:

    • 谢谢罗森。让我测试一下,这个周末。
    猜你喜欢
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    • 2015-05-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 2013-06-20
    相关资源
    最近更新 更多