Home > Articles > Programming > Java

Using the Struts Framework to Develop a Message Board--Part 4: Developing the Controller for the Application

📄 Contents

  1. Using the Struts Framework to Develop a Message Board--Part 4: Developing the Controller for the Application
  2. Creating the Web.xml Configuration File
  3. Deploying 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.
From the author of

Development of the controller involves the creation of Action classes, as well as the mapping configuration that needs to be used by the ActionServlet.

For the creation of a new message, we will create an AddMessageAction class that extends from ActionBase. You will need to implement the perform method as defined in the Action interface. The method accepts as parameter the ActionServlet instance, the ActionMapping class describing the mappings, the model instance, and the HTTP request and response instances.

Create the AddMessageAction Class

An Action class implements a specific command, such as adding a message to the message board, and forwards the request to the JSP that displays the added message.

The struts-example Web app has samples of Action classes that contain a lot of boilerplate code that you can use in your own Action classes. This includes extracting the internationalized messages, forwarding requests to named pages, invoking actions for cancelled requests, and providing error messages. In the perform method shown in Listing 1, we obtain a reference to the singleton instance of MessageBoard and add the message to it.

You can use the HTTP request and response instances to retrieve and set request and session variables. You will need to return ActionForward instances to forward the request to the specific output JSP. The ActionMapping and ActionForward instances are created by the ActionServlet instance from the action-mapping XML file.

Listing 1 AddMessageAction.java—The Action Class to Add a Message to the Singleton MessageBoard Instance

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.*;

public final class AddMessageAction 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);
        HttpSession session = request.getSession();
        MessageBoard mboard = MessageBoard.getSingleton();

        if (form == null) {
            String error = messages.getMessage("error.called.directly");
            request.setAttribute("error", error);
            return (mapping.findForward("help"));

        Message msgForm = (Message) form;
        String parentId = msgForm.getParentId();
        if (parentId == null || parentId.length() == 0) {
            parentId = "1"; // Set a default value
        if (isCancelled(request)) {
            // Remove unnecessary session attributes
            if (mapping.getFormAttribute() != null) {
            request.setAttribute("message", mboard.getMessage(parentId));
            return (mapping.findForward("success"));

        ErrorMessages errors = msgForm._validate();
        if (errors.getSize() > 0) {
            // Errors encountered while validating form
            saveErrors(request, errors);
            return (new ActionForward(mapping.getInputForm()));

        // Form is no longer required...remove from session
        if (mapping.getFormAttribute() != null) {

        // Set the identity as session data
        Identity user = new Identity();

        session.setAttribute("user", user);

        if (mboard.getMessage(parentId) == null ) {
            String error = messages.getMessage("error.parent.notexist");
            // Couldn't find parent message to attach to
            request.setAttribute("error", error);
            return (mapping.findForward("help"));

        OOMessage message = mboard.addMessage(msgForm, parentId);
        request.setAttribute("message", message);

        return (mapping.findForward("success"));

Specifying the Action Mappings

The action mappings are specified in an XML file within an action-mapping element. This element nests an action element that specifies a named action, add; the model class being used in the dialogue, MessageForm; the specific instance of the model in the session scope, messageForm; and the input JSP in the dialogue, /message.jsp, as shown in Listing 2. It also nests zero or more forward tags. The forward tags provide symbolic names for physical JSPs. This name is also useful in extracting the ActionForward instance from the ActionMapping instance passed in the perform method of the Action class.

Listing 2 action.xml—Specifying the Action Mappings for ActionServlet

  <action path="/add"
        <forward name="help" path="/help.jsp"/>
        <forward name="success" path="/showmessage.jsp"/>
  • + Share This
  • 🔖 Save To Your Account