Struts2 Validation Using XML

Struts 2 validation using XML example.

UserAction.java

package classes;
    package action;

    import com.opensymphony.xwork2.ActionSupport;
    import java.util.Date;
    public class UserAction extends ActionSupport {
        private String userName;
        private String password;
        private String email;
        private Integer age;
        private Date bday;

        public Date getBday() {
            return bday;
        }

        public void setBday(Date bday) {
            this.bday = bday;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String login() {

            // TODO: login check

            return SUCCESS;
        }

        public String getEmail() {
            return email;
        }

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

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

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }
    }

UserAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE validators PUBLIC
          "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
          "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
    <field name="userName">
        <field-validator type="requiredstring">
            <message>Username Required</message>
        </field-validator>
    </field>
    <field name="password">
        <field-validator type="requiredstring">
            <message>Password Required</message>
        </field-validator>
    </field>
    <field name="email">
        <field-validator type="email">
            <message>Invalid Email</message>
        </field-validator>
    </field>
    <field name="age">
        <field-validator type="required">
            <message>Age Is Required</message>
        </field-validator>
        <field-validator type="int">
            <param name="min">18</param>
            <param name="max">25</param>
            <message>Invalid Age</message>
        </field-validator>
    </field>
    <field name="bday">
        <field-validator type="date">
            <param name="min">12/31/2000</param>
            <param name="max">12/31/2010</param>
            <message>Invalid Date Range</message>
        </field-validator>
    </field>

</validators>

1 comment:

  1. Not see the image name and the price in the shopping cart in its place is the id
    I'm working with struts2 here I show the codes:
    Link is : http:// matf5000.wix.com/codigo_java

    ReplyDelete