facebook

Struts Error Handling with struts:error tag

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #297690 Reply

    ionEyes
    Member

    Hi there,

    I am not too sure whether to post this as I am not sure if this is a Struts issue or myEclipse issue or me. Here goes.

    I have a struts action and form page. Within the Form object I have a validation of form fields. that I was hoping would cause the struts:error tag to operate to reveal form errors. I am using struts 1.3, with myEclipse 6 on tomcat v6. The debugger and println statements reveals the validate method is being called and I assume the correct behaviour – namely filling out the ActionErrors object and returning it.

    That validate method is as follows.:

    
        public ActionErrors validate(ActionMapping mapping,
                HttpServletRequest request) {
            // TODO Auto-generated method stub
            System.out.println("validate called on form : " + mapping.getName());
    
            ActionErrors errors = new ActionErrors();
    
            if (getContactName() == null || getContactName().length() < 1) {
                errors.add("contactName", new ActionMessage("a contact name is required"));
            }
            if (getContactEmail() == null || getContactEmail().length() < 1) {
                errors.add("contactEmail", new ActionMessage(
                        "a valid email address is required"));
            }
            
    //        System.out.println(errors.toString());
    
            return errors;
        }
    

    The corresponding JSP code looks as follows:

    
    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
    <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
    <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
    
            <html:form action="/newBusiness">
                <table title="Contact Details" align="left">
                    <tr align="left">
                        <td>
                            Name
                        </td>
                        <td>
                            <html:text property="contactName" />
                        </td>
                        <td>
                            <html:errors property="contactName" />
                        </td>
                    </tr>
                </table>
    

    – shortened for brevity.

    I can place the validation in the ActionObjects execute() method and try and do it all manually – which is very inefficient and leads to lots of code in the JSP to retrieve the error object from the session or request and then perform the revelation of errors on the page.

    I was hoping to use the struts:error tag – taken from examples online – and in the above example in the manner I have to reveal the errors.

    Is there something I have missed – or something obviously wrong ?

    I have not used resource bundles and externalised strings as its a simple site, that does not require internationalisation.

    Can anyone explain why html:errors in the JSP above is not revealing the errors that the field validation is coded for ?

    Thank you.

    #297702 Reply

    support-joy
    Member

    ionEyes,

    if (getContactName() == null || getContactName().length() < 1) {
    errors.add(“contactName”, new ActionMessage(“a contact name is required”));
    }

    you should place that String value(“a contact name is required”) as a key’s value in the properties file and pass that key name as the parameter for new ActionMessage()

    #297753 Reply

    ionEyes
    Member

    Thank you for getting back to me so quickly Joy.

    Must I place the string value in the property file as well as defining the error message here ?
    Does this mean there is no way to avoid the property file ?

    I assumed (possibly wrongly) that I could kind of get away with this definition of the error message only. I will admit I am a bit confused now why there is the ability to specify an error with a new ActionMessage object in the validate method ?

    Thank you in advance.

    #297776 Reply

    support-joy
    Member

    ionEyes,
    i would like to know if the solution works for you. As per Struts Framework, you need to place the key value pairs in a properties file as shown below
    key = a contact name is required
    and use the key as follows in your validate() method
    errors.add(“contactName”, new ActionMessage(“key”));

    This doesn’t seem to be a MyEclipse problem, rather it is related to coding. I would recommend you to crosspost your queries to Struts forums to get alternate solutions.

    #297812 Reply

    ionEyes
    Member

    Thank you Joy.

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Struts Error Handling with struts:error tag

You must be logged in to post in the forum log in