Struts in Action
Building web applications with the leading Java framework
Ted N. Husted, Cedric Dumoulin, George Franciscus, David Winterfeldt
  • October 2002
  • ISBN 9781930110502
  • 672 pages


Struts 2 in Action is now available. An eBook of this older edition is included at no additional cost when you buy the revised edition!

Struts in Action is a comprehensive introduction to the Struts framework. It covers initial design, data validation, database access, dynamic page assembly, localization, product configuration, and other important areas. It shows you how to use both JSP tags and Velocity templates. It carefully explains the Struts architecture and control flow, as well as how to extend framework classes.

Table of Contents detailed table of contents




about this book

Part 1 Getting started with Struts

1. Introduction

1.1. What is this book about?

1.1.1. Who makes the Struts software?

1.1.2. Why is Struts open source?

1.1.3. Why is it called Struts?

1.2. What are application frameworks?

1.2.1. Other types of frameworks

1.3. Enabling technologies

1.3.1. Hypertext Transfer Protocol (HTTP)

1.3.2. Common Gateway Interface (CGI)

1.3.3. Java servlets

1.3.4. JavaServer Pages

1.3.5. JSP tags

1.3.6. JavaBeans

1.3.7. Model 2

1.4. Struts from 30,000 feet

1.4.1. Building a simple application

1.4.2. Jump-starting development

1.4.3. Where the rubber meets the road

1.4.4. Looking back

1.5. Summary

2. Exploring the Struts architecture

2.1. Talking the talk

2.2. Why we need Struts

2.2.1. One step back, three steps forward

2.2.2. Enter Struts

2.2.3. Struts controller components

2.2.4. Developing a web application with Struts

2.3. Why we need frameworks

2.3.1. The Web—a never-ending kluge

2.3.2. The servlet solution

2.3.3. Servlet frameworks

2.3.4. The whitebox-blackbox continuum

2.4. Struts, Model 2, and MVC

2.4.1. The evolution of MVC

2.4.2. The rise of Model 2

2.4.3. Application layers—decoupling the view

2.4.4. How Struts implements Model 2, MVC, and layers

2.5. Struts control flow

2.5.1. The big picture

2.5.2. The finer details

2.5.3. Is Struts performant?

2.6. The strengths and weaknesses of Struts

2.6.1. The weak points

2.6.2. Struts' strong points

2.7. Summary

3. Building a simple application

3.1. Strut by Strut

3.1.1. Why a logon application?

3.2. Touring a logon application

3.2.1. Start here

3.2.2. Screens we’ll see

3.2.3. The welcome screen

3.2.4. The logon screen

3.2.5. The welcome screen, again

3.2.6. The welcome screen, good-bye

3.2.7. Feature roundup

3.3. Dissecting the logon application

3.3.1. The browser source for the welcome screen

3.3.2. The JSP source for the welcome screen

3.3.3. The configuration source for the welcome screen

3.3.4. The browser source for the logon screen

3.3.5. The configuration source for the logon screen

3.3.6. The LogonSubmit source

3.3.7. The LogonForm source

3.3.8. The LogonAction source

3.3.9. The LogoffAction source

3.4. Constructing an application

3.4.1. Defining the requirements

3.4.2. Planning the application

3.4.3. Planning the source tree

3.4.4. Setting up development tools

3.4.5. Setting up the build.xml file

3.4.6. Setting up the web.xml file

3.4.7. Setting up the struts-config.xml file

3.4.8. Testing the deployment

3.4.9. Constructing our welcome page

3.4.10. Constructing the logon page

3.4.11. Constructing the Constants class

3.4.12. Constructing the other classes

3.4.13. Creating the user directory

3.4.14. Configuring the ActionErrors

3.4.15. Compiling and testing the logon page

3.4.16. Amending the welcome page

3.4.17. The Struts ActionForward Action

3.5. Summary

4. Configuring Struts components

4.1. Three XMLs and a Properties file

4.1.1. The rest of the family

4.2. The web application deployment descriptor

4.2.1. The web.xml file

4.2.2. ActionServlet parameters

4.3. The Struts configuration

4.3.1. Details, details

4.3.2. Change management

4.3.3. The principle of Protected Variation

4.4. The Struts configuration elements

