Article

Complete the MVC Puzzle with Struts

Page: 1 2 3 4 5 6 7 Next

The Model Layer

Having looked at the view layer of a Struts application, we can now consider the Model layer.

The model layer in a Struts application can be implemented using any Java-based technology, such as EJB, Hibernate, or JDO. Often, the model is represented as simple JavaBeans containing the data and business logic. As discussed, ActionForm objects do not represent the true model of the application, and our Model layer should not directly represent HTML form objects. As far as possible, the model objects should be developed so that they have no knowledge of the environment (Struts or otherwise) that they’re being used within. This allows us to more easily reuse them across environments and applications.

For the purposes of this article, we’ll develop a simple JavaBean-based model layer that contains no persistence mechanism. As this will map to our ActionForm object, we could simply replace it with a more complex technology in the future.

The Controller Layer

Now that we understand how to build the Model and View pieces of a Struts application, we can move on to look at the Controller. Struts includes a Servlet that implements the primary functions of the Controller, which is to map the incoming URL to an action object. This Servlet is known as the ActionServlet, and provides the following functions:

  1. Decide what action is required to service a users request
  2. Provide view data to the view
  3. Decide which view to show next

Most of the controller’s work is delegated to a set of simple Action classes. A Struts developer must provide these actions to implement the logic of their application. In order to create an Action, the developer must implement the Action interface. This interface contains the following method, in which the bulk of the logic is placed:

public ActionForward execute(ActionMapping mapping,    
                    ActionForm form,    
                    HttpServletRequest request,    
                    HttpServletResponse response)    
throws Exception;


As you can see, the method takes an ActionForm as one of its parameters. The ActionServlet ensures that the correct form is passed to the method. As discussed earlier, ActionForms help us to transfer data from the Model layer to the View layer.

ActionForms are very simple objects; the code below represents the ActionForm that we would use for a simple HTML form:

import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;    
import org.apache.struts.action.ActionForm;    
import org.apache.struts.action.ActionMapping;    
   
public class Login extends ActionForm {    
   
   protected String username;    
   protected String password;    
   
   public void setUsername(String username) {    
       this.username = username;    
   }    
       
   public String getUsername() {    
       return username;    
   }    
       
   public void setPassword(String password) {    
       this.password=password;    
   }    
       
   public String getPassword() {    
       return password;    
   }    
   
}

The action also takes an ActionMapping object. This is automatically dealt with by the ActionServlet, and represents the configuration of your application. The configuration is achieved using an XML file, usually called struts-config.xml, which the next section will discuss in detail.

The action also takes the standard request and response; your application can interrogate these to get the required information to do its job. Once the Action has done its processing, it simply asks the ActionMapping where it should go next, and returns this to the ControllerServlet.

Pulling the Layers Together

Now that we’ve looked at the layers that exist in a Struts application, we can examine how they are tied together to form a full application. As we’ve already mentioned, Struts applications are configured using an XML file, named struts-config.xml. This configuration file contains information about every configurable aspect of the application, including:

  1. the controller to use
  2. the ActionForms and the HTML forms that they map to
  3. the Actions
  4. the ActionMappings, which determine the flow of control through the application

Each element within the struts-config.xml file is contained within the <struts-config> element:

<?xml version="1.0" encoding="ISO-8859-1" ?>    
   <!DOCTYPE struts-config PUBLIC    
             "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"    
             "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">    
   <struts-config>

For the purposes of this article, we will not consider the configuration of the controller component, as this is largely unnecessary for most applications.

In order to configure our ActionForm objects, we’ll use the following XML configuration:

<form-beans>    
 <form-bean    
               name="logonForm"    
               type="com.samjdalton.struts.LogonForm"/>    
</form-beans>

As we can see, this declares a form called logonForm and indicates that the com.samjdalton.struts.LogonForm class implements this form.

Next, we declare our ActionMappings. This is achieved using the following XML configuration tags:

<action-mappings>    
               
           <action    
               path="/Login"    
               forward="/login.jsp"/>    
     <action    
               path="/Welcome"    
               forward="/welcome.jsp"/>    
   
     <action    
               path="/ProcessLogin"    
               type="com.samjdalton.struts.LoginAction"    
               name="logonForm"    
               scope="request"    
               validate="true"    
               input="/Login.do">    
               <forward    
                   name="success"    
                   path="/Welcome.do"/>    
               <forward    
                   name="failure"    
                   path="/Logon.do"/>    
           </action>      
</action-mappings>

This declares three actions for our application. The first two (/Login and /Welcome) are very simple, in that they just forward on to a JSP page. The third is more complex. This action is called on the submission of a form. It creates the ActionForm represented by the logonForm element, and calls the LoginAction class to process the information. This element also contains 2 <forward> elements. These define the flow of control for the application. The application references these by their name (success or failure) and the control is passed to the relevant resource.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links