Validating form data using validate method of form bean tutorial

In this tutorial you will learn how to validate the form data using struts inbuilt feature, we will use the same registration application example and validate the registration data.

We will update below files in registration example in order to validate the registration form data.

Registeration.jsp

We need to put the tags inside the registeration.jsp page, this tag used to display the errors which are stored in ActionErrors object.

 
 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"  prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"  prefix="bean"%>
 
<html:form action="/registration" styleId="registrationForm">
    <div style="color: red;" align="center">
        <html:errors />
    </div>
 
    <table align="center">
        <tr>
            <td><bean:message key="registration.firstname" /></td>
            <td><html:text property="firstname"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.lastname" /></td>
            <td><html:text property="lastname"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.gender" /></td>
            <td>
                <html:select property="gender">
                    <html:option value="">Select</html:option>
                    <html:option value="MALE">Male</html:option>
                    <html:option value="FEMALE">Female</html:option>
                </html:select>
            </td>
        </tr>
        <tr>
            <td><bean:message key="registration.email" /></td>
            <td><html:text property="email"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.mobile" /></td>
            <td><html:text property="mobile"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.address" /></td>
            <td><html:text property="address"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.username" /></td>
            <td><html:text property="username"></html:text></td>
        </tr>
        <tr>
            <td><bean:message key="registration.password" /></td>
            <td><html:password property="password"></html:password></td>
        </tr>
        <tr>
            <td><html:submit property="submit">Submit</html:submit></td>
            <td><html:reset property="reset">Reset</html:reset></td>
        </tr>
    </table>
</html:form>
 
 
 

RegistrationForm.java

You need to update the RegistrationForm.java class to override the validate () method, which returns the ActionErrors object.

We are checking all the fields of RegistrationForm for null or blank, and putting the error message in ActionErrors object. The error messages are configured inside ApplicationResources.properties file.

The RegistrationForm.java validate () method looks like below code.

 
 
@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    if (firstname == null || firstname.length() < 1) {
        errors.add("firstname", new ActionMessage("error.firstname.required"));
    }
    if (lastname == null || lastname.length() < 1) {
        errors.add("lastname", new ActionMessage("error.lastname.required"));
    }
    if (email == null || email.length() < 1) {
        errors.add("email", new ActionMessage("error.email.required"));
    }
    if (gender == null || gender.length() < 1) {
        errors.add("gender", new ActionMessage("error.gender.required"));
    }
    if (mobile == null || mobile.length() < 1) {
        errors.add("mobile", new ActionMessage("error.mobile.required"));
    }
    if (address == null || address.length() < 1) {
        errors.add("address", new ActionMessage("error.address.required"));
    }
    if (username == null || username.length() < 1) {
        errors.add("username", new ActionMessage("error.username.required"));
    }
    if (password == null || password.length() < 1) {
        errors.add("password", new ActionMessage("error.password.required"));
    }
    return errors;
}
 
 
 

ApplicationResources.properties

We need to put the error message inside ApplicationResources.properties file, the keys inside ApplicationResources.properties used in validate() method of RegistrationForm.java class to add a new ActionMessage into ActionErrors object.

 
 
errors.prefix=<li>
errors.suffix=</li>
errors.header=<ul>
errors.footer=</ul>
 
error.firstname.required=First name is required
error.lastname.required=Last name is required
error.gender.required=Gender is required
error.mobile.required=Mobile no is required
error.email.required=E-Mail is required
error.address.required=Address is required
error.username.required=Username is required
error.password.required=Password is required
 
 
 

If the ActionErrors object is not empty, than the struts control the returned back to the input page, which is mentioned in action mapping of struts-config.xml. The input page should have tag, which used to display the error message in the page. Once there is no error message inside ActionErrors object, the corresponding action RegistrationAction.java will be called.

Struts-config.xml

In order to validate the form data and displaying the error messages we need to update the action tag of struts configuration as below.

 
 
<action-mappings>
    <action path="/registration" name="registrationForm" input="/registration.jsp" validate="true"
        type="com.java.struts.action.RegistrationAction" scope="request">
        <forward name="success" path="/registrationSuccess.jsp" />
    </action>
</action-mappings>
 
 
 

You need to put validate attribute as true value.

You need to put input attribute which value can be any JSP page that has one tag as . This tag used to display the error message in page.