4.4.1. <global-exceptions>

4.4.2. <form-beans>

4.4.3. <global-forwards>

4.4.4. <action-mappings>

4.4.5. <controller>

4.4.6. <message-resources>

4.4.7. <plug-in>

4.4.8. <data-sources>

4.4.9. Rolling your own

4.4.10. A skeleton Struts config

4.5. The application resources file

4.6. The Ant build file

4.7. Configuring the Struts core

4.7.1. Installing Java and a Java servlet container

4.7.2. Installing a development environment

4.7.3. Installing the Struts core files

4.8. Configuring the Tiles framework

4.9. Configuring the Struts Validator

4.10. Getting started with the Struts Blank application

4.11. Configuring modular applications

4.11.1. Divide and conquer

4.11.2. Prefixing pages

4.11.3. Retrofitting a configuration

4.12. Sharing the Struts JAR

4.13. Summary

Part 2 Raising your framework

5. Coping with ActionForms

5.1. Garbage in, treasure out

5.1.1. ActionForm requirements

5.2. The many faces of an ActionForm

5.2.1. The ActionForm as a field harvester

5.2.2. The ActionForm as a data buffer

5.2.3. The ActionForm as a data validator

5.2.4. The ActionForm as a type transformer

5.2.5. The ActionForm as a transfer object

5.2.6. The ActionForm as a firewall

5.3. ActionForm design consequences

5.3.1. ActionForms may share names

5.3.2. ActionForms may minimize custom code

5.3.3. ActionForms may encapsulate helpers

5.3.4. ActionForms may nest other beans

5.4. ActionForm flavors

5.4.1. Map-backed ActionForms

5.4.2. DynaActionForms

5.5. Why isn’t an ActionForm…​

5.5.1. Why isn’t an ActionForm just a Map?

5.5.2. Why isn’t an ActionForm a plain JavaBean?

5.5.3. Why isn’t an ActionForm an interface?

5.6. Debriefing ActionForms

5.6.1. Implementing a business-layer interface

5.6.2. Nesting a mutable value object

5.6.3. Setting an immutable value object

5.6.4. Setting a mutable value object

5.6.5. Using a factory method

5.6.6. Passing a Map

5.6.7. Transferring values by reflection

5.6.8. Using an adaptor class

5.7. BaseForm

5.7.1. SessionLocale

5.7.2. Dispatch

5.7.3. Autopopulation

5.7.4. BaseMapForm

5.8. Summary

6. Wiring with ActionForwards

6.1. What ActionForwards do

6.2. How ActionForwards work

6.2.1. Forward versus redirect

6.3. Global and local forwards

6.4. Runtime parameters

6.4.1. Adding parameters in the page

6.4.2. Adding parameters in the Action class

6.5. Dynamic forwards

6.6. Why doesn’t the address bar change?

6.7. Rolling your own ActionForward

6.8. Summary

7. Designing with ActionMappings

7.1. Enter ActionMappings

7.1.1. The ActionMapping bean

7.1.2. The ActionMappings catalog

7.2. ActionMapping properties

7.2.1. The path property

7.2.2. The forward property

7.2.3. The include property

7.2.4. The type property

7.2.5. The className property

7.2.6. The name property

7.2.7. The roles property

7.2.8. The scope property

7.2.9. The validate property

7.2.10. The input property

7.2.11. The parameter property

7.2.12. The attribute property

7.2.13. The prefix and suffix properties

7.2.14. The unknown ActionMapping

7.3. Nested components

7.3.1. Local forwards

7.3.2. Local exceptions

7.4. Rolling your own ActionMapping

7.5. Summary

8. Working with Action objects

8.1. Ready, set, action!

8.2. Getting it done with Action objects

8.2.1. What are Actions?

8.2.2. When are Actions called?

8.2.3. What do Actions do?

8.2.4. What does an Action look like?

8.3. The standard Actions

8.3.1. Standard bridge Action classes

8.3.2. Standard base Actions

8.4. Chaining Actions

8.4.1. Starting fresh

8.5. Scaffold Actions

8.5.1. Forward-only Actions

8.5.2. Helper Actions

8.6. Base View Actions

8.7. Helper Action techniques

8.7.1. Optional forwarding

8.7.2. Calling ahead

