Cover image for Programming Jakarta Struts

Book description

Web tier frameworks have really taken off in the past year or so. Developers who used to spend hours and hours writing low-level features have realized the enormous benefits of using well-written frameworks to build the presentation tier so they can get to coding the "good stuff", the business logic at the core of the program. The Struts Framework, originally created by Craig R. McClanahan and donated to the Apache Software Foundation's Jakarta project in 2000, has become one of the most popular presentation frameworks for building web applications with Java Servlet and JavaServer Pages (JSP) technology. It encourages application architecture based on the Model-View-Controller (MVC) design paradigm, colloquially known as the Model 2 approach. As popular as Struts is becoming, the online documentation is inadequate, focusing on the most basic functionality and leaving out information crucial to developers writing today's complex web applications. O'Reilly's Programming Jakarta Struts was written by Chuck Cavaness after his internet company decided to adopt the framework, then spent months really figuring out how to use it to its fullest potential. He calls the books, "the culmination of lessons learned (the hard way) during the building of our application." Readers will benefit from the real-world, "this is how to do it" approach Cavaness takes to developing complex enterprise applications using Struts, and his focus on the 1.1 version of the Framework makes this the most up-to-date book available. Programming Jakarta Struts covers:

  • An overview of the concepts involved in writing web applications

  • Detailed installation and configuration instructions to get Struts up and running quickly

  • A thorough discussion of how Struts implements the Model-View-Controller pattern, and how to interface with that pattern in your own applications

  • JSP and Jakarta Tag Libraries for authoring complex web pages

  • Logging, Validation, and Exception Handling with Struts

  • Using the new Struts template framework, Tiles.

  • Writing internationalization and localization code using Struts

  • Practical, real-world best practices for web applications

Craig McClanahan, originator of Struts, says of the book, "One thing a lot of open source packages lack is a comprehensive guide to all of the features -- something that goes far enough past "hello, world" to get you into solving real application design problems, and it looks like you've hit just the right level for a lot of people."

