Using ActionMessages in struts


Web tier: servlets, JSP, Web frameworks: Using ActionMessages in struts

  1. Using ActionMessages in struts (2 messages)

    In my Action class I have the following piece of code:
    ActionMessages messages = new ActionMessages();
           new ActionMessage("actioinMessage.updateSuccess"));
    return actionMapping.findForward("success");

    In the I have:
    actioinMessage.updateSuccess= Successfully Updated

    My problem is how display this message in a JSP, i.e, in a JSP page how to access the action-messages stored in the request.
  2. <logic:messagesPresent message="message">
                    <html:messages id="message">
    • <bean:write name="message"/>
    •               </html:messages>

    "message" refers to the name of the attribute of the ActionMessages in the request. Here, we conditionally display some HTML if these messages are there, and then display the messages as an unordered list. The <html:messages/> tag iterates through the messages.
  3. Suppose u have code like below
    ActionMessages messages = new ActionMessages();
    ActionMessage msg = new ActionMessage("data.ok");
    messages.add("message1", msg);

    msg = new ActionMessage("data.continue");
    messages.add("message2", msg);

    saveMessages(request, messages);

    and in the resource file u have message values like

    data.ok=Data saved successfully
    data.continue=Please continue

    U can display messages in JSP as

    <html:messages id="msg" message="true">
    <bean:write name="msg"/><br>

    Refer this link for simple Example