Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Creating the Show Message Action

The ShowMessageAction class displays a message with an ID as specified by the request parameter msgId.

Listing 2 ShowMessageAction.java Displaying a Message Specified by the Request Parameter msgId

import java.io.IOException;
import java.util.Locale;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.util.MessageResources;


public final class ShowMessageAction extends ActionBase {
    public ActionForward perform(ActionServlet servlet,
                 ActionMapping mapping,
                 ActionForm form,
                 HttpServletRequest request,
                 HttpServletResponse response)
    throws IOException, ServletException {

    // Extract attributes we will need
    Locale locale = getLocale(request);
    MessageResources messages = getResources(servlet);
    String msgId = request.getParameter("msgId");

    if (msgId == null) {
        String error = messages.getMessage("error.no.message",
                                          msgId);
        request.setAttribute("error", error);
        return (mapping.findForward("help"));
    }

    MessageBoard mbox = MessageBoard.getSingleton();
    Message msg = mbox.getMessage(msgId);

    //System.out.println(msg);

    if (msg == null) {
        String error = messages.getMessage("error.no.message",
                                           msgId);
        request.setAttribute("error", error);
        return (mapping.findForward("help"));
    }
    request.setAttribute("message", msg);
    return (mapping.findForward("success"));
    }
}
  • + Share This
  • 🔖 Save To Your Account