Table of Contents

  1. Programming Jakarta Struts
    1. Preface
      1. Organization
      2. Conventions Used in This Book
      3. Comments and Questions
      4. Acknowledgments
    2. 1. Introduction
      1. A Brief History of the Web
      2. What Are Java Servlets?
      3. JavaServer Pages
      4. JSP Model 1 and Model 2 Architectures
      5. Why Is Model-View-Controller So Important?
        1. The MVC Model
        2. The MVC View
        3. The MVC Controller
      6. What Is a Framework?
      7. Creation of the Struts Framework
        1. Contribution to the Struts Project
      8. Alternatives to Struts
        1. Building Your Own Framework
        2. Barracuda
        3. Cocoon
        4. Expresso
        5. Freemarker, Velocity, and WebMacro
        6. Maverick
        7. SiteMesh
        8. Jakarta Turbine
        9. WebWork
        10. JavaServer Faces
    3. 2. Inside the Web Tier
      1. An Architecture Overview
        1. The Client Tier
        2. The Web Tier
        3. The Middle Tier
        4. The Enterprise Information System Tier
        5. Where Does Struts Fit In?
      2. The HTTP Request/Response Phase
        1. The HTTP Request
        2. The HTTP Response
        3. HTTP Versus HTTPS
      3. Struts and Scope
        1. Request Scope
        2. Session Scope
        3. Application Scope
        4. Page Scope
      4. Using URL Parameters
      5. Forward Versus Redirect
        1. Using a Redirect
        2. Using a Forward
        3. Which Is Best for Struts?
    4. 3. Overview of the Struts Framework
      1. A Banking Account Example
      2. Looking at the Big Picture
        1. The Struts Component Packages
      3. Struts Controller Components
        1. The Struts ActionServlet
        2. Struts Action Classes
        3. Mapping the Actions
        4. Determining the Next View
      4. Struts Model Components
      5. Struts View Components
        1. Using the Struts ActionForm
        2. Using JSP for Presentation
        3. Using Custom Tag Libraries
        4. Using Message Resource Bundles
      6. Multiple Application Support
      7. Summary
    5. 4. Configuring Struts Applications
      1. The Storefront Application
      2. What Is a Web Application?
        1. Elements of a Web Application
      3. The Web Application Directory Structure
        1. Web Application Archive Files
      4. The Web Application Deployment Descriptor
        1. Web Application Deployment Descriptor DTD
      5. Configuring the web.xml File for Struts
        1. Mapping the Struts ActionServlet
          1. Mapping requests to servlets
        2. Specifying Multiple Application Modules
        3. Declaring the Initialization Parameters
        4. Configuring the Tag Libraries
        5. Setting Up the Welcome File List
          1. Using a Struts action in the welcome file list
        6. Configuring Error Handling in web.xml
      6. The Struts Configuration File
        1. Configuring Multiple Application Modules
      7. The org.apache.struts.config Package
        1. The ApplicationConfig Class
        2. The Struts Configuration DTD
          1. The data-sources element
          2. The form-beans element
          3. The global-exceptions element
          4. The global-forwards element
          5. The action-mappings element
          6. The controller element
          7. The message-resources element
          8. The plug-in element
        3. A Complete struts-config.xml File
        4. Using Multiple Application Modules
        5. Specifying a DOCTYPE Element
      8. The Digester Component
      9. The Struts Console Tool
      10. Reloading the Configuration Files
    6. 5. Struts Controller Components
      1. The Controller Mechanism
        1. The ActionServlet Class
          1. Extending the ActionServlet class
          2. Struts initialization process
        2. The RequestProcessor Class
          1. Extending the RequestProcessor class
        3. The Action Class
          1. The Action class cache
          2. The ActionForward class
        4. Creating Multithreaded Action Classes
        5. Business Logic and the Action Class
        6. Using the Prebuilt Struts Actions
          1. The org.apache.struts.actions.ForwardAction class
          2. The org.apache.struts.actions.IncludeAction class
          3. The org.apache.struts.actions.DispatchAction class
          4. The org.apache.struts.actions.LookupDispatchAction class
          5. The org.apache.struts.actions.SwitchAction class
      2. The Utilities Classes
        1. The RequestUtils Class
        2. The ResponseUtils Class
        3. The Commons BeanUtils Package
        4. The Commons Collection Package
        5. Security in the Action Classes
    7. 6. Struts Model Components
      1. The “M” in MVC
        1. Types of Models
          1. The conceptual model
          2. The design model
      2. What Is a Business Object?
        1. Requirements for Business Objects
        2. The Importance of Business Objects
      3. Persistence
        1. Persisting Business Objects
        2. Storing Objects into a Relational Model
          1. The impedance mismatch
      4. What Does Struts Offer for the Model?
      5. Building the Storefront Model
        1. Accessing a Relational Database
        2. Creating the Storefront Business Objects
        3. The Storefront Data Model
        4. Mapping the Business Objects to the Database
        5. Object-to-Relational Mapping Frameworks
        6. The Storefront Persistence Framework
        7. The Business Delegate and DAO Patterns in Action
        8. Conclusion
    8. 7. Struts View Components
      1. What Is a View?
        1. Using Views Within the Struts Framework
          1. HTML documents
          2. JSP custom tags
          3. JavaScript and stylesheets
          4. Multimedia files
          5. Message resource bundles
        2. Using JavaBeans Within the View Components
          1. Quick refresher
          2. How Struts applications use JavaBeans
          3. The Data Transfer Object pattern
      2. What Are ActionForms?
        1. ActionForms and Scope
        2. The Lifecycle of an ActionForm
        3. Creating an ActionForm
          1. The validate( ) method
          2. The reset( ) method
        4. Declaring ActionForms in the Struts Configuration File
        5. Using an ActionForm in an Action
        6. Declaring ActionForm Properties as Strings
        7. Using ActionForms Across Multiple Pages
      3. Using ActionErrors
        1. The ActionMessage Class
        2. Creating ActionErrors in the Action Class
      4. Performing Presentation Validation
      5. Using the DynaActionForm Class
        1. Configuring Dynamic ActionForms
        2. Performing Validation with the DynaActionForm
      6. Looking Ahead to JavaServer Faces
        1. What Does JSF Have to Do with Struts?
    9. 8. JSP Custom Tag Libraries
      1. Custom Tags Overview
        1. What Is a Tag?
        2. What Is a JSP Custom Tag?
          1. Advantages of using custom tags
        3. What Is a Tag Library?
          1. Tag library components
          2. Tag handler
          3. The TLD file
          4. The web.xml file
          5. The JSP page
      2. Tag Libraries Included with Struts
        1. Using Tag Libraries with Struts Applications
      3. Using JavaBeans with Struts Tags
        1. Accessing Simple Properties
        2. Accessing Nested Properties
        3. Accessing Indexed Properties
      4. Struts HTML Tags
        1. The html Tag
        2. The base Tag
        3. The form Tag
          1. The action attribute
          2. The enctype attribute
          3. The name attribute
          4. The scope attribute
          5. The type attribute
        4. Using Multiple form Tags
        5. The button and cancel Tags
        6. The checkbox Tag
        7. The messages and errors Tags
        8. JavaScript Event Handlers
        9. HTML Navigation Attributes
      5. Logic Tags
        1. Value Comparison
        2. Substring Matching
        3. Redirecting and Forwarding
        4. Collection Utilities
        5. Messages and Errors
      6. Bean Tags
        1. The define Tag
        2. The header Tag
        3. The include Tag
        4. The message Tag
        5. The parameter Tag
        6. The resource Tag
        7. The write Tag
      7. Template Tags
      8. Nested Tags
      9. Other Useful Tag Libraries
        1. The Jakarta Taglibs Project
        2. JSPTags.com
      10. The JSP Standard Tag Library (JSTL)
        1. JSTL Core Tags
        2. JSTL Internationalization Tags
        3. JSTL XML Tags
        4. JSTL SQL Tags
        5. A New Expression Language
        6. JSTL Requirements
        7. JSTL and Struts
    10. 9. Extending the Struts Framework
      1. What Are Extension Points?
      2. General Extension Points
        1. Using the PlugIn Mechanism
          1. Adding the plug-in to the configuration file
        2. Extending the Struts Configuration Classes
      3. Controller Extension Points
        1. Extending the ActionServlet Class
        2. Extending the RequestProcessor Class
          1. Using the processPreprocess( ) method
        3. Extending the Base Action Class
      4. Extending View Components
        1. Extending Struts Custom Tags
      5. Extending Model Components
        1. The UserContainer and ApplicationContainer Classes
      6. Downsides to Extending the Framework
    11. 10. Exception Handling
      1. Java Exception Handling
        1. Java Exceptions
        2. The Method Invocation Stack
        3. What About the throws Clause?
        4. Checked and Unchecked Exceptions
        5. Performance Impact of Exception Handling
      2. System Versus Application Exceptions
      3. Using Chained Exceptions
        1. Dealing with Multiple Exceptions
      4. Exception Handling Provided by Struts
        1. Declarative Versus Programmatic Exception Handling
        2. Using Programmatic Exception Handling
      5. Tying Up the Loose Ends
        1. Handling Remote Exceptions
        2. Exceptions in Custom Tags
          1. The TryCatchFinally interface
        3. Internationalized Exception Handling
      6. Conclusion
    12. 11. The Validator Framework
      1. The Need for a Validation Framework
      2. Installing and Configuring the Validator
        1. Required Packages
        2. Configuring the Validation Rules
          1. The validation-rules.xml file
          2. The validation.xml file
        3. Plugging In the Validator
      3. Using an ActionForm with the Validator
      4. Creating Your Own Validation Rules
      5. The Validator and JSP Custom Tags
        1. Using JavaScript with the Validator
          1. Configuring the validation-rules.xml file for JavaScript
      6. Internationalizing the Validation
      7. Using the Validator Outside of Struts
        1. Modifying the validation-rules.xml File
    13. 12. Internationalization and Struts
      1. What Is Internationalization?
      2. Support for I18N in Java
        1. The Locale Class
          1. The default locale
          2. Determining the user’s locale
        2. Java Resource Bundles
        3. The MessageFormat Class
        4. Multilingual Support
      3. Internationalizing Your Struts Applications
        1. The Struts Resource Bundle
          1. Creating a Struts resource bundle
          2. Resource bundle naming guidelines
          3. The resource bundle and the classpath
        2. Accessing the Resource Bundle
          1. The Bean tag library’s MessageTag class
        3. Setting the Character Set
      4. Exception Handling and Internationalization
    14. 13. Struts and Enterprise JavaBeans
      1. Implementing the Storefront Service Using EJB
        1. A Quick EJB Overview
        2. The Session Façade
        3. The Business Interface
        4. Stateless Session Bean Implementation
        5. JBoss Deployment
      2. Interfacing Struts to EJB
        1. Using a Business Delegate
          1. Exception handling
          2. Swapping the implementation
        2. Managing EJB Home and Remote References
          1. Implementing an EJBHomeFactory
          2. Using an EJBHomeFactory in a business delegate
          3. What about the remote references?
        3. Using Dynamic Proxies
      3. Conclusion
    15. 14. Using Tiles
      1. Understanding Templates
        1. What Is a Template?
      2. Installing and Configuring Tiles
        1. Downloading Tiles
        2. Installing the Required JARs and Misc Files
        3. Adding the Tiles Tag Library
        4. Configuring Tiles to Work with Struts
      3. Overview of Tiles
        1. What Is a Tile?
        2. Using a Layout Tile
        3. Planning Your Layout
        4. Passing Parameters to the Layout
      4. The Tiles Tag Library
        1. The insert Tag
        2. The definition Tag
        3. The put Tag
        4. The putList Tag
        5. The add Tag
        6. The get Tag
        7. The getAsString Tag
        8. The useAttribute Tag
        9. The importAttribute Tag
        10. The initComponentDefinitions Tag
      5. Using Definitions
        1. Declaring Definitions in a JSP Page
        2. Declaring Definitions in a Configuration File
          1. Creating a definition configuration file
        3. Extending Tile Definitions
        4. Using Definitions as Forwards in Struts
      6. Internationalization Support with Tiles
    16. 15. Logging in a Struts Application
      1. Logging in a Web Application
      2. Using the Servlet Container for Logging
        1. Using Filters
        2. Creating a Filter
          1. Creating the filter class
          2. Declaring the filter in the deployment descriptor
          3. Packaging the filter
        3. Using Event Listeners
        4. Creating an Event Listener
          1. Creating the event listener class
          2. Declaring the event listener in the deployment descriptor
          3. Packaging the event listener
      3. Jakarta Commons Logging
        1. Installing the Commons Logging Package
        2. Using the Commons Logging API
        3. Struts Framework and the Commons Logging Package
      4. Using the log4j Package
        1. Integrating log4j with Struts
        2. What Are Loggers?
        3. Configuring log4j Appenders
        4. Understanding the log4j Log Levels
        5. Initializing log4j
          1. Initializing using the log4j.properties file
          2. Initializing using an XML file
          3. Specifying a relative versus an absolute path
          4. Synchronization issues
        6. Log File Rollover
      5. Using Commons Logging in JSP Pages
      6. The Performance Impact of log4j
      7. Third-Party log4j Extensions
      8. Java 1.4 Logging API
    17. 16. Packaging Your Struts Application
      1. To Package or Not to Package
        1. What Is Packaging?
        2. What Is Deployment?
      2. Deciding How to Package Your Application
        1. Namespace Management
        2. JSP File Placement
        3. Precompiling JavaServer Pages
          1. Precompiling JSP pages with Tomcat
          2. Precompiling JSP pages with Resin
          3. Precompiling JSP pages with WebLogic
        4. Packaging EJB Resources with Struts
      3. Packaging the Application as a WAR File
        1. Creating the WAR File
      4. Building Your Struts Applications with Ant
        1. What Is Ant?
        2. Installing and Configuring Ant
        3. Getting Started
        4. Compiling Java Source Files
        5. Using Ant to Build the WAR File
        6. Cleaning Up
      5. Creating an Automated Build Environment
        1. Using cron to Invoke Ant
        2. Using Microsoft Task Scheduler
      6. Restarting Your Server Remotely
    18. 17. Addressing Performance
      1. What Is Good Performance?
      2. Performance Versus Load Testing
      3. Performance- and Stress-Testing Tools
      4. Testing the Storefront Application
        1. Understand the Performance Goals
        2. Establish a Performance Baseline
          1. Taking a baseline
        3. Find the Trouble Areas
        4. Make the Necessary Changes to Software and Hardware
      5. Performance and Scalability Gotchas
        1. Request Scope Versus Session
        2. Using the synchronized Keyword
          1. Using java.util.Vector and java.util.Hashtable
        3. Using Too Many Custom Tags
        4. Improperly Tuning the JVM
        5. Using Too Many Remote Calls
        6. Using Too Many Graphics
    19. A. Changes Since Struts 1.0
      1. ActionServlet and RequestProcessor
      2. Modifications to the Struts Action Class
      3. Changes to web.xml and struts-config.xml
      4. New Features of Struts 1.1
        1. Declarative Exception Handling
        2. Dynamic ActionForms
        3. Plug-ins
        4. Multiple Application Modules
        5. Nested Tags
      5. The Struts Validator
        1. Change to the ORO Package
      6. Change to Commons Logging
      7. Removal of Admin Actions
      8. Deprecation of the GenericDataSource
      9. Dependency on Commons Projects
    20. B. Downloading and Installing Struts
      1. The Binary Versus Source Distributions
        1. Building Struts from the Source Distribution
        2. Installing the Binary Distribution
      2. Tips on Installing Struts in Tomcat
      3. Tips on Installing Struts in WebLogic
      4. Tips on Installing Struts in WebSphere
      5. Tips on Installing Struts in JRun
    21. C. Resources
      1. The Struts Mailing Lists
      2. The Struts Resource Web Page
      3. Validator Site
      4. Tiles Site
      5. Nested Tags Site
      6. The Struts Console
      7. Easy Struts Project
    22. Index
    23. Colophon