Charles Engelke's Blog

July 8, 2003

Building Web Applications with Struts

Filed under: OSCON 2003 — Charles Engelke @ 5:36 pm

This afternoon I’m attending a tutorial on using Struts to build web
applications. As with my Enterprise Java Development tutorial from this
morning, I don’t have experience with this language, so I’ll have to wait
and see if I can follow enough to be useful.

The presenters are Craig McClanahan and Amy Roh, and we didn’t get their
slides in paper as we did for every other presentation. Instead, they’ve
got them on the web. Because
the conference wireless network seems to have settled down this afternoon, I
was just able to download them (I tried last night, but only a Star Office
file version was available then, and I have PowerPoint installed here).
I’ve got the slides on my desktop now: all 182 of them. That sounds like a
lot more than three hours of material to me.

We were just asked how many people have an advanced knowledge of Struts
(nobody but the speakers), intermediate (none), basic (a few), and none
(most of us). That encourages me that I’ll be able to keep up!

Version 1.1 of Struts is now final, on the Apache web site.

We will have a background in the fundamental technology, then an
introduction to Struts. We will then create a Struts application, then
discuss factors we need to consider in deciding on web architectures, create
a more advanced application, then some leading edge technologies. Sounds
like a good agenda.

The technology backgrounder was pretty basic, and went very fast.
The slides almost provide a script for the materials. Struts uses HTTP,
HTML, JavaScript, Servlets, and JSP.

Now, on to Struts. Craig McClanahan was the primary author of Struts,
and was triggered by a need to take a US-centric application to Europe in
multiple languages. And to make it available on the web. The 0.91 JSP
specification gave two fundamental approaches to development. Model
1
makes a resource (like as JSP page) responsible for creating and
processing a form. Model 2 has a resource responsible only for
creating markup, processing the result is dispatched to a different resource.
McClanahan preferred the second approach, so he created a home grown
architecture. This was based on the “Model-View-Controller” paradigm
popular for client-server applications. The model is the persistent
data and business logic. The view is the user interface. The
controller is management software that dispatches requests to the
various components to do their job to create an entire application. The
flow could be as follows:

  1. Browser submits a request to a controller.
  2. The controller dispatches orders to the business logic component.
  3. The business logic unit gets and stores information by communicating
    with the model data.
  4. The controller dispatches a request to the view component to display
    the result.
  5. The view component pulls information from the model data.
  6. The view component renders its result to the browser.

Struts has only minimal infrastructure for the model tier. Instead, it
makes it easy to integrate other tools (like JDBC). The view tier has more
features. Form Beans represent state of form fields, and manage
default field data and validation. There’s a validation framework
that abstracts the rules to a separate XML document. It always checks at
the server, and can optionally create JavaScript for client-side validation,
too. Custom tab libraries are provided for JSP (JSP pages embed Java
code into the static HTML, and the Java is executed when the page is to be
rendered to a requester). Tiles Framework for JSP supports templating for a
common look and feel of all pages.

The controller tier also has a lot of
support in Struts. Behavior is specified by a configuration document, and
there can be multiple documents allowed. The Request Processor
standardizes the processing lifecycle:

  • Extract action mapping path
  • Select locale (if necessary)
  • Select action mapping to utilize
  • Perform role-based access checks
  • Server-side validation (if requested)
  • Invoke application Action
  • Forward based on application outcome

The Struts web page has
pointers to several complete books about Struts. McClanahan likes Chuck
Cavaness’s Programming
Jakarta Struts
, James Turner’s and Kevin Bedell’s
Struts Kick Start, and Ted
Husted’s, Cedric Dumoulin’s, George Franciscus’s, and David Winterfeldt’s
Struts in Action.

An aside: he mentions a tool called HTTPUnit that tests web applications
by taking the HTML files and looking at them as documents, rather than byte
sequences, making it easier to test the parts that matter and skip parts
that change incidentally.

Our First Struts Application. I always love to see examples, so
this next part should be really good. It’s the sample delivered in the
Struts distribution in webapps/struts-example.war. That’s
disappointing, because we’re just skimming over an existing application
rather than starting with a blank page. Describing how something that
exists works isn’t the same as showing how to create something that works.
And we’re back to the presenter (Amy Roh) basically using the slides as a
script, moving extremely quickly. I’m not getting much out of this, but all
the details are in the slides, available at the location I mentioned earlier
in this posting.

In fact, at the mid-afternoon break, I decided not to go back to the
presentation. I think I’ll get as much out of the slides as I would from
attending.

Advertisements

Create a free website or blog at WordPress.com.

%d bloggers like this: