Question

I have the following formbean:

public class EmployeeForm extends org.apache.struts.action.ActionForm {

private int empid,age;

public int getEmpid() {
    return empid;
}

public void setEmpid(int empid) {
    this.empid = empid;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    if(age>20)
    this.age = age;

}

public String getEmpname() {
    return empname;
}

public void setEmpname(String empname) {
    this.empname = empname;
}

public String getPosition() {
    return position;
}

public void setPosition(String position) {
    this.position = position;
}
private String empname,position;

public EmployeeForm() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * This is the action called from the Struts framework.
 *
 * @param mapping The ActionMapping used to select this instance.
 * @param request The HTTP Request we are processing.
 * @return
 */
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
    if (getEmpname() == null || getEmpname().length() < 1) {
        errors.add("name", new ActionMessage("error.name.required"));
        // TODO: add 'error.name.required' key to your resources
    }
    return errors;
}
}

Now, I want to display user with an error when he enters an age less than 20. How to do that? Is there any possibility of throwing an error from the bean itself?

Following is the view that directs to ActionObject:

<html:errors/>
    <html:form action="EmployeeAction" >

        <table>
            <tr><td>Enter employee Id</td>
                <td><html:text name="EmployeeForm" property="empid" /></td>
            </tr>

            <tr><td>Enter employee Name</td>
                <td><html:text name="EmployeeForm" property="empname" /></td>
            </tr>
            <tr><td>Enter employee Age</td>
                <td><html:text name="EmployeeForm" property="age" /></td>
            </tr>
            <tr><td>Enter employee position</td>
                <td><html:text name="EmployeeForm" property="position" /></td>
            </tr>


            <tr><td align="right"><html:submit value="Add" property="method" /></td>
                <td> <html:submit property="method" value="Delete" />

            <html:submit property="method">Update</html:submit></td>
        </tr>


            </table>
    </html:form>
Was it helpful?

Solution

Define the helper method to get message resources

public static MessageResources getResources(HttpServletRequest request) {
  return ((MessageResources) request.getAttribute(Globals.MESSAGES_KEY));
}

then use resources

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
  try {
    MessageResources resources = getResources(request);
    ActionErrors errors = super.validate(mapping, request);
    if (getEmpname() == null || getEmpname().length() < 1) {
        errors.add("name", new ActionMessage("error.name.required"));
        throw new Exception(getResources(request).getMessage("error.name.required"));
    }
    return errors;
  } catch (Exception ex) {
    request.setAttribute(Globals.EXCEPTION_KEY, ex);
    return null;
  }
}

check the exception key in the request processor

// Check out if exception occurred
Exception exception = (Exception)request.getAttribute(Globals.EXCEPTION_KEY);
if (exception != null){
  ActionForward forward = processException(request, response, exception, form, mapping);
  processForwardConfig(request, response, forward);
  return;
}

OTHER TIPS

Firstly ,you can use JavaScript to validate.Secondly, you can use the method : validate() or validateXxxx() in the class EmployeeAction. Moreover,you can configure the XML file : EmployeeAction-Validation.xml.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top