2

I'm trying to validate a form using Spring WebFlow and Hibernate Validator Annotations, but I'm doing something wrong.

If I use the Spring validation way (ValidationContext, MessageContext, validate${state}, etc), all works fine, but I want to use the Hibernate Annotations like @Email, @NotEmpty, etc.

I've read a lot of forums, Spring documentation, but I don't know what I'm doing wrong.

At this moment, this is my view-state and next action code:

<var name="user" class="myproject.web.pojo.User"/>

<view-state id="startSignup" model="user">
    <binder>
        <binding property="email" />
        <binding property="name" />
        <binding property="lastName" />
        <binding property="password" />
    </binder>
    <on-entry>
        <set name="flowScope.user" value="new info.teaming.web.pojo.User()" />
    </on-entry>
    <transition on="dataEntered" to="lookupUser" />
</view-state>

<action-state id="lookupUser">
    <evaluate expression="signupActions.lookupUser(user)" />
    <transition to="startSignup" on-exception="myproject.web.service.exception.UserAlreadyExistsException" />
    <transition to="saveOrder" />
</action-state>
    (...)

This is the startSignup.jsp code:

<!--  sf:form method="POST" modelAttribute="user" -->
<sf:form method="POST" commandName="user" >
<input type="hidden" name="_flowExecutionKey" value="${flowExecutionKey}" />
<fieldset>
    <table cellspacing="0">
        <tr>
            <th>
                <label for="name"><fmt:message key="signUp.name"/></label>
            </th>
            <td>
                <sf:input path="name" size="25" id="name" />
            </td>
            <sf:errors path="name" />
        </tr>
        <tr>
            <th>
                <label for="lastName"><fmt:message key="signUp.lastName"/></label>
            </th>
            <td>
                <sf:input path="lastName" size="45" maxlength="45" id="lastName" />
            </td>
        </tr>
        <tr>
            <th>
                <label for="password"><fmt:message key="signUp.password"/></label>
            </th>
            <td>
                <sf:password path="password" size="20" showPassword="true" id="password" />
            </td>
        </tr>
        <tr>
            <th>
                <label for="email"><fmt:message key="signUp.email"/></label>
            </th>
            <td>
                <sf:input path="email" size="30" id="email" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="_eventId_dataEntered" value="<fmt:message key="signUp.register"/>"/>
            </td>
        </tr>
    </table>
</fieldset>
 </sf:form>

This is the myproject.web.pojo.User POJO code:

import java.io.Serializable;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

public class User implements Serializable {

private static final long serialVersionUID = 1L;

@NotEmpty
@Length(min = 2, max = 25)  
private String name;  

@NotEmpty
@Email
private String email;  

@NotEmpty
@Length(min = 2, max = 45)  
private String lastName;  

@NotEmpty
@Length(min = 6, max = 20)
private String password;

public String getName() {  
    return name;  
}  

public void setName(String name) {  
    this.name = name;  
}  

public String getEmail() {  
    return email;  
}  

public void setEmail(String email) {  
    this.email = email;  
}  

public void setPassword(String password) {
    this.password = password;
}

public String getPassword() {
    return password;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getLastName() {
    return lastName;
}  

    //  public void validateStartSignup(ValidationContext context) {
    //      MessageContext messages = context.getMessageContext();
    //      
    //      messages.addMessage(new   MessageBuilder().error().source("name").code("user.name.notEmpty").build());
    //  }

  }

(note: as you can see, when I work with Spring Validation (uncomment the validate method), the form validates successfully)

And finally, this is my SignupAction code:

import myproject.web.model.UsrUser;
import myproject.web.model.dao.UsrUserDAO;
import myproject.web.pojo.User;
import myproject.web.service.exception.UserAlreadyExistsException;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Component;
import org.springframework.webflow.action.MultiAction;
import org.springframework.webflow.execution.Event;

@Component
public class SignupActions extends MultiAction {

@Resource
UsrUserDAO usrUserDAO;

private static final long serialVersionUID = 1L;

public Event lookupUser(@Valid User user) throws UserAlreadyExistsException {
    try {
        usrUserDAO.findByEmail(user.getEmail());
    } catch (javax.persistence.NoResultException e) {
        return success();
    }

    throw new UserAlreadyExistsException();
}

public void saveUser(UsrUser user) {
    return;
}

}

When I work with Hibernate Validation, the flow arrives to the saveUser method, without any validation.

I'm using Spring 3:

  • Spring 3.0.5.RELEASE
  • Spring WebFlow 2.2.1.RELEASE
  • Hibernate Validator 4.0.2.GA

What I'm doing wrong?

Thanks a lot for helping me! :)

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jordi
  • 21
  • 3

2 Answers2

0

have you defined your validator in Spring configuration XML file?

According to Spring Webflow reference it shoud be defined like this:

<webflow:flow-registry flow-builder-services="flowBuilderServices" />

<webflow:flow-builder-services id="flowBuilderServices" validator="validator" />

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
shimon001
  • 733
  • 9
  • 24
0

Try, Changing:

@Resource
UsrUserDAO usrUserDAO;

By:

@Autowired
private UsrUserManager UsrUserManager;

Using MVC manager.

And:

@Transactional
public Event lookupUser(@Valid User user) throws UserAlreadyExistsException { ...

as a Spring MVC controller, or reusing this MVC code if you already desing it ;)