STRUTS: Problem with ActionMessages ! Please help !

Discussions

Web tier: servlets, JSP, Web frameworks: STRUTS: Problem with ActionMessages ! Please help !

  1. Hi !
    I am developing a webbased struts application.
    Due to changes in struts version 1.2 an derros we get (ClassCastException in ErrorsTag.java), I decided to change the Actions from ActionErrors to ActionMessages.


    When I try with some validation and database erros, the AxctrionMessages object will be filled correctly.

    But when I want to display them, nothing is displayed.

    This is my source code:

    AbstractAction:
    protected void setActionMessages(ActionMessages messagesToAdd, HttpServletRequest request) {
    if((messagesToAdd != null) && (messagesToAdd.size() > 0)){
     saveMessages(request, actionMessagesToAdd);
    }
    else return;
    }

    Action:
    ActionMessages actionMessages = null;
    try {

    actionMessages = form.validateCustom(mapping, request);
    if (actionMessages!=null && !actionMessages.isEmpty()) {
    setActionMessages(actionMessages, request); //Here I use the abstract method
    return findActionForward(mapping, form.getActionForward());
    }

    ...

    actionMessages = prepareData(data, request);
    if ((actionMessages != null) && (actionMessages.size() > 0)){
    saveMessages(request, actionMessages);
    return findActionForward(mapping, form.getActionForward());
    }
    else {
    actionMessages = checkData(data);
    }
    if ((actionMessages != null) && (actionMessages.size() > 0)){
    saveMessages(request, actionMessages);
    return findActionForward(mapping, form.getActionForward());
    }
    else {
    actionMessages = validateSpecial(data, request);
    }

    and so on...

    In the jsp, I have:
    <logic:messagesPresent>
    <h3>Errors:</h3>
    <html:messages id="msg">
    <bean:write name="msg"/>
    <br>
    </html:messages>
    </logic:messagesPresent>

    For example, when the method "prepareData();" gets an erro, it will be hold in the actionMessages object.
    Thats ok.
    This means, the saveMessages() method will be called.
    After that the "findActionForward()" method will be called an the jsp will be shown.
    Now I would expect to see the messages in the header of the jsp.
    But I still see the same JSP like before, no error message etc.

    What I am doing wrong ???

    Please help !

    best regards
    Michael
  2. Hi !!!
    Try <html:errors property="property" />
    Where "property" is the property that contains the error.

    MV
  3. message=true[ Go to top ]

    Hi Michael,

    I reckon that you need message="true" on both the logic & the html tags to get the message to display. I think this is because, by default, the tags go looking for 'errors' rather than 'messages' & you've got them stored as 'messages' now.
    The user guide isn't particularly descriptive, but worth a look...
    http://struts.apache.org/userGuide/struts-logic.html#messagesPresent

    This works for us anyway:

    <logic:messagesPresent message="true">
        <html:messages id="message" message="true">
    <bean:write name="message" />
        </html:messages>
    </logic:messagesPresent>

    Regards,
    Louise
  4. Have you checked out the Struts Wiki on this?
    http://wiki.apache.org/struts/StrutsDeprecatedActionErrors
    may be useful