【问题标题】:Spring Webflow - Unable to invoke annotation validation on a model beanSpring Webflow - 无法在模型 bean 上调用注释验证
【发布时间】:2015-03-30 18:06:26
【问题描述】:

请看下面的代码 sn-p。我的流程从显示具有 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;
    }
}

Web 流上下文:

<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 注释验证。 :(

到目前为止起作用的是自定义 validate${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>

【问题讨论】:

  • 伙计们,有人可以在这里提供帮助和建议。
  • @WillieWheeler 你能帮忙吗?

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


【解决方案1】:

我注意到您没有在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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    相关资源
    最近更新 更多