Home > Articles > Programming > Java

Using the Struts Framework to Develop a Message Board--Part 5: Developing Action Classes to Post and Display Messages

📄 Contents

  1. Using the Struts Framework to Develop a Message Board--Part 5: Developing Action Classes to Post and Display Messages
  2. Creating the Show Message Action
  3. Augment the Application Resources File
  4. Augment the action.xml File
  5. Running the Application
  • Print
  • + Share This
In this seven-part series, Java expert Maneesh Sahu explores how to use Apache Software Foundation's Struts framework to develop a Web-based message board. In part 5, he develops an action class to post and display messages.
Like this article? We recommend

Like this article? We recommend

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 =

    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;
        if (msg.getSubject() != null) {
               messages.getMessage("reply.subject.prefix") +
               SPACE + msg.getSubject());

        if (msg.getBody() != null) {
            StringBuffer sb = new StringBuffer();
            String prefix = messages.getMessage("reply.body.prefix") +
            java.util.StringTokenizer st = new
            while (st.hasMoreTokens()) {
        // Set the user properties
        Identity identity = (Identity) session.getAttribute("user");
        if (identity != null) {
        return (mapping.findForward("success"));
  • + Share This
  • 🔖 Save To Your Account