Using the Struts Framework to Develop a Message Board--Part 5: Developing Action Classes to Post and Display Messages
- Using the Struts Framework to Develop a Message Board--Part 5: Developing Action Classes to Post and Display Messages
- Creating the Show Message Action
- Augment the Application Resources File
- Augment the action.xml File
- Running the Application
In the previous sections, the input form's data carried a null parentId property. This caused us to add recovery code in the AddMessageAction class to set parentId to 1, the root message in the board. The name and email of the user creating the messages also had to be set for every post. In this section, we will create an action class to set the parentId to the ID of the message to which the post is a reply, set the subject to the parent message prefixed with "Re:" and prefix each sentence of the replies' body with ":".
Creating the Create Reply Action
The CreateReplyAction class is built on the same lines as AddMessageAction. Within the perform method, you need to obtain the parent message's ID from the parentId request parameter. Then, construct a Message instance using the Identity instance named user, which may be present in the session scope as well as from the parent message's subject and body (shown in Listing 1). Within the method, you will return an ActionForward instance representing showmessage.jsp.
Listing 1 CreateReplyAction.java—Preparing the Message Form Using Data from the Identity Class and the Parent Message
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 CreateReplyAction extends ActionBase { public final static String SPACE = " "; public final static String NEWLINE = System.getProperty("line.separator"); 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); HttpSession session = request.getSession(); String parentId = request.getParameter("parentId"); MessageBoard mboard = MessageBoard.getSingleton(); Message msg = null; if (parentId == null) { parentId = "1"; } if ((msg = mboard.getMessage(parentId)) == null ) { String error = messages.getMessage("error.parent.notexist"); System.out.println(error + parentId); request.setAttribute("error", error); return (mapping.findForward("help")); } // Populate the message form with default values if (form == null) { form = new Message(); session.setAttribute(mapping.getFormAttribute(), form); } Message msgForm = (Message) form; msgForm.setParentId(msg.getId()); if (msg.getSubject() != null) { msgForm.setSubject( messages.getMessage("reply.subject.prefix") + SPACE + msg.getSubject()); } if (msg.getBody() != null) { StringBuffer sb = new StringBuffer(); String prefix = messages.getMessage("reply.body.prefix") + SPACE; java.util.StringTokenizer st = new java.util.StringTokenizer(msg.getBody(), NEWLINE); while (st.hasMoreTokens()) { sb.append(prefix); sb.append(st.nextToken()); sb.append(NEWLINE); } msgForm.setBody(sb.toString()); } // Set the user properties Identity identity = (Identity) session.getAttribute("user"); if (identity != null) { msgForm.setName(identity.getName()); msgForm.setEmail(identity.getEmail()); } return (mapping.findForward("success")); } }