8.7.3. Catching chained exceptions

8.7.4. Smart error forwarding

8.7.5. Confirming success

8.7.6. Alternate views

8.7.7. Reflecting methods

8.7.8. Reflecting classes

8.8. Using smart forwarding

8.9. Summary

9. Extending ActionServlet

9.1. Where’s the beef?

9.1.1. The servlet’s Gang of Three

9.2. The RequestProcessor

9.2.1. The process method

9.2.2. processRoles

9.3. The ExceptionHandler

9.4. PlugIn

9.5. Summary

Part 3 Building your pages

10. Displaying dynamic content

10.1. Tag—you’re it

10.1.1. JSP tags—what are they good for?

10.1.2. Struts and JSTL

10.1.3. Struts tags and MVC

10.2. Working with tag extensions

10.2.1. How are tag extensions written?

10.2.2. How are tag extensions installed?

10.2.3. What tag extensions are not

10.3. The Struts taglibs

10.3.1. Features common to Struts tags

10.3.2. The bean tags

10.3.3. The html tags

10.3.4. The logic tags

10.4. Using Struts JSP tags

10.4.1. The Struts tag team

10.4.2. Fundamentals

10.4.3. Techniques

10.4.4. Successful controls

10.5. Alternate views

10.5.1. Struts and JSPs

10.5.2. Servlet contexts

10.5.3. Beyond JSPs

10.6. Summary

11. Developing applications with Tiles

11.1. Leveraging layouts

11.1.1. Layering with dynamic templates

11.1.2. Template consequences

11.1.3. Using templates

11.1.4. Combining templates, Tiles, and Struts

11.2. Building a layout template

11.2.1. But what is a tile?

11.2.2. Deploying a Tiles template

11.2.3. Adding a style sheet

11.2.4. Templates and MVC

11.3. Tiles Definitions

11.3.1. Declaring Definitions

11.3.2. JSP declarations

11.3.3. Configuration file declarations

11.3.4. Using Definitions as ActionForwards

11.4. Tile attributes

11.4.1. useAttribute

11.4.2. importAttribute

11.4.3. put

11.4.4. putList and add

11.5. Migrating an application to Tiles

11.5.1. Setting up the Tiles framework

11.5.2. Testing the default configuration

11.5.3. Reviewing the pages

11.5.4. Refactoring a page with

11.5.5. Extracting the tags into a Definition

11.5.6. Normalizing your base layout

11.5.7. Refining your Definitions into base and extended classes

11.5.8. Developing a routine

11.5.9. Managing the migration

11.6. Summary

12. Validating user input

12.1. I know it when I see it

12.1.1. Input we can’t refuse

12.1.2. Web-tier validations

12.1.3. Validator consequences

12.2. Overview of the Struts Validator

12.2.1. Logon example

12.3. Basic validators

12.3.1. The required validator

12.3.2. The mask validator

12.3.3. The range validator

12.3.4. The maxLength validator

12.3.5. The minLength validator

12.3.6. The byte, short, integer, long, float, and double validators

12.3.7. The date validator

12.3.8. The creditCard validator

12.3.9. The email validator

12.4. Resource bundles

12.4.1. The default bundle

12.4.2. Default validator messages

12.4.3. Custom validator messages

12.5. Configuration files

12.6. Validator JSP tags

12.7. ValidatorForm and ValidatorActionForm

12.8. Localized validations

12.9. Pluggable validators

12.9.1. Creating pluggable validators

12.10. Techniques

12.10.1. Multipage validations

12.10.2. Cancel buttons

12.10.3. Custom messages

12.10.4. Interrelated fields

12.10.5. Combining validators with the validate method

12.11. Migrating an application to the Struts Validator

12.11.1. Setting up the Validator framework

12.11.2. Testing the default configuration

12.11.3. Reviewing your validations

12.11.4. Extending ValidatorForm or the Scaffold BaseForm

12.11.5. Selecting a validation to migrate

12.11.6. Adding the formset, form, and field elements

12.11.7. Adding new entries to the ApplicationResources

12.11.8. Calling the Struts Validator

12.11.9. Test and repeat

12.11.10. Removing the ActionForm subclass

12.12. Summary

13. Localizing content

13.1. By any other name

13.1.1. Why localize?

