You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Filipe David Manana <fd...@ieee.org> on 2008/01/24 18:33:52 UTC

Struts2 Annotations Validation of an Object's property

Hi,

I have an action that has an object of type "User" as a property. I
have a jsp that has a form with several fields, each one targets a
property of this User object.
How can I use annotations to validate these fields?


public class User {
  private String name;
  private String email;

  // getters and setters...
}

public class MyAction extends ActionSupport {
  private User user = new User();

  public User getUser() {
    return this.user;
  }
}

With the xml solution I would have:

 <field name="user.name">
     <field-validator type="requiredstring">
          <message key="errors.required.name"/>
      </field-validator>
  </field>
 <field name="user.email">
     <field-validator type="email">
          <message key="errors.required.email"/>
      </field-validator>
  </field>


How can I use annotations to accomplish the same thing?
For simple properties I just do something like this before the setter
method: @RequiredStringValidator(message = "Please enter your name",
trim = true)

cheers

-- 
Filipe David Manana,
fdmanana@ieee.org

Obvious facts are like secrets to those not trained to see them.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org