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.


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=""  prefix="html"%>
<%@ taglib uri=""  prefix="bean"%>
<html:form action="/registration" styleId="registrationForm">
    <div style="color: red;" align="center">
        <html:errors />
    <table align="center">
            <td><bean:message key="registration.firstname" /></td>
            <td><html:text property="firstname"></html:text></td>
            <td><bean:message key="registration.lastname" /></td>
            <td><html:text property="lastname"></html:text></td>
            <td><bean:message key="registration.gender" /></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>
            <td><bean:message key="" /></td>
            <td><html:text property="email"></html:text></td>
            <td><bean:message key="" /></td>
            <td><html:text property="mobile"></html:text></td>
            <td><bean:message key="registration.address" /></td>
            <td><html:text property="address"></html:text></td>
            <td><bean:message key="registration.username" /></td>
            <td><html:text property="username"></html:text></td>
            <td><bean:message key="registration.password" /></td>
            <td><html:password property="password"></html:password></td>
            <td><html:submit property="submit">Submit</html:submit></td>
            <td><html:reset property="reset">Reset</html:reset></td>

You need to update the 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 file.

The validate () method looks like below code.

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(""));
    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(""));
    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;

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

error.firstname.required=First name is required
error.lastname.required=Last name is required
error.gender.required=Gender is required no is required 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 will be called.


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 path="/registration" name="registrationForm" input="/registration.jsp" validate="true"
        type="" scope="request">
        <forward name="success" path="/registrationSuccess.jsp" />

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.