ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Java Web Development with Stripes

by Mark Eagle
01/24/2007

Stripes is an open source, action-based Java web framework designed around the principles that web development should be simple and productive for developers. Traditional Java web development focused on versatility through decoupling, which resulted in multiple configuration files, additional objects, and other fragmented resources. These difficulties subjected many developers to a higher learning curve and reduced productivity. As a result, some Java developers have been lured away by non-Java frameworks such as Ruby on Rails or Django. Java web frameworks, like Stripes, are beginning to learn from the successes of alternative frameworks that streamline development. This article will show how Stripes distinguishes itself from other action based Java web frameworks, such as Struts, while supplying some of the simplicity available in Ruby on Rails.

Figure 1 shows the normal flow of events and components that are typical for applications that are written with Stripes.

Stripes overview
Figure 1. Typical Stripes flow

As you can see, this is pretty much what you would expect from an MVC framework. One of the main differences between Stripes and other action-based frameworks is the absence of external configuration files. As we will see, Stripes uses annotations and convention over configuration to allow more development and less clutter.

Building Your First Stripe Action

Let's jump right into the Stripes framework by creating a "Hello World" example to understand how things fit together. The HelloWorldAction class will prompt the user for a first and last name and then echo it out in a separate view. First, we will write the controller Java class.

public class HelloWorldAction implements ActionBean {    

  @ValidateNestedProperties({
    @Validate(field = "firstName", required = true, 
              on = {"hello"}),
    @Validate(field = "age", required = true, minvalue = 13, 
              on = {"hello"}) 
  })
  private Person person;
  private ActionBeanContext context;
    
  @DefaultHandler 
  public Resolution index() {
    return new ForwardResolution("Hello.jsp");
  }
        
  public Resolution hello() {
    return new ForwardResolution("SayHello.jsp");
  }

  public void setPerson(String person) {this.person = person;}
  public String getPerson() { return person;}
  public void setContext(ActionBeanContext c) {this.context = c; }
  public ActionBeanContext getContext() {return context; }
}

The controller class resembles a POJO (Plain Old Java Object) that implements a Stripes-specific interface called ActionBean. All Stripes actions need to implement this interface to allow the StripesDispatcher servlet to inject an ActionBeanContext object into the current action being serviced. The ActionBeanContext object allows you to access servlet API objects such as the request, response, and servlet context. Most of the time it is not necessary to access these low-level API objects in a Stripes application. The ActionBeanContext class also allows you to get state information about the current action as well as add informational messages and error messages from the current action. The ActionBeanContext field and accessors can be stored in a base class since all Stripes actions will require this implementation.

The rest of the controller class should be familiar to any Java developer. There is a Person object with accessors that will be used to read and write our person's first and last name to our views. While this is a simple nested object, Stripes allows more sophisticated data binding with Java collections, generics support, and indexed properties. Since Stripes can handle complex data binding, your domain objects can be reused in other layers that need them. For example, it is easy to collect information in a domain object via Stripes and make persistent changes with other POJO frameworks like Hibernate or EJB 3.

A simple Stripes validation annotation has been added to the person field to ensure the user enters a first and last name when invoking the hello method. If the user does not enter these required fields, they will be returned to the source page and shown an error message related to this validation. This validation will only be checked when the hello event is requested, as specified in the annotation attribute (on = {"hello"}). Stripes will also generate an error message using sensible defaults based on the type of validation and the name of the field. For example, if the required firstName field of the Person object is not provided when the form is submitted, the user will see the following:

Person First Name is a required field.

This message is constructed by taking the object graph component for Person.firstName and making it more human readable. These validation error messages can be overridden if necessary to provide more customization.

Pages: 1, 2, 3

Next Pagearrow