Article

Complete the MVC Puzzle with Struts

Page: 1 2 3 4 5 6 7 Next

The Model

The model in our application is very simple, consisting of just one JavaBean. This is shown below:

package com.samjdalton.mvc.model;  
 
 
public class UserBean {  
 
   private String name;  
 
   public UserBean(){}  
   public UserBean(String name) {  
       this.setName(name);  
   }  
 
   public void setName(String name) {  
       this.name=name;  
   }  
 
   public String getName() {  
       return name;  
   }  
}

UserBean.class

Application in Action

As you can see, this is a very simple Model 2 application that could be greatly extended. For example, we could configure the actions dynamically; we could also externalize the flow of control (the actions could ask a configuration manager which page to go to next).

However, there is a pre-existing framework that provides all of this configurability and much more. That framework is called Struts.

Introduction to Struts

The Struts project was conceived by Craig McClanahan in early 2000 to provide a standard method for developing MVC-based Java Web applications. Struts 1.0 was finally released in the middle of 2001, and is now part of the Jakarta project from the Apache Foundation. Struts is in wide scale use in a variety of different projects, across arrange of industries (I personally have used it in various projects from banking to shipping!).

Struts is a highly configurable, highly extensible MVC framework that we can use to develop almost any Java Web application imaginable. Each member of the MVC framework is provided for within Struts. Let’s turn our thoughts to the task of installing Struts.

Installing Struts

You can obtain the latest Struts distribution (at the time of writing, this is 1.1) at http://apache.get-software.com/jakarta/struts/binaries/jakarta-struts-1.1.zip . Once you've downloaded it, you should unzip the application. The distribution contains all the libraries required to develop a Struts application. The distribution contains a blank Struts Web application (struts-blank.war) within the Webapps directory. This is particularly useful as it contains a skeleton Web application that you can build upon to create your own applications.

You can put your own code within the WEB-INF/classes directory, your own configuration within the WEB-INF/struts-config.xml, and with that, you’re away! You have a fully functional Struts application. You can create a war file and simply deploy this in your favourite Web container (for example, copy it to the webapps directory for Tomcat).

Next, we’ll look at the components provided by Struts.

The View Layer

The view layer of most Struts applications is made up of JavaServer pages. To facilitate the development of the view, Struts provides a set of JSP custom tag libraries. These tag libraries allow us to easily provide fully internationalised user interfaces that interact with the model components of a Struts application.

The vast majority of dynamic Web front ends are based on HTML forms, and users of such applications have come to expect from these applications certain behaviours, such as form validation. With standard JSP, this is a tedious process that involves recording the contents of the form and populating every form element with information from a JavaBean in case of error. Struts facilitates this sort of form processing and validation using FormBeans. These, in combination with the Struts tag libraries, make View development with forms really simple and natural.

Below is an example of a Struts JSP view:

<%@ taglib  uri="/WEB-INF/struts-html.tld" prefix="html" %>  
 
<html:html>  
<head></head>  
 
<body bgcolor="white">  
 
<html:errors/>  
<html:form action="/logon">  
<table border="0" width="100%">  
   <tr>  
       <td>  
           Username:  
       </td>  
       <td>  
           <html:text  property="username"/>  
       </td>  
   </tr>  
   <tr>  
       <td>  
          Password:  
       </td>  
       <td>  
           <html:password property="password"/>  
       </td>  
   </tr>  
   <tr>  
       <td>  
           <html:submit/>  
       </td>  
       <td>  
           &nbsp;  
       </td>  
   </tr>  
</table>  
 
</html:form>  
</body>  
</html:html>

As you can see, this does not look dissimilar to a standard HTML form. There are no nasty JSP scriptlets within the page, yet we have some pretty rich functionality. The page imports the Struts HTML tag library that allows us to assemble forms that provide validation, error handling, and model interaction. We also see the use of the <html:errors> tag, which displays any errors that the model or controller has registered. The <html:form> tag creates an HTML form based on an ActionForm object. As we can see, the form's action is set to /logon; this value is used by the tag to look up the ActionForm to use in the configuration file (see below). This mapping consists of the name associated with the form object, and the scope in which it is stored (session, page, application, etc.). The properties of the object are then used by the <html:text> and <html:password> tags to populate the form. The ActionForm is also automatically populated with the form values when it is submitted.

It might seem as though ActionForms represent the Model of our application, but, in actual fact, they should be considered part of the controller part of the application. Although ActionForm beans can represent the properties of our model, they contain no persistence logic, nor any business logic. ActionForms are used to pass Model information between the Model and the View.

As ActionForms represent part of the controller part of our application, let’s now look at building ActionForms in the controller section.

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

Sponsored Links