Using the Struts Framework to Develop a Message Board--Part 6: Viewing the Bulletin Board
- 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
The next step is to view the bulletin board containing the hierarchy of messages. To display the hierarchy, we use a data island. A data island is text that’s constructed by a bean—MessagesIsland, in this example. A JSP embeds the resultant text (created by the bean) to construct the output. An action class, ShowHierarchyAction, is responsible for initializing the MessagesIsland instance by setting the required format and setting the instance in the request scope for access by the JSP.
Creating the Data Island Bean MessagesIsland
The MessagesIsland bean traverses the message hierarchy recursively in order to generate the data island. The constructor takes a format string for displaying a message and the offset string for replies, as shown in Listing 1.
Listing 1 MessagesIsland.java—Recursively Creating the Data Island
import java.text.MessageFormat; public class MessagesIsland { public String SPACE; public MessageFormat formatter; protected StringBuffer sb = new StringBuffer(); protected OOMessage rootMessage; public MessagesIsland(String format, String space) { formatter = new MessageFormat(format); SPACE = space; } public void setRootMessage(OOMessage message) { rootMessage = message; } public OOMessage getRootMessage() { return rootMessage; } public String getText() throws Exception { printFromRoot(rootMessage); return sb.toString(); } // Methods to display the messages protected void printFromRoot(OOMessage rootMessage) throws Exception { if (rootMessage == null) return; for (int j = 0; j < rootMessage.getReplyCount(); j++) { printMessageHierarchy(rootMessage.getReplyAt(j), 0); } } protected void printMessageHierarchy(OOMessage msg, int level) throws Exception { for (int j = 0; j < level; j++) { sb.append(SPACE); } String[] args = { msg.getId(), msg.getSubject(), msg.getName(), msg.getTimestamp()}; sb.append(formatter.format(args)); // print the replies recursively for (int i = 0; i < msg.getReplyCount(); i++) { printMessageHierarchy(msg.getReplyAt(i), (level+1)); } } }