Spring Webflow - 无法在模型bean上调用注释验证

时间:2015-01-31 08:55:50

标签: java spring validation spring-webflow spring-webflow-2

请参阅下面的代码段。我的流程从显示带有3个字段的简单JSP开始。当我提交表单时,我希望通过表单bean上的注释配置的验证(JSR-303)启动并显示错误消息。但那并没有发生。该页面将提交给searchActions.findExistingPlayer方法。任何指针都会有所帮助。

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Source project: sip05, branch: 03 (Maven Project) -->

<flow xmlns="http://www.springframework.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/webflow
    http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"
    start-state="findExistingPlayerForm">

    <view-state id="findExistingPlayerForm">
        <on-render>
            <evaluate expression="findExistingPlayerFormAction.setupForm"></evaluate>
        </on-render>
        <transition on="find" to="findExistingPlayerFormActionState">
            <evaluate expression="findExistingPlayerFormAction.bindAndValidate"></evaluate>
        </transition>
    </view-state>

    <action-state id="findExistingPlayerFormActionState">
        <evaluate expression="searchActions.findExistingPlayer"></evaluate>
        <transition on="success" to="displayFindExistingPlayerResult"></transition>
    </action-state>
    <view-state id="displayFindExistingPlayerResult">
        <<---Some transitions-->>>
    </view-state>
    <end-state id="newSearchEndState" />

</flow>

动作映射

<bean id="findExistingPlayerFormAction" class="org.springframework.webflow.action.FormAction">
        <property name="formObjectClass"
            value="com.saurabhd.springwebflow.form.PlayerSearchForm" />
    </bean>

形式:

public class PlayerSearchForm implements Serializable{
private static final long serialVersionUID = 1L;


    private String firstName;

    private String lastName;
    private String homePhone;

    @NotEmpty
    @Size(min=10)
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    @NotNull
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @NotBlank
    public String getHomePhone() {
        return homePhone;
    }
    public void setHomePhone(String homePhone) {
        this.homePhone = homePhone;
    }
}

网络流量背景:

<flow:flow-builder-services id="flowBuilderServices"
        development="true" 
        validator="validator"/>
<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

JSP

<%-- Source project: sip05, branch: 03 (Maven Project) --%>
<%@ include file="/WEB-INF/jsp/taglibs.jspf" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html 
  xmlns:c="http://java.sun.com/jsp/jstl/core"
  xmlns:jsp="http://java.sun.com/JSP/Page"
  xmlns:spring="http://www.springframework.org/tags"
  xmlns:form="http://www.springframework.org/tags/form">

  <head><title>Find Existing Player(s)</title></head>

  <body>
    <h2>Search</h2>          
    <p>
      Please fill the info below:
    </p>

    <form:form commandName="playerSearchForm" action="${flowExecutionUrl}">         
      <label for="firstname">Player First Name</label>
      <form:input path="firstName" /><br/>
      <form:errors path="firstName"/> <br/><br/>

      <label for="lastName">Player Last Name</label>
      <form:input path="lastName" /><br/>
      <form:errors path="lastName"/> <br/><br/>



      <label for="homePhone">Home Phone:</label>
      <form:input path="homePhone" /><br/>
      <form:errors path="homePhone"/> <br/><br/>

      <input type="submit" name="_eventId_skip"
        value="Skip"/>
      <input type="submit" name="_eventId_find"
        value="Find"/>            
    </form:form>

  </body>
</html>

更新 - 我还尝试了视图状态下的模型绑定。它也无法在我的bean上调用JSR-303注释验证。 :(

到目前为止,有效的是自定义验证$ {view-state-id} 方法。见下文:

public void validateFindExistingPlayerForm(ValidationContext context){
        MessageContext messages = context.getMessageContext();
        if(StringUtils.isEmpty(firstName)){
            messages.addMessage(new MessageBuilder().error().source("firstName").
                    defaultText("Please enter the value for First Name.").build());
        }
    }

Flow.xml

<var name="playerSearchForm" class="com.saurabhd.springwebflow.form.PlayerSearchForm"/>

    <view-state id="findExistingPlayerForm" model="playerSearchForm">
        <transition on="find" to="findExistingPlayerFormActionState">
        </transition>
    </view-state>

1 个答案:

答案 0 :(得分:0)

我注意到您尚未在view-state上正确定义模型。在定义模型时,必须将正在使用的模型实例设置为流范围或视图范围。

您正在做的是在流程中创建PlayerSearchForm的新实例并绑定到view-state。这不起作用,因为您必须绑定正在视图上工作的实例。

因此,您有两种方法可以传递对象的引用。

第一种方式:

//You can set objects into the flow scope, like this. 
RequestContextHolder.getRequestContext().getFlowScope().put("playerSearchForm", instanceOfPlayerSearchForm);

第二种方式:

<evaluate expression="findExistingPlayerFormAction.getPlayerSearchForm()" result="flowScope.playerSearchForm"/>


public PlayerSearchForm getPlayerSearchForm(){
   return instanceOfPlayerSearchForm;
}