You are previewing Apache Struts 2 Web Application Development.
O'Reilly logo
Apache Struts 2 Web Application Development

Book Description

A beginner’s guide for Java developers

  • Design, develop, test, and deploy your web applications using Struts 2 framework

  • No prior knowledge of JavaScript and CSS is required

  • Apply the best of agile development techniques and TDD techniques

  • Step-by-step instructions and careful explanations with lots of code examples

In Detail

Struts 2.1 is a modern, extensible, agile web application framework suitable for both small- and large-scale web applications.

The book begins with a comprehensive look at Struts 2.1 basics, interspersed with detours into more advanced development topics. You'll learn about configuring Struts 2.1 actions, results, and interceptors via both XML and Java annotations. You'll get an introduction to most of the Struts 2.1 custom tags and learn how they can assist in rapid application prototyping and development.

From there you'll make your way into Struts 2.1's strong support for form validation and type conversion, which allows you to treat your form values as domain objects without cluttering your code. A look at Struts 2.1's interceptors is the final piece of the Struts 2.1 puzzle, allowing you to leverage the standard Struts 2 interceptors as well as implement your own custom behavior.

After covering Struts 2.1 you'll journey into the world of JavaScript, a surprisingly capable language, the Document Object Model (DOM), and CSS, and learn how to create clean and concise client-side behavior. You'll leverage that knowledge as you move on to Struts 2 themes and templates, which give you a powerful way to encapsulate site-wide user interface behavior.

The book closes with a look at some tools that make the application development life cycle easier to manage, particularly in a team environment, and more automatic.

