Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Developing Jakarta Struts (from 2003)

Developing Jakarta Struts (from 2003)

This is a presentation given to the Central Iowa Java Users group in 2003. (The technology here is thus a little dated - but it's nice to have this one archived.)

Chad Thompson

January 11, 2013
Tweet

More Decks by Chad Thompson

Other Decks in Programming

Transcript

  1. Chad Thompson Central Iowa Java Users Group [email protected] Developing Web

    Applications with Jakarta Struts Thursday, September 6, 12
  2. A special “Thank You” to Lynn Miner of Impact Innovations

    for providing food to our user group meeting! As always, any presentation/meeting suggestions are welcome: [email protected] A thought we had: A meeting consisting of “shorter” (20 min?) presentations to introduce new tech, brief overviews.... Before we begin.... Thursday, September 6, 12
  3. Chad Thompson Central Iowa Java Users Group [email protected] Developing Web

    Applications with Jakarta Struts Thursday, September 6, 12
  4. Web Application Frameworks Struts Basics (ActionServlet, Actions, Forms) Developing A

    Struts Application Messages/Internationalization Additional Complimentary Tech. (if time allows) Along the Way: Tools, Tips, Best Practices Roadmap Thursday, September 6, 12
  5. Frameworks have evolved with Java Server technology JSP/Servlets are HARD!

    (Compare CFM/ASP) Frameworks define re-usable components to make this job easier. A good framework defines how components work to create a usable application. Why Frameworks? Thursday, September 6, 12
  6. At this point, there are several mature web frameworks to

    choose from. A mature framework can provide functionality that while not “hard to implement”, YOU won’t have to. A note of caution: When adopting a framework, you are adopting a MENTAL MODEL of application development. GET TO KNOW YOUR MODEL! Adopting a Framework Thursday, September 6, 12
  7. Barracuda - A Model 2 Approach Similar to Struts, with

    event notification mechanism, XMLC (http://barracuda.enhydra.org) WebWork - A framework based on a view “pulling” data from a model (http://mav.sourceforge.net) Jakarta Turbine - A Servlet-Based Framework (http://jakarta.apache.org/turbine) Building Your Own (!!) Important Point: Do your research! Alternative Frameworks Thursday, September 6, 12
  8. An Open Source Application Framework Focused Entirely on Servlet/JSP Tech.

    Current Revision: 1.1 Release Candidate 1 http://jakarta.apache.org/struts What Is Struts? Thursday, September 6, 12
  9. “Model 2” Web Application Development Use JSPs to Handle “View”

    Tasks Use Servlets for Programmatic Tasks Similar to Smalltalk MVC (and in other ways, Swing/GUI development) Goal of Struts: Provide a simple, extensible framework for Model 2 Web Application Development. Forward to the Past! Thursday, September 6, 12
  10. A Central Controller (ActionServlet) Configured via XML A Delegating Mechanism

    (Actions) Can Unit Test at this Level User Interaction (JSPs/ActionForms) Built-In Validation Mechanisms Built-In Internationalization Mechanism What Does Struts Provide? Thursday, September 6, 12
  11. A Large Set of Custom JSP Tags A mechanism for

    defining re-usable JSP Layouts (The “Tiles” framework) A flexible view mechanism Example: Velocity Templates/XSL Methods Java Standard Tag Library (JSTL) Interoperability w/ other Jakarta Projects Thursday, September 6, 12
  12. Struts is now at “1.1, Release Candidate 1” Struts 1.1

    is “Ready For Prime Time” Important New Features of Release 1.1 Several “Commons” Packages Created Integrated Tiles Layout Framework Support for Multiple Application Modules! Application can be “divided” into several sub applications Some New Features... Thursday, September 6, 12
  13. Controller ActionServlet (XML Configured) Actions - Interact w/ Business Model

    View JSPs, Custom Tags ActionForms (Custom or “Dyna”) Model Up to you! (POJOs, EJBs, JDOs, etc.) Struts Components Thursday, September 6, 12
  14. More to Developing Than Just the Framework! Items to Consider

    (not unique to Struts): Directory Structure Building and Deploying (Tools: Ant/Tomcat) Unit Testing (JUnit/StrutsTestCase/?) Application Logging Development Environment? Developing An Application Thursday, September 6, 12
  15. Create a Directory Structure Packaged (.WAR) or Exploded? Create a

    Build Script to Manipulate Files Advice: Follow Tomcat Example Closely “Application Developers Guide” Create web.xml to deploy ActionServlet Create the Struts Configuration File Example: Gradebook Application Setting Up Thursday, September 6, 12
  16. The Struts Configuration file is used to control application flow.

    Action Mappings, Form Definitions, etc. The Struts Configuration File can be confusing and difficult to maintain! Solution: Struts Console! http://www.jamesholmes.com/struts Other Alternatives: Check “Resources” Struts Configuration Thursday, September 6, 12
  17. What is an Action? An action is a Java class

    that does the “work” of the application. Can be complicated, or very simple Struts has several “built in” actions First Example: An “entry” action to a login screen. Developing A First Action Thursday, September 6, 12
  18. Goal: Application Log On Application needs to Authenticate a User

    Establish a User Session Solution: Develop a “LogonAction” that takes input from a form, and performs validation. Executing a Task Thursday, September 6, 12
  19. We need to pass & validate a username & password

    The information passed to the server is expressed in terms of an “ActionForm” Gradebook Logon Thursday, September 6, 12
  20. The View: A JSP! Typically, JSPs can be laid out

    using HTML tools. (e.g. Dreamweaver) Struts Provides Custom Tags to Interact with the ActionServlet (thus, Actions) Interaction between JSP and Action is represented by an “ActionForm” Struts View Components Thursday, September 6, 12
  21. An overview of “important” tags <html:.../> Easy ways of associating

    forms, links, images, etc. with Struts environment <bean:.../> A way to interact with JavaBeans, or Struts Resources Struts Custom Tags Thursday, September 6, 12
  22. <logic:.../> Provides conditional/iterator functions An important substitute/compliment: JSTL “Core”, XML,

    Formatting, SQL A Simple Expression Language Struts Custom Tags are evolving toward JSTL standards (i.e. “Struts EL” tags) Thursday, September 6, 12
  23. Represent Data passed from client entry to the server. (Typically;

    Enter into a JSP) Creating ActionForms Create an object that extends ActionForm Use a “DynaForm” (Essentially, a hash) Struts ActionForm Thursday, September 6, 12
  24. ... are NOT the application “model” ... can perform validation

    Implement validate() method Use the Commons Validator Framework! ... do not have to be coded. The new Struts 1.1 “Dyna” forms essentially provide hash- like functionality. Dyna Forms can be coupled with Validator Framework! ActionForms... Thursday, September 6, 12
  25. You want to be sure that users can be prompted

    to enter data that is valid to your application. Typically broken into functional validation and application validation. Functional Validation: Override ActionForm.validate() Use the included Validator Framework Validating Form Data Thursday, September 6, 12
  26. Gradebook Logon Performs Simple Validation Has the data been entered?

    Back To Example... .... More On Error Handling/Validation Later Thursday, September 6, 12
  27. A Common Misunderstanding: Struts DOES NOT provide for your application

    model! Struts “Action” classes perform interaction with model, HTTP environment Think of Action classes as acting in the classic “Façade” pattern of Gamma, et. al. An Action takes input, submits to the model, then “takes action” on the result. Interacting With Model Components Thursday, September 6, 12
  28. Goal: Interface with a persistence class in order to verify

    a user login. Action Simply Interacts with Data Object Example: LogonAction Thursday, September 6, 12
  29. Plug In Mechanism Allows “PlugIns” to initialize application context. Extending

    Views: Use Custom Tags! Take Care: Extending the Framework in “unusual” ways may interfere with upgrading to newer versions of the framework! Example: Several Struts 1.0 Applications extend the ActionServlet class itself Extending the Framework Thursday, September 6, 12
  30. Struts uses “Message Resources” to output text messages (or titles,

    or fields, etc...) Advantage: Internationalization! Struts has a ‘default’ Resources File, but you can also create international files by registering Domain/Language files (e.g. Resources_en_US.properties) What’s Happening? Thursday, September 6, 12
  31. Application Errors Example: Failed Validation Use the “ActionErrors” class to

    pass errors back to calling Action ActionErrors can be global or field-specific Thursday, September 6, 12
  32. The Action Servlet can take action if a certain exception

    is thrown. Can be global or Action-based Can either be a simple forward, or passed to a custom error handler class. Application Exceptions Thursday, September 6, 12
  33. Tiles is a way to define “reusable” JSP components. Idea:

    Create a template that contains holders for variables. These variables are other documents (JSP/HTML/Text) that can be placed in the main layout Tiles adds functionality to “<jsp:include/>” Re-usable layouts make design much simpler! Tiles is included with Struts Distribution! Tiles Layout Framework Thursday, September 6, 12
  34. Tiles Definitions can be created to make management of complex

    layouts simpler. Definitions can be either in JSPs, or in an external XML definition. Definitions can also be extended, or overridden. http://www.oreilly.com/catalog/jakarta/ chapter/ch14.pdf (free!) Tiles Definitions Thursday, September 6, 12
  35. Commons Validation The Commons Validation Framework is a way to

    define reusable validation rules. (e.g. What is a valid e-mail address?) The Validation Framework Relies on two configuration files validation.xml: Ties rules to application validator-rules.xml: Defines Rules! Thursday, September 6, 12
  36. Rules are tied to specific fields in a form Simple

    Example: The Field is Required Can Also Define “Masks” (Regular Expressions) Thursday, September 6, 12
  37. Basic Validation Rules are included with the Struts distribution. (e.g.

    “required”, “minLength”, “maxLength”, etc.) Custom Validation rules can be created and added to the definition file. A big feature: The built-in validation rules come with Javascript that allows you to “turn on” client side validation! Thursday, September 6, 12
  38. Quality, Speed of Development, and Maintenance tasks are simplified by

    having a properly developed Unit Test suite. Testing Tools: JUnit (http://www.junit.org) StrutsTestCase (http:// strutstestcase.sourceforge.net) HttpUnit (http://httpunit.sourceforge.net) JWebUnit (http://jwebunit.sourceforge.net) Unit Testing Thursday, September 6, 12
  39. Based on either a “MockObjects” or Cactus- based test. (Extend

    appropriate object) Can be run as part of an automated test suite! (Advice: Use MockObjects while developing) StrutsTestCase Thursday, September 6, 12
  40. How do I determine if my Unit Tests (both JUnit,

    Struts, etc.) are testing my entire application? (i.e. What Tests have I missed?) Commercial Code Coverage Tools Clover (http://www.thecortex.net/clover/) JProbe (http://java.quest.com/jprobe/ jprobe.shtml) Unit Test Coverage Thursday, September 6, 12
  41. JDK 1.4 Contains a “Logging” API Apache Log4J is a

    proven logging tool that is configuration-based. (http://jakarta.apache.org/log4j) For those that can’t decide: Jakarta Commons Logging (http://jakarta.apache.org/commons/logging) Application Logging Thursday, September 6, 12
  42. Books Programming Jakarta Struts (Cavaness) The Struts Framework (Spielman) Struts

    in Action (Husted) Other Resources Thursday, September 6, 12
  43. Online http://jakarta.apache.org/struts (The beginning, particularly the “resources” section) http://www.onjava.com (Several

    Articles dealing with Struts/Tiles) http://husted.com/struts/ (Patterns and Strategies for developing Struts applications!) Thursday, September 6, 12