Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Creating the ShowHierarchy Action Class

This action class creates an instance of the MessagesIsland bean after passing the format and space arguments. It also sets the root message determined by its ID, which is extracted from the msgId request parameter (shown in Listing 2).

Listing 2 ShowHierarchyAction.java—The Action Class to Display the Message Board

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 ShowHierarchyAction 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);
    MessageBoard mboard = MessageBoard.getSingleton();

    String msgId = request.getParameter("msgId");
    if (msgId == null || mboard.getMessage(msgId) == null) {
        String error = messages.getMessage("error.no.message",
                                          msgId);
        request.setAttribute("error", error);

        return mapping.findForward("help");
    }

    MessagesIsland dataIsland =
        new MessagesIsland(
            messages.getMessage("showtree.format"),
            messages.getMessage("showtree.space"));
    dataIsland.setRootMessage(mboard.getMessage(msgId));

    request.setAttribute("dataIsland", dataIsland);
    return (mapping.findForward("success"));
    }
}

Augment the ApplicationResources.properties file to include the new messages.

showtree.format=<a href="show.do?msgId={0}">{1}</a> &nbsp; <b>({2})</b> <font size="-1">{3}</font><br/>
showtree.space=&nbsp; &nbsp; &nbsp;
showtree.title=Message Board Hierarchy
  • + Share This
  • 🔖 Save To Your Account