无法理解为何Spring Web Flow验证无法正常工作

时间:2019-03-04 19:35:56

标签: java spring hibernate spring-webflow

我已经根据this视频使用Spring Web Flow对我的注册流程进行了验证,但是我不明白为什么它不起作用。我有验证豆:

 @Bean
public LocalValidatorFactoryBean validatorFactoryBean(){
    return new LocalValidatorFactoryBean();
}

我也将其添加到流构建器中:

 @Bean
public FlowBuilderServices flowBuilderServices() {
    return getFlowBuilderServicesBuilder().setValidator(new LocalValidatorFactoryBean())
            .setViewFactoryCreator(mvcViewFactoryCreator()).setValidator(validatorFactoryBean())
            .setDevelopmentMode(true).build();
}

我使用Hibernate作为ORM,所以我将Hibernate Validator添加到pom.xml

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.4.1.Final</version>
    </dependency>

我的流程代码是:

  <var name="user" class="entity.User"/>
 <input name="data" type="java.lang.Boolean"/>
 <view-state id="registratingForm" view="/WEB-INF/pages/registration.jsp" 
 model="user" >
<on-entry>
<evaluate expression="orderFlowService.check()" result="data" />
 </on-entry>
    <transition on="submit" to="saveUser" validate="true" >
        <evaluate expression="userServiceImpl.validateImg(requestParameters.photos,user)" result="user" />
    </transition>

</view-state>

<decision-state id="checking" >
    <if test="orderFlowService.check()" then="fromBaseFlow" 
else="userRegistrated"  />
</decision-state>

<action-state id="saveUser">
    <evaluate expression="userServiceImpl.add(user)"/>
    <transition to="checking" />
</action-state>
<end-state id="userRegistrated" view="/index.jsp">

</end-state>
<end-state id="fromBaseFlow" view="externalRedirect:contextRelative:/base- 
flow" />
</flow>

我的实体代码:

  @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idUsers")
private int id;
@Column(name = "login")
@Size(min = 3, max = 15,message = "Неправильний розмір")
@NotEmpty(message = "Не може бути пустим!")
private String login;
@Size(min = 6,message = "Неправильний розмір")
@NotEmpty(message = "Не може бути пустим!")
@Column(name = "password")
private String password;
@NotNull(message = "Не може бути пустим!")
@Column(name = "email")
private String email;
@Column(name = "photo")
private String path;
@Column(name = "about")
private String about;

我的JSP代码:

  <form:form modelAttribute="user" enctype="multipart/form-data">
    <table>
        <tr>
            <td><form:label path="login">Логін:</form:label></td>
            <td><form:input path="login"/> <form:errors path="login" cssClass="isa_error"/>   <sf:errors path="login" cssClass="isa_warning" /></td>
        </tr>
        <tr>
            <td><form:label path="password">Пароль:</form:label></td>
            <td><form:password path="password"></form:password><form:errors path="password" cssClass="isa_error" /> </td>
        </tr>
        <tr>
            <td><form:label path="email">Пошта(електронна):</form:label></td>
            <td><form:input path="email"/> <form:errors path="email" cssClass="isa_error"/></td>

        </tr>
        <tr>
          <td><form:label path="about">Опис</form:label></td>
            <td><textarea name="about" cols="40" rows="10">Введіть опис сюди!</textarea></td>
        </tr>
        <tr>
            <td>Аватар:</td>
            <td><input  id="photos" name="photos" type="file" ></td>

        </tr>
        <tr>
            <input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}"/>
            <td><input  type="submit" value="Додати" name="_eventId_submit" ></td>
        </tr>
      <tr>
          <td><form:errors cssClass="isa_error" delimiter=""/> </td>

      </tr>
    </table>
<p>
    <a href="/index.jsp" class="button">На головну!</a>

</p>

如果我将输入留空,则会发生此异常:

 javax.validation.ConstraintViolationException: Validation failed for classes 
[entity.User] during persist time for groups 
[javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='Неправильний розмір', 
propertyPath=login, rootBeanClass=class entity.User, 
messageTemplate='Неправильний розмір'}
ConstraintViolationImpl{interpolatedMessage='Не може бути пустим!', propertyPath=login, rootBeanClass=class entity.User, messageTemplate='Не може бути пустим!'}
]

我的验证必须像上面的视频一样将我重定向到当前页面,并显示诸如('Ca n't null null')之类的消息,但它会引发带有验证错误的异常。

0 个答案:

没有答案