Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Subscription to Java Pro

email article
printer friendly
get the code

Who Needs Struts?
For smaller projects it's sometimes better to write code by hand rather than incur the overhead of a major framework.
by Lew Bloch

September 28, 2005

Model-View-Controller (MVC) identifies a design pattern that suits J2EE applications especially well, as the abundance of tutorials, white papers, and frameworks such as Apache Struts attest. The benefit is real, the hype notwithstanding, so we use Struts everywhere and the bluebirds of happiness sing sweetly, right? Well, maybe. Sometimes the framework is just a wee bit overmuch for a small application, or it otherwise obscures the underlying MVC-ness. Wouldn't it occasionally be simpler and more useful to avoid all that scaffolding? Let's look at how to use a template for MVC applications that uses just Java, JavaServer Pages (JSP), and a few simple HTML idioms to gain all the benefit of the MVC pattern with virtually no overhead except to use your brain.

ADVERTISEMENT

The heart of the MVC application is the controller, which we'll implement as the Controller class. Its job is to link an action from the front end to some model logic on the back end, and then show some result through a view JSP. That JSP will accept user input and post it to the Controller as another action, and the cycle repeats. Unlike the frameworks, our implementation is minimal and does not use external resources to define its flow, although that would be a simple enhancement.

The Controller assumes that every JSP sends at least one parameter named action, which maps to a unique combination of a model object and a view. When Controller receives a POST, it uses action to look up a model-view pair, and then it hands off the HTTP request to an instantiation of the model and forwards control to the view. Simple.

Each model object is of a class that derives from the parent Logic class, so that the controller need not care about the specifics except in the initialization. The corresponding view JSP is usually cognizant of the specific subclass it needs. The simplified code is shown in Listing 1. (You can also download the code here.)

We leave out try-catch blocks and other necessary code for expository purposes, but it really is close to being this simple. The model-view pair associated with each action is expressed by the inner class MV:

private static class MV
{
  public final 
    Class <? extends Logic> clazz;
  public final String         view;
  public MV( 
    Class <? extends Logic> clazz, 
    String view )
  {
    this.clazz = clazz;
    this.view = view;
  }
  public static final MV DEFAULT 
    = new MV( Logic.class, "index.jsp" );
}

The rest is up to the subclasses of Logic and the JSPs, the model, and view, respectively. For example, Controller maps the action value "simple" to the model-view pair MV(SimpleLogic.class, "simple.jsp"). So when Controller invokes logic.setRequest(request), SimpleLogic takes care of it.




Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home