hell_storm2004
hell_storm2004

Reputation: 1605

Can't get Form Validation working

I was learning Struts 1.1 and trying to do some form validation with my code.

But the errors that I had described in the MessageResources.properties file do not get displayed on the JSP. I tried a lot of options but couldn't get it off the ground. I have attached some of the code.

MessageResources.properties

error.name.required = Please mention your name.
error.email.incorrect = You E-Mail ID is Incorrect.
error.phone.numericError = Phone number should consist only of digits.
error.phone.lengthIncorrect = Phone number should be only of 10 digits.

struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
                           "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
 <form-beans>
     <form-bean name="detailsForm" type="com.example.form.DetailsForm"/>
 </form-beans>
 <action-mappings>
   <action input="/detailsEntry.jsp" name="detailsForm" path="/DetailsForm" type="com.example.action.DetailsAction" validate="true">
      <forward name="success" path="/displayDetails.jsp"/>
      <forward name="failure" path="/failure.jsp"/>
  </action>
  </action-mappings>
 </struts-config>

Form Class:

package com.example.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class DetailsForm extends ActionForm {

private String name;
private String email;
private String phone;

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 String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
    this.name = null;
    this.email = null;
    this.phone = null;
}

@Override
public ActionErrors validate(ActionMapping mapping,
        HttpServletRequest request) {
    ActionErrors actionErrors = new ActionErrors();

    if (this.name.equals(null) || this.name.length() == 0) {
        actionErrors.add("name", new ActionError("error.name.required"));
    }

    return actionErrors;
}

private boolean isNumeric(String phoneNumber) {
    try {
        Integer.parseInt(phoneNumber);
        return true;
    }
    catch (NumberFormatException numberFormatException) {
        return false;
    }
}
}

Upvotes: 1

Views: 1049

Answers (2)

blueMac
blueMac

Reputation: 41

Don't forget to add the following to your jsp:

<html:errors />

Your error messages will appear where ever you put this tag on on your jsp.

And if you want your error message to be displayed next to the field they relates to then use the following:

<html:errors property="custName" />

where "custName" is the name you gave the error message when you created it in your form ex:

ActionMessages errors = new ActionMessages();
errors.add("custName", new ActionMessage("custName.invalid"));
request.setAttribute(Globals.ERROR_KEY, errors);

Upvotes: 4

Dave Newton
Dave Newton

Reputation: 160181

The default resource filename is ApplicationResources.properties.

Using a different (or multiple) resource files requires configuration in struts-config.xml:

<message-resource parameter="MessageResources" null="false" />

Upvotes: 4

Related Questions