Cover image for Programming Jakarta Struts, Second Edition

Book description

While the look and feel of an enterprise web application is certainly important, developers usually find themselves spending far too much time on the front-end presentation before they can get to coding the good stuff--the business logic at the heart of the program. Jakarta Struts addresses this issue by combining Java Servlets, Java ServerPages (JSP), custom tags, and messaging resources (like Java Message Service) into a unified, re-usable framework. The result is a cooperative, synergistic platform that's efficient and suitable for independent developers, large development teams, and everyone in between. The Struts Framework has become a highly popular open source project, but there's still woefully little documentation on the technology. What does exist is far too basic and lacks critical information for developers like you writing today's complex web applications. The revised and expanded Programming Jakarta Struts, 2nd Edition covers everything the successful earlier edition did--including an overview of the concepts involved in writing web applications; installation and configuration instructions for getting Struts up and running; a thorough discussion of how Struts implements the Model-View-Controller (MVC) design paradigm (known as the Model 2 approach) and how to interface with that pattern in your own applications; Logging, Validation, and Exception Handling with Struts; using Tiles; writing internationalization and localization code using Struts; and practical, real-world best practices for web applications--as well as plenty more: now fully up to date with Struts 1.1, this edition covers the latest material on tag libraries and the new JavaServerFaces (JSF) APIs and even includes all-new chapters on JSF, JSTL/EL, and security. With each enterprise system he s developed, author Chuck Cavaness has spent many grueling hours learning invaluable lessons about Struts and figuring out the dos and the don'ts of building web applications. He saves you time and headaches by sharing that invaluable real-world experience here, with his realistic, practical, here's how to do it approach to using the Struts Framework to its fullest potential.

Table of Contents

  1. Programming Jakarta Struts, 2nd Edition
    1. Preface
      1. Organization
      2. Conventions Used in This Book
      3. Using Code Examples
      4. Comments and Questions
      5. 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?
        1. Creation of the Struts Framework
        2. Contribution to the Struts Project
      7. 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. Spring Framework
        11. 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. Using Multiple Struts Configuration Files
        5. Configuring the Tag Libraries
        6. Setting Up the Welcome File List
          1. Using a Struts action in the welcome file list
        7. Configuring Error Handling in web.xml
      6. The Struts Configuration File
        1. Configuring Multiple Application Modules
        2. The org.apache.struts.config Package
        3. The ApplicationConfig Class
        4. 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
        5. A Complete struts-config.xml File
        6. Using Multiple Application Modules
        7. Specifying a DOCTYPE Element
      7. The Digester Component
      8. The Struts Console Tool
      9. 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
        4. The ActionForward class
        5. Creating Multithreaded Action Classes
        6. Business Logic and the Action Class
        7. 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?
        1. Building the Storefront Model
        2. Accessing a Relational Database
        3. Creating the Storefront Business Objects
        4. The Storefront Data Model
        5. Mapping the Business Objects to the Database
        6. Object-to-Relational Mapping Frameworks
        7. The Storefront Persistence Framework
        8. The Business Delegate and DAO Patterns in Action
        9. 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. Nested Tags
      8. Other Useful Tag Libraries
        1. The Jakarta Taglibs Project
        2. JSPTags.com
      9. The JSP Standard Tag Library (JSTL)
        1. JSTL Core Tags
        2. JSTL Internationalization Tags
        3. JSTL XML Tags
        4. JSTL SQL Tags
        5. Functions
        6. A New Expression Language
        7. JSTL Requirements
        8. 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
        2. Extending Model Components
        3. The UserContainer and ApplicationContainer Classes
      5. 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
      2. Performance Impact of Exception Handling
      3. System Versus Application Exceptions
      4. Using Chained Exceptions
        1. Dealing with Multiple Exceptions
      5. Exception Handling Provided by Struts
        1. Declarative Versus Programmatic Exception Handling
        2. Using Programmatic Exception Handling
      6. Tying Up the Loose Ends
        1. Handling Remote Exceptions
        2. Exceptions in Custom Tags
          1. The TryCatchFinally interface
        3. Internationalized Exception Handling
      7. 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
        3. The validation.xml file
        4. 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
          1. The definitions-debug property
          2. The definitions-parser-details property
          3. The definitions-parser-validate property
          4. The moduleAware property
          5. The definitions-factory-class property
      3. Using Tiles
        1. What Is a Tile?
        2. Using a Layout Tile
        3. 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?
        3. Deciding How to Package Your Application
        4. Namespace Management
        5. JSP File Placement
        6. Precompiling JavaServer Pages
          1. Precompiling JSP pages with Tomcat
          2. Precompiling JSP pages with Resin
          3. Precompiling JSP pages with WebLogic
        7. Packaging EJB Resources with Struts
      2. Packaging the Application as a WAR File
        1. Creating the WAR File
      3. 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
      4. Creating an Automated Build Environment
        1. Using cron to Invoke Ant
        2. Using Microsoft Task Scheduler
      5. 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
        3. Using java.util.Vector and java.util.Hashtable
        4. Using Too Many Custom Tags
        5. Improperly Tuning the JVM
        6. Using Too Many Remote Calls
        7. Using Too Many Graphics
    19. 18. JavaServer Faces
      1. Struts and JavaServer Faces
        1. Do We Really Need Another UI Framework?
        2. The Big-Picture Benefits of JSF
      2. Overview of JSF Architecture
        1. The JSF Request/Response Lifecycle
      3. Installing and Running the Example Struts-Faces Application
      4. Converting Existing Struts Applications to JSF
      5. Further Reading
    20. 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. Action Statics Changed
      5. TagUtils and ModuleUtils
      6. New Features of Struts 1.1
        1. Declarative Exception Handling
        2. Dynamic ActionForms
        3. Plug-ins
        4. Multiple Application Modules
        5. Nested Tags
      7. The Struts Validator
      8. Change to Commons Logging
      9. Removal of Admin Actions
      10. Deprecation of the GenericDataSource
      11. Dependency on Commons Projects
    21. 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
    22. C. Resources
      1. The Struts Mailing Lists
      2. The Struts Resource Web Page
      3. Tiles Site
      4. Nested Tags Site
      5. The Struts Console
      6. Easy Struts Project
    23. Index
    24. Colophon