Table of Contents

  1. Apache Struts 2 Web Application Development
  2. Credits
  3. About the Author
  4. Acknowledgement
  5. About the Reviewers
  6. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code for the book
      2. Errata
      3. Piracy
      4. Questions
  7. 1. Struts and Agile Development
    1. Struts 2 in a nutshell
      1. The filter dispatcher
      2. Interceptors
      3. Actions
      4. Results
      5. Plug-ins
    2. Agile in a nutshell
      1. User stories
      2. Testing
      3. Refactoring
      4. Short iterations
    3. Real applications in a nutshell
      1. Making it pretty
      2. JavaScript
      3. Documentation
      4. All of the rest
    4. Getting started
      1. Creating our own applications
        1. Doing it "by hand"
        2. Using Maven
    5. Summary
  8. 2. Basic Configuration
    1. Setting up our environment
      1. A sanity-checking application
      2. Configuring web.xml for Struts 2
      3. Writing our first action
        1. Configuring our first action with XML
        2. Configuring our result
        3. Choosing an action method
    2. Getting started with our application
      1. Gathering user stories—defining our application
      2. Building skeletal applications using wildcards
      3. Matching multiple wildcards
      4. More wildcard tricks
      5. Packages and namespaces
      6. Creating site navigation
      7. Including external configuration files
      8. Our application so far
      9. Examining our configuration
      10. Configuration via convention and annotations
        1. The Convention Plug-in and action-less actions
        2. The Convention Plug-in and action configuration
        3. Configuring the Convention Plug-in with annotations
    3. Summary
  9. 3. Actions and ActionSupport
    1. ActionSupport and its interfaces
      1. The Action interface
        1. Action's convenience strings
      2. The TextProvider interface
      3. Detour—action properties, JSPs, and more tags
      4. Continuing with message lookup
      5. Parameterized messages
      6. The LocaleProvider interface
      7. The Validateable and ValidationAware interfaces
    2. Implementing our first user story
      1. Refining our story
      2. Creating the recipe form
      3. Adding some validation
      4. Displaying our error messages
    3. More action interfaces
      1. Detour—creating the list of interfaces to explore
        1. Leveraging the IDE
        2. Using the command line
        3. Examining class files
    4. Additional action interfaces
      1. Preparable interface
      2. Accessing scoped attributes (and request parameters)
      3. Accessing servlet objects
      4. Request parameter/action property filtering
    5. Summary
  10. 4. Results and Result Types
    1. The dispatcher result type
    2. The redirect result type
    3. The redirectAction result type
    4. The chain result type (action chaining)
    5. The parse parameter and a usecase detour
      1. Displaying the form
      2. Coding our action
      3. Configuring our success result
      4. Type conversion sneak attack
      5. Coding the show action
    6. The FreeMarker and Velocity result types
      1. FreeMarker result configuration
    7. The XSLT result type
    8. The plaintext result
    9. The stream result
    10. The httpheader result
    11. The Tiles and JasperReports results
    12. Creating custom result types
      1. Configuring our custom result type
      2. Writing the action
      3. Implementing our markdown result type
    13. Summary
  11. 5. OGNL, the Value Stack, and Custom Tags
    1. OGNL
      1. Contents of the value stack and the <s:property> tag
        1. Escaping values
        2. Default values
        3. Escaping values for JavaScript
      2. Other value stack objects and the debug tag
      3. A dirty EL trick
    2. The <s:set> tag
    3. Calling static methods from OGNL
    4. Conditionals
    5. Collections and iteration
      1. The <s:iterator> tag
        1. Tracking iteration status
        2. CSS detour: Alternating table row background color
        3. The <s:generator> tag
        4. It's not a list, it's an iterator!
        5. Silent death
        6. Another potential stumper (Struts 2.0 only)
        7. What is <s:generator> for?
      2. The <s:append> tag
      3. The <s:merge> tag
      4. The <s:subset> tag
        1. Arbitrary filtering with the <s:subset> tag
        2. Dirty OGNL secrets
      5. The <s:sort> tag
      6. Are the collection tags useful?
    6. Referencing other pages and actions
      1. The <s:include> tag
      2. The <s:action> tag
      3. The <s:url> tag
    7. Summary
  12. 6. Form Tags
    1. Form tag basics
      1. The "xhtml" theme in a nutshell
      2. The <s:head> tag
      3. The <s:form> tag
      4. Common input element tag attributes
        1. Values, labels, and names (and keys)
        2. All the rest
      5. Basic rendering
        1. But I don't want tables
      6. Basic form input elements
        1. The <s:textfield>, <s:password>, and <s:hidden> tags
        2. The <s:textarea> tag
        3. The <s:label> tag
        4. The <s:radio> tag
        5. The <s:checkbox> tag
        6. The <s:checkboxlist> tag
        7. Using the <s:checkboxlist> tag to implement a user story
        8. The <s:select> tag
        9. The <s:optgroup> tag
        10. The <s:submit> tag
        11. The <s:reset> tag
      7. Combination form tags
        1. <s:combobox> tag
        2. The <s:updownselect> tag
        3. The <s:optiontransferselect> tag
        4. The <s:doubleselect> tag
      8. Uploading files
      9. Preventing double submits with the <s:token> tag
    2. Summary
  13. 7. Form Validation and Type Conversion
    1. Form validation
      1. Manual validation
      2. Configuring XML validation
        1. Customizing validation messages
      3. What validations are available?
        1. The requiredstring validator
        2. The stringlength validator
        3. Detourplaying games with validation messages
        4. The required and int validators
        5. But wait, there's more
        6. The double validator
        7. The email validator
        8. The url validator
        9. The date validator
        10. The regex validator
        11. The expression and fieldexpression validators
        12. Combining validation methods
        13. The conversion validator
        14. The visitor validator
      4. Configuring validation with annotations
        1. The @Validation annotation
        2. The @Validations annotation
        3. The @SkipValidation annotation
        4. The @RequiredFieldValidator annotation
        5. The @IntRangeFieldValidator annotation
        6. The @DoubleRangeFieldValidator annotation
        7. The remaining validation annotations
      5. Client-side validation
      6. Custom validators
    2. Type conversion
      1. The problem
      2. The solution
      3. Defining our own converters
        1. Type conversion usecase&mdash;trimmed BigDecimals
        2. Configuring conversion across the entire application
        3. Custom type conversion is handy
      4. Collections
        1. Usecase&mdash;adding a list of ingredients
          1. Why add addIngredient()?
        2. Updating our new recipe form
        3. Map-based collections
    3. Summary
  14. 8. Interceptors
    1. The basics
      1. Configuring interceptor stacks
      2. Configuring interceptors
      3. Configuring interceptors for individual actions
      4. How interceptors work
    2. Interceptors in the default stack
      1. The exception interceptor
      2. The alias interceptor
      3. The servletConfig interceptor
      4. The prepare interceptor
      5. The i18n interceptor
      6. The chain interceptor
      7. The debugging interceptor
      8. The profiling interceptor
      9. The scopedModelDriven interceptor
      10. The modelDriven interceptor
        1. Getting back to the scopedModelDriven interceptor
      11. The fileUpload interceptor
      12. The checkbox interceptor
      13. The staticParams interceptor
      14. The params interceptor
        1. Ordered parameters and ad hoc factory patterns
      15. The conversionError interceptor
      16. The validation interceptor
      17. The workflow interceptor
    3. Other important interceptors and interceptor stacks
      1. The token interceptor
      2. The store interceptor
      3. The roles Interceptor
      4. The clearSession interceptor
      5. The paramsPrepareParamsStack interceptor stack
    4. Writing our own interceptors
      1. The trim interceptor
      2. Configuring the trim interceptor
      3. The Test Action
      4. Testing the trim interceptor
      5. Modifying application flow with interceptors
        1. Configuring the result
        2. Writing the ResultMappingInterceptor
        3. Writing the ResultMappingInterceptor and making it work
    5. Summary
  15. 9. Exceptions and Logging
    1. Handling exceptions in Struts 2
      1. Global exception mappings
      2. Action-specific exception mappings
      3. Accessing the exception
      4. Architecting exceptions and exception handling
        1. Checked versus unchecked exceptions
        2. Application-specific exceptions
        3. Abstracting underlying exceptions
      5. Handling exceptions
    2. Logging
      1. Introduction to logging
        1. Using the loggers
        2. Configuring the loggers
    3. Summary
  16. 10. Getting Started with JavaScript
    1. Introduction to JavaScript
      1. Playing with JavaScript
    2. Minor syntax and language notes
      1. Unicode
      2. Whitespace
      3. Semicolons
      4. Null and undefined values
      5. The equal and strict equal operators
      6. The logical OR operator
    3. Variables and scoping
      1. JavaScript data types
        1. Numbers
          1. Decimal, hex, octal
          2. Conversions
        2. Strings
          1. Length and conversions
          2. Conversions to other types
        3. Arrays
          1. Array functions
    4. Exception handling
    5. Introduction to JavaScript objects and OOP
      1. Open objects and object augmentation
      2. Object values can be functions
      3. Object maker functions
    6. Functions
      1. Function parameters
        1. Some trickery
      2. Inner functions
      3. Closures
    7. Introduction to JavaScript classes
      1. Creating classes
        1. Variable and function access
        2. JavaScript's "this" keyword
      2. Prototypes
    8. JavaScript modules and OOP
      1. Creating a namespace
    9. Summary
  17. 11. Advanced JavaScript, the DOM, and CSS
    1. The importance of markup
      1. ID or style attribute?
      2. Dressing up our form
        1. JavaScript modules and jQuery
      3. Adding onclick handlers
        1. Using a function builder
          1. Of what value is this
        2. Accessing module data
        3. The final trick
    2. Adding dynamic form elements
      1. Identifying where to add the elements
      2. The JavaScript "Recipe" module
    3. Summary
  18. 12. Themes and Templates
    1. Extracting the templates
    2. A maze of twisty little passages
      1. Creating our theme
    3. Other uses of templates
    4. Summary
  19. 13. Rich Internet Applications
    1. What this chapter is and isn't
    2. Dojo tags
      1. Simple use cases really are simple
      2. The Dojo <sx:head> tag
      3. The Dojo <sx:a> tag
      4. A brief side journey on topics
      5. The Dojo <sx:div> tag
        1. Finishing our user story
        2. Highlighting the need to know
      6. Dojo and forms
    3. The REST plug-in
      1. REST in a nutshell
      2. The REST plug-in in a nutshell
        1. REST plug-in URLs
        2. REST plug-in results
      3. A web browser client example
        1. The REST controller
        2. REST controller responses
      4. An example of a useful client
      5. A command-line example
      6. Custom content handler example
        1. YAML in a nutshell
        2. Writing our YAML handler
        3. Configuring our YAML handler
        4. Handling our YAML
    4. Summary
  20. 14. Comprehensive Testing
    1. Test-driven development
      1. Pay as we go
    2. Unit testing
    3. Unit tests focus code intent
      1. Keep tests small
      2. Test, code, refactor&mdash;the "heartbeat" of TDD
      3. JUnit
        1. Revisiting our iterator filter
        2. The test environment
        3. The initial test stub
        4. Testing vowel recognition
        5. Testing non-string parameter exceptions
        6. Test granularity and test setup
      4. TestNG
      5. Legacy code and unit testing
      6. Simple action testing
        1. Detour: Dependency Injection (Inversion of Control)
        2. Dependency Injection helps us test
      7. Detour: Struts and Spring in a nutshell
        1. Spring web.xml configuration
        2. Spring context configuration file
    4. Testing Struts 2 in context
      1. Testing a Struts interceptor
    5. Client-side (functional) testing
      1. Selenium
        1. Selenium RC
        2. The test scenario
        3. Our first Selenium test
    6. Other forms of testing
      1. Acceptance testing
      2. Load testing
        1. Load testing with JMeter
      3. Recovery testing
    7. Summary
  21. 15. Documenting our Application
    1. Documenting Java
      1. Self-documenting code
        1. Document why, not what
        2. Make your code read like the problem
        3. Contract-oriented programming
          1. SpringContracts
          2. Getting into details
      2. Javadocs
        1. Always write Javadocs!
        2. The first sentence
        3. Add information beyond the API name
        4. Write for multiple formats
        5. Generating targeted Javadocs
          1. Visibility
          2. The -exclude argument
        6. The -use argument
        7. Creating new Javadoc tags with the -tag argument
        8. Never write Javadocs!
        9. Never write inline Java comments!
      3. Using UML
        1. Package diagrams
        2. Class diagrams
        3. Sequence diagrams
    2. Documenting web applications
      1. High-level overviews
      2. Documenting JSPs
      3. Documenting JavaScript
      4. Documenting interaction
        1. More UML and the power of scribbling
        2. Don't spend so much time making pretty pictures
      5. User documentation
    3. Documenting development
      1. Source code control systems
        1. Code and mental history
        2. Commit comment commitment
        3. When (and what) do we commit
        4. Branching
        5. Branching discipline
      2. Issue and bug management
        1. Linking to the SCCS
      3. Wikis
      4. RSS and IRC/chat systems
      5. Word processor documents
    4. Summary