␡
- Using the Struts Framework to Develop a Message Board--Part 6: Viewing the Bulletin Board
- Creating the ShowHierarchy Action Class
- Creating the View JSP
- Running the Application
Like this article? We recommend
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.javaThe 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> <b>({2})</b> <font size="-1">{3}</font><br/> showtree.space= showtree.title=Message Board Hierarchy