13.1.2. How Java internationalization works

13.2. Struts' internationalized components

13.2.1. Session Locale attribute

13.2.2. MessageResources

13.2.3. The default resource bundle

13.2.4. ActionErrors

13.2.5. ActionMessages

13.2.6. Locale-sensitive JSP tags

13.3. Localizing a Struts application

13.3.1. Enabling localization

13.3.2. Using the framework Locale object

13.3.3. Placing labels and messages in Properties files

13.3.4. Creating language-specified Properties files

13.3.5. Specifying an appropriate key in localization-aware components

13.3.6. Using <bean:message> with other components

13.4. Localizing other components

13.4.1. Localizing the Struts Validator

13.4.2. Localizing Tiles

13.4.3. Localizing collections

13.5. Summary

14. Using data services with Struts

14.1. Stepping out

14.1.1. JDBC from a patterns perspective

14.1.2. Introducing our data services

14.2. Exploring the business layer

14.2.1. Struts—bringing your own Model

14.2.2. Defining business objects

14.2.3. Designing business objects

14.2.4. Design consequences

14.2.5. Mixing business with Actions (not)

14.2.6. A simple example

14.3. Using ProcessBeans and JDBC with Struts

14.3.1. Introducing ProcessBeans

14.3.2. ProcessBeans as transfer objects

14.3.3. Populating ProcessBeans

14.3.4. Executing ProcessBeans

14.3.5. Accessing data services

14.3.6. Following a typical flow

14.3.7. Coding a business activity

14.3.8. ProcessBeans as a persistence layer

14.3.9. Using other persistence layers

14.4. Using result objects

14.4.1. ResultList methods

14.5. Using helper Actions

14.6. Using Lucene

14.6.1. searchProperties redux

14.7. Using content syndication

14.7.1. Digesting RSS

14.7.2. Retrieve and render

14.7.3. Syndicating RSS

14.8. Using EJBs with Struts

14.8.1. Session Facade

14.8.2. Data transfer objects

14.8.3. Implementation patterns

14.9. Summary

Part 4 Struts by example

15. Artimus: pulling out the stops

15.1. The framework’s framework

15.2. Scaffold—birth of a toolset

15.3. About Artimus

15.3.1. Building Artimus

15.4. The deployment descriptor (web.xml)

15.4.1. Configuring Artimus

15.4.2. Our application properties

15.4.3. Our connection adaptor

15.4.4. Our startup priority

15.4.5. Other configuration settings

15.4.6. Our security settings

15.4.7. The URLs we protect

15.4.8. The authorized users

15.4.9. Our authentication strategy

15.5. ArtimusServlet

15.5.1. Our subclass

15.5.2. Our String tokens

15.5.3. Our extension point

15.6. The application and SQL Properties files

15.7. index.jsp

15.8. Global forwards

15.9. /find/Recent

15.9.1. extends bean

15.9.2. super.execute

15.9.3. getArticles

15.9.4. Access.findByLast and ResultList

15.9.5. ProcessResult

15.9.6. ProcessAction

15.10. tiles.xml and Article.jsp

15.10.1. useAttribute

15.10.2. baseStyle

15.10.3. title

15.10.4. Tiles

15.11. result.jsp

15.11.1. The legend

15.11.2. isResult?

15.11.3. RESULT

15.12. Article actions

15.13. view.jsp

15.13.1. headline

15.13.2. content

15.13.3. contributor

15.14. edit.jsp

15.14.1. Article content

15.14.2. Contributed / contributor

15.14.3. Article ID

15.14.4. Validation

15.15. /do/Menu

15.15.1. logon

15.15.2. menu

15.15.3. Our controls

15.15.4. saveResult

15.15.5. Our results

15.16. menu.jsp

15.16.1. /find/Hours

15.16.2. /menu/Find

15.16.3. /find/Last

15.16.4. /menu/Contributor

15.16.5. /menu/Manager

15.17. Summary

16. Redux: migrating to Struts 1.1

16.1. Next station, Struts 1.1

16.1.1. Struts 1.1 feature roundup

16.1.2. Features we can use

16.2. Baseline changes

16.2.1. Tiles in Struts 1.1

16.2.2. Validator in Struts 1.1

16.2.3. ReloadAction in Struts 1.1

