You are previewing Learning Jakarta Struts 1.2: a concise and practical tutorial.
O'Reilly logo
Learning Jakarta Struts 1.2: a concise and practical tutorial

Book Description

A step-by-step introduction to building Struts web applications for Java developers

  • Learn to build Struts applications right away

  • Build an ecommerce store step-by-step using Struts

  • Well-structured and logical progression through the essentials

  • In Detail

    Jakarta Struts is an Open Source Java framework for developing web applications. By cleanly separating logic and presentation, Struts makes applications more manageable and maintainable.

    Since its donation to the Apache Foundation in 2001, Struts has been rapidly accepted as the leading Java web application framework, and community support and development is well established.

    Struts-based web sites are built from the ground up to be easily modifiable and maintainable, and internationalization and flexibility of design are deeply rooted. Struts uses the Model-View-Controller design pattern to enforce a strict separation between processing logic and presentation logic, and enables efficient object re-use.

    The book is written as a structured tutorial, with each chapter building on the last. The book begins by introducing the architecture of a Struts application in terms of the Model-View-Controller pattern. Having explained how to install Jakarta and Struts, the book then goes straight into an initial implementation of the book store. The well structured code of the book store application is explained and related simply to the architectural issues.

    Custom Actions, internationalization and the possibilities offered by Taglibs are covered early to illustrate the power and flexibility inherent in the framework. The bookstore application is then enhanced in functionality and quality through the addition of logging and configuration data, and well-crafted forms. At each stage of enhancement, the design issues are laid out succinctly, then the practical implementation explained clearly. This combination of theory and practical example lays a solid understanding of both the principles and the practice of building Struts applications.

    This book is designed as a rapid and effective Struts tutorial for Java developers. The book builds a fully-featured web bookstore application incrementally, with each stage described step-by-step. Concepts are introduced simply and clearly as the design and implementation of this sample project evolves. The emphasis is on rapid learning through clear and well structured examples.

    Table of Contents

    1. Learning Jakarta Struts 1.2
      1. Learning Jakarta Struts 1.2
      2. Credits
      3. Preface
        1. Struts is Simple
        2. What You Should Know Beforehand
        3. Operating System and Tools
        4. Software Versions
        5. Code
        6. Exercises
        7. Struts as Magic Bullet
      4. About the Book
        1. What This Book Covers
        2. Conventions
        3. Reader Feedback
        4. Customer Support
          1. Downloading the Example Code for the Book
          2. Errata
          3. Questions
      5. 1. Introduction to Struts
        1. 1.1 The Apache Jakarta Project
        2. 1.2 The Struts Project
        3. 1.3 The Struts Framework
        4. 1.4 Model Classes
        5. 1.5 Design Patterns
        6. 1.6 The Model View Controller (MVC)
          1. Exercise 1
        7. 1.7 MVC versus Model 2
          1. Exercise 2
        8. 1.8 Controller Component: ActionServlet
        9. 1.9 Design Guidelines for Action Classes
        10. 1.10 Reflection, Introspection, and DynaBeans
        11. 1.11 Session
        12. 1.12 Business Logic
        13. 1.13 Threads
          1. Exercise 3
        14. 1.14 Expandability
        15. 1.15 Debugging Struts Applications
        16. 1.16 Some Experiences
          1. 1.16.1 Struts in Universities and Colleges
          2. 1.16.2 Struts in the Enterprise
          3. 1.16.3 Struts from a Tester’s Point of View
          4. 1.16.4 Struts in Project
            1. Developer 1: Björn
              1. Advantages
              2. Disadvantages
              3. Conclusion
            2. Developer 2: Thorsten
              1. Advantages
              2. Disadvantages
              3. Conclusion
        17. Summary
      6. 2. Hello Struts
        1. 2.1 Tomcat Server Installation
        2. 2.2 Installation of Struts
        3. 2.3 A Simple Page
          1. Listing 2.1: index.jsp
        4. 2.4 Internationalization with Struts
          1. Listing 2.2: Hello Struts (index.jsp)
          2. Exercise 1
        5. Summary
      7. 3. The Struts Shop
        1. 3.1 Why a Bookstore
        2. 3.2 Iterations
        3. 3.3 Elements of the Shop
          1. Exercise 1
        4. 3.4 Installing MySQL
        5. 3.5 Tables
          1. Exercise 2
        6. Summary
      8. 4. Internationalization and Taglibs
        1. 4.1 The Structure for the Shop
        2. 4.2 Internationalization
          1. 4.2.1 Internationalization and Java
            1. Listing 4.1: Test.java
          2. 4.2.2 Internationalization and Struts
        3. 4.3 The Welcome Page for the Shop
          1. Listing 4.2: index.jsp
          2. Listing 4.3: index.jsp
        4. 4.4 Custom Actions
          1. Listing 4.4: CountryDate.java
          2. Listing 4.5: taglib.tld
          3. Listing 4.6: index.jsp
        5. 4.5 struts-bean.tld
          1. Exercise 2
        6. Summary
      9. 5. Logging and Configuration
        1. 5.1 Configuration
          1. Exercise 1
        2. 5.2 Simple Logging
        3. 5.3 Logging Solutions
          1. Exercise 2
        4. 5.4 JDK Logging
          1. Exercise 3
        5. 5.5 Reading Configuration Settings
        6. 5.6 Logging with Log4J
          1. Exercise 4
        7. 5.7 Jakarta Commons Logging
        8. 5.8 Deciding What to Log
          1. Exercise 5
        9. Summary
      10. 6. Forms
        1. 6.1 Preparation
          1. Listing 6.1: Book.java (first version)
          2. Listing 6.2: struts-config.xml
          3. Listing 6.3: BookAction.java (first version)
          4. Listing 6.4: BookForm.java
          5. Exercise 1
        2. 6.2 Struts Form Beans
          1. 6.2.1 The Author Form
            1. Listing 6.5: The Struts Author Form
          2. 6.2.2 Formatting the Error Message Display
        3. 6.3 Database
          1. 6.3.1 Database and JDBC
            1. Listing 6.6: Author.java (with DB)
            2. Listing 6.7: struts-config.xml (with Book and Author entries)
          2. 6.3.2 Using Databases in Struts
            1. Listing 6.8: BookForm.validate()
            2. Listing 6.9: BookAction with database
            3. Listing 6.10: DataSources in struts-config.xml
            4. Exercise 2
        4. Summary
      11. 7. Logic
        1. 7.1 Value Comparison
          1. Listing 7.1: compare.jsp
        2. 7.2 Substring Matching
          1. Listing 7.2: substring.jsp
        3. 7.3 Redirection
          1. Listing 7.3: Conditional redirect
        4. 7.4 Collection Utilities
          1. Listing 7.4: iterate.jsp
        5. 7.5 Sorting Tags
          1. Listing 7.5: Book.compareTo()
          2. Listing 7.6: BookCatalog.java
          3. Listing 7.7: BookCatalog.jsp
          4. Exercises
        6. Summary
      12. 8. Exceptions
        1. 8.1 Tomcat Error Pages
          1. Listing 8.1: Configuring Tomcat error pages
        2. 8.2 Custom Exceptions
        3. 8.3 Exception Handling
          1. Listing 8.2: DuplicateKeyException.java
          2. Listing 8.3: DuplicateKeyException.java (extends Exception)
          3. Listing 8.4: DuplicateKeyExceptionHandler
          4. Listing 8.5: BookAction.execute()
          5. Listing 8.6: errors/duplicateKeyError.jsp
          6. Listing 8.7: errors/Error.jsp
          7. Listing 8.8: DuplicateKeyExceptionHandler.java
        4. 8.4 ActionErrors
          1. Listing 8.9: saveErrors()
          2. Exercise
        5. Summary
      13. 9. Controller and Templates
        1. 9.1 Consistent Layout without Struts
          1. 9.1.1 Why Central Control?
            1. Listing 9.1: StartServlet.java
            2. Exercises
        2. 9.2 Central Controller with Struts
          1. Listing 9.2: Definition of ActionServlet in web.xml
        3. 9.3 Consistent Layout with Struts Tiles
          1. 9.3.1 Header, Main, and Footer
            1. Listing 9.3: Layout with includes
            2. Listing 9.4: Layout.jsp
            3. Listing 9.5: Example.jsp
          2. 9.3.2 Definitions in a JSP
            1. Listing 9.6: Layout.jsp
            2. Listing 9.7: tiles-defs.xml
            3. Listing 9.8: TilesDefinitions.jsp
            4. Listing 9.9: CreateAuthor.jsp
            5. Listing 9.10: CreateBook.jsp
            6. Listing 9.11: Footer.jsp
            7. Listing 9.12: Header.jsp
            8. Exercises
        4. Summary
      14. 10. Putting It All Together
        1. 10.1 Authorization
          1. 10.1.1 Authorization for Tomcat
            1. Listing 10.1: Authorization with web.xml
            2. Exercise 1
          2. 10.1.2 An Excursion into Session
            1. Listing 10.2: index.jsp
          3. 10.1.3 Customers with Application-level Authorization
            1. Listing 10.3: Customer Table
            2. Listing 10.4: Customer.java
            3. Listing 10.5: CustomerForm.java
            4. Listing 10.6: CustomerAction.execute()
        2. 10.2 BookCatalog
          1. Listing 10.7: BookCatalog.java
          2. Listing 10.8: view/BookCatalogView.jsp
          3. Exercise 2
        3. 10.3 Price Information
                1. Listing 10.9: PriceFormat.java
            1. Exercise 3
        4. 10.4 The Shopping Cart
          1. 10.4.1 Login
            1. Listing 10.10: view/Login.jsp
            2. Listing 10.11: DynaAction in struts-config.xml
            3. Listing 10.12: LoginAction.execute()
          2. Exercise 4
        5. 10.5 Using the Shopping Cart
                1. Listing 10.14: Shoppingcart table
                2. Listing 10.15: ‘Buy’ link in ViewBookCatalog.jsp
            1. Exercise 5
        6. 10.6 Placing Orders
            1. Exercise 6
                1. Listing 10.17: FulfillOrderAction.execute()
        7. Summary
      15. 11. Struts Validator and Plug-In Classes
        1. 11.1 Verifying User Input with the Struts Validator
                1. Listing 11.1: Validator error messages
                2. Listing 11.2: Author.jsp with Validator
            1. Exercise 1
                1. Listing 11.3: WEB-INF/validation.xml
                2. Listing 11.4: Modified rule for the ID
          1. 11.1.1 JavaScript
            1. JavaScript with the Validator
        2. 11.2 Plug-In Classes
                1. Listing 11.5: struts/MyStrutsPlugin.java
            1. Exercise 2
        3. Summary
      16. 12. JSTL
        1. 12.1 Installation
        2. 12.2 Internationalization
                1. Listing 12.1: Hello JSTL
            1. Exercise 1
        3. 12.3 Java Classes
            1. Exercise 2
        4. 12.4 Iteration
                1. Listing 12.2: Book catalog without Java
            1. Exercise 3
        5. 12.5 JSTL and Struts-EL
                1. Listing 12.3: Iteration with Struts EL and JSTL
                2. Listing 12.4: logic-compare.jsp
        6. Summary
      17. 13. Tools and Tricks
        1. 13.1 Code Conventions
        2. 13.2 The Struts Console
            1. Exercise 1
        3. 13.3 Struts in WSAD (Eclipse)
        4. 13.4 StrutsDoc and Ant
          1. 13.4.1 Installing Ant
          2. 13.4.2 Java API Generation
                1. Listing 13.1: build.xml
          3. 13.4.3 StrutsDoc
                1. Listing 13.2: StrutsDoc target
        5. 13.5 Display Tags
        6. 13.6 Workflow
        7. 13.7 Testing: HTTPUnit
                1. Listing 13.3: Test.java (HTTPUnit)
                2. Listing 13.4: Test result
                3. Listing 13.5: LinkChecker.java
        8. 13.8 Different Struts Functions
          1. 13.8.1 Switching Languages
            1. Exercise 2
                1. Listing 13.6: Locale allocation in struts-config.xml
                2. Listing 13.7: LocaleAction.execute()
            2. Exercise 3
          2. 13.8.2 Uploading The Files
                1. Listing 13.8: struts.UploadForm.java
                2. Listing 13.9: struts.UploadAction.java
                3. Listing 13.10: view.UploadView.jsp
          3. 13.8.3 Precompilation
                1. Listing 13.11: tools.Precompiler.java
                2. Listing 13.12: resources/urls.properties
            1. Exercise 4
          4. 13.8.4 Validation of struts-config.xml and web.xml
        9. 13.9 XSLT Configuration Overview
          1. 13.9.1 struts-config.xml
                1. Listing 13.13: StrutsForwards.xsl
            1. Exercise 5
          2. 13.9.2 web.xml
                1. Listing 13.14: Mappings in web.xml
                2. Listing 13.15: ServletMappings.xsl
        10. 13.10 Extending ActionServlet
                1. Listing 13.16: MyActionServlet.java
        11. Summary
      18. A. Solutions
        1. Chapter 2: Hello Struts
          1. Exercise 1
        2. Chapter 3: The Struts Shop
          1. Exercise 2
                1. Listing A.1: Orders.txt
        3. Chapter 4: Internationalization and Taglibs
          1. Exercise 1
          2. Exercise 2
        4. Chapter 5: Logging and Configuration
          1. Exercise 1
                1. Listing A.2: PropertiesManager.java
          2. Exercise 4
          3. Exercise 5
        5. Chapter 6: Forms
          1. Exercise 2
        6. Chapter 7: Logic
          1. Exercise 1
        7. Chapter 9: Controller and Templates
        8. Chapter 10: Putting it All Together
          1. Exercise 4
          2. Exercise 5
        9. Chapter 12: JSTL
          1. Exercise 3
      19. B. Glossary
        1. Listing B.1 Header.jsp
      20. C. Literature