16.2.4. Other baseline changes to web.xml and struts-config.xml

16.2.5. message.jsp (1.1)

16.2.6. form.jsp (1.1)

16.2.7. MenuCreate (1.1)

16.2.8. Onward

16.3. Discretionary changes

16.3.1. Form to DynaActionForm

16.3.2. Action-based security

16.3.3. Action path changes

16.3.4. Application resources in Struts 1.1

16.4. Summary

17. Velocity: replacing JSPs

17.1. Moving to Velocity templates

17.2. Change makes the framework

17.3. Why we need Velocity

17.3.1. Velocity is light, fast, and versatile

17.3.2. Velocity works well with others

17.3.3. Velocity is simple but powerful

17.4. Using Velocity with web applications

17.4.1. Using Velocity with servlet resources

17.4.2. Using Velocity with context attributes

17.4.3. How Velocity works with Struts

17.4.4. The VelocityStruts toolkit

17.4.5. The Struts View tools

17.5. Our logon templates

17.6. Setting up VelocityViewServlet

17.6.1. Installing the VelocityViewServlet

17.6.2. Deploying the Velocity servlet

17.6.3. The toolbox configuration file

17.7. Setting up struts-config

17.8. Summary

Appendix A: Design patterns

A.1. A brief history of design patterns

A.1.1. The Gang of Four

A.1.2. J2EE Blueprints

A.1.3. Core J2EE Patterns

A.2. Why patterns are important

A.3. What patterns won’t do

A.4. Struts—a Who’s Who of design patterns

A.4.1. The Service to Worker pattern

A.4.2. The Singleton pattern

A.4.3. The Session Facade pattern

A.4.4. Value Object/Value Object Assembler patterns

A.4.5. The Composite View pattern

A.4.6. The Synchronizer Token pattern

A.4.7. The Decorator pattern

Appendix B: The struts-config API

B.1. <struts-config>

B.1.1. <set-property>

B.1.2. <data-sources>

B.1.3. <data-source>

B.1.4. <global-exceptions>

B.1.5. <exception>

B.1.6. <form-beans>

B.1.7. <form-bean>

B.1.8. <form-property>

B.1.9. <global-forwards>

B.1.10. <forward>

B.1.11. <action-mappings>

B.1.12. <action>

B.1.13. <controller>

B.1.14. <message-resources>

B.1.15. <plug-in>

Appendix C: Taglib quick reference




About the Technology

Struts solves the most common problems of web development. By basing your application on the Struts framework, you can reuse proven solutions and focus on what's unique to your own case. Struts is an open-source product distributed by the Apache Software Foundation.

About the book

Differences between Struts 1.1 and Struts 1.0. are pointed out and a case-study illustrates the 1.0 to 1.1 transition. The book shows you dozens of proven design techniques, patterns, and strategies, many of them not found elsewhere.

What's inside

  • Struts 1.1 and 1.0
  • Jakarta Validator and Tile packages
  • Jakarta Scaffold toolkit
  • Velocity templates
  • HTTP, Java Servlets, and JSP tags
  • Dynamic web programming
  • Servlet-centric application design
  • Working with databases and data services
  • Design and development patterns
  • Tons of examples

About the authors

Ted Husted is an acknowledged Struts authority, an active member of the Struts development team, and manager of the JGuru Struts Forum. As a consultant, Ted has worked with professional Struts development teams throughout the United States. Ted also helps manage the Apache Jakarta project, which hosts the Struts framework. Ted lives in Fairport, New York with his wife, two children, four computers, and an aging cat.

Cedric Dumoulin is an active member of the Struts development team and the author of the Tiles framework. Cedric is presently a researcher at the University of Lille. He has also worked in the R&D department of a leading international internet banking company. He lives in Lille, France.

George Franciscus is a principal at Nexcel, providing technical and management consulting services in several industries including Telecommunications, Banking, Life Insurance and Property and Casualty Insurance. George has expertise in Java, J2EE, Domino, relational databases, and mainframe technologies. He holds a BSc in Computer Science from the University of Toronto. George lives in Toronto, Ontario with his wife and three children.

David Winterfeldt is a Struts committer and author of the Commons Validator package. He works as a senior developer at a major company implementing J2EE technology. David currently lives in New York City.