SlideShare una empresa de Scribd logo
1 de 46
Descargar para leer sin conexión
Struts

An Open-source Architecture for
      Web Applications


             David Morris
           dmorris@midrangeserver.com


                             Fall COMMON 2003
                             450180
                             47GS
                             Copyright © 2003 David Morris Consulting
                                                                        1
Overview

●   Why should you use Struts
●   Installing Struts
●   Struts Concepts
    –   What is MVC
    –   Configuring Struts
    –   Tag libraries
●   Build a practical example
●   What do you do when things go wrong?
●   Advanced Topics

                                           2
Important Concepts

Struts is a Servlet that supports your applications

JSP Pages are also Servlets

Struts provides a pluggable framework

Model View Controller separates responsibilities

Tools exist to simplify Struts development
                                                      3
Developer Goals

●   Leverage existing skills
    –   The pool of Struts developers is large and growing
    –   Struts is the standard Servlet/JSP Framework!
●   Build on quality components
    –   Use widely deployed technologies
    –   Define interfaces, preconditions, post-conditions and test
        to prove it!
●   Use known design patterns
    –   Model View Controller pattern clearly seperates
        presentation, control, and business logic
    –   Avoid vendor lock in!
                                                                     4
Fitting the Pieces

●   Struts is the quot;plumbingquot; – plug in your pieces
●   Familiar concepts to iSeries programmers:
    –   Taglibs = DDS Keywords
    –   Struts Form = Display File
    –   Action Class = Main line program
    –   Business logic beans = Service programs
●   Struts runs on any platform – Standards based
    –   Database can be DB2, MySQL, etc.
    –   Servlet support can come from Tomcat, WebSphere,
        WebLogic, etc.
    –   There are lots of examples and help available
                                                           5
Model View Controller


     View               Controller




                         Model


                                     6
Requirements

●   A Java Runtime Environment
    –   Use latest stable version for best results
    –   Add tools.jar for JSP pages
●   A Servlet Container
    –   Tomcat, WebSphere, WebLogic, etc.
●   Memory and CPU
    –   More is always better!

                                                     7
Setup

Download and install the following:
  –   A JDK and Java Runtime Environment
       iSeries, Sun, JRocket, Blackdown, etc.

  –   An application server
       Tomcat 4.1.27 will work just fine

  –   Struts
       Currently Struts 1.1



                                                8
Tomcat Setup

●   Download release from http://jakarta.apache.org
    –   For iSeries get .zip, Linux .tar, Windows .exe
●   Unpack zip files or run Windows executable
●   Delete folders under webapps
●   Eclipse - Import Tomcat as simple project
●   Add web application context to conf/server.xml
    –   Use no-examples as base
    –   Remove Tomcat-Apache service
    –   Setup security realm if necessary
                                                         9
Running Tomcat

On a PC:
  With .exe version Tomcat starts as a service
  With .zip version run ../bin/startup.bat
On the iSeries:
  Submit using Qshell (QSH) command
  Modify iSeries-toolkit script
      sourceforge.net/projects/iseries-toolkit
Within Eclipse:
  Use sysdeo or WDSc plugin

                                                 10
Installing Applications

●   Struts is a Servlet Application
●   Drop a .war file into Tomcat's ../webapps directory
●   Create application under ../webapps directory
●   Add a context entry into ../conf/server.xml
    <Context path=quot;quot; docbase=quot;/webapps/spanquot; debug=quot;2quot; />




                                                            11
Struts Web Deployment

Modify application's web.xml to add Struts Action:
<web-app>
  <display-name>myApp</display-name>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>application</param-name>
      <param-value>ApplicationResources</param-value>
    </init-param>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    ...
    <load-on-startup>1</load-on-startup>
  </servlet>

   <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>
                                                                            12
Configuring Struts

Once:
  Add action servlet to web.xml
        Consider using multiple struts-config files
  Modify struts-config.xml
        Decide whether to use caching
        Add plugins

Per application:
  Add form definition
  Add action-mappings


                                                      13
Forms

●   Struts Display files
●   JSP corresponds to a form bean

Basic Flow:
    Call reset
    Properties set on form bean using reflection
    The validate method is called
    The form is presented to an Action
    The action modifies form values
    The JSP page displays modified values from the form (or
      model)
                                                              14
Taglibs

●   Heart of JSP pages
●   Encapsulate presentation logic
    –   Can encapsulate business logic       Warning
● Display file of the Servlet world
● Actually compile into a Servlet

● Easy to understand and separate form from function

● Customizable


Unfortunately
    JSP Pages take a lot of time to develop
    Tag libraries are prone to bugs and difficult to keep up to
      date
                                                                  15
ApplicationResources.properties

●   Used to store application constants
    –   Example: text.customer.name=Name
    –   Predefined values for errors like: errors.header,
        errors.footer, errors.prefix, and errors.suffix
●   Used for error messages
    –   Example: errors.required={0} is required.




                                                            16
Accessing Bean Properties

●   Struts tags use reflection to locate properties
    –   Access nested properties with dot notation
    –   The name of the bean is optional and will default to the
        form bean if not specified
         <bean:write name=quot;myBeanquot; property=quot;customer.namequot;/>

         In this example myBean is the name of an attribute set in request
           or session scope:
            SomeBean someBean = new SomeBean();
            someBean.getCustomer().setName(quot;Davidquot;);
            request.getSession().setAttribute(quot;myBeanquot;, someBean);




                                                                             17
Actions

●   Extend org.apache.struts.Action
    –   You must implement execute method that receives
        mapping, form, request, and response
●   Your execute method is called by Struts Action at
    the appropriate time (remember web.xml?)
●   Process form data, invoke business rules, decide
    where to go next, and return status
●   DispatchAction simplifies Action logic by calling
    targeted methods
                                                          18
Returning Messages

●   Struts contains built in support for messages
    –   ActionMessages contains a set of messages
    –   ActionMessage is a stores an individual message
●   ActionErrors returned from a form's validate
    method will end processing
●   There are two types of messages supported:
         ActionErrors.GLOBAL_MESSAGE
         ActionMessages.GLOBAL_MESSAGE
●   Call saveMessages(request, messages) in Action to
    prepare messages for display
                                                          19
Struts Flow

1. User select URL ending in .do ie: /myApp.do
2. Struts Action servlet receives control
3. URL is associated with an Action class
   The Action class is associated with a Form bean
4. The Form's reset method is called
5. Request parameters are mapped to the Form
6. The execute method on the Action class is called
  An ActionForward is returned ie: /myApp.jsp

                                                      20
Visualizing Struts Flow

          Browser                9
                                               my.jsp


                                                   8
                        1
                                                            3
                                        Action Servlet          MyForm
1   Browser invokes Struts Action
2   Action invokes reset on form
3   Action populates form from request parms                2
4
5
    Form passed to MyAction
    Action invokes Logic
                                               4        7
6   Logic return status
7   MyAction returns ActionForward
8   Action Servlet forwards to jsp                          6
9   The jsp builds an HTML page                MyAction          Logic
                                                            5
                                                                         21
Creating a Struts Application

1. Create entity beans
2. Create logic beans to manipulate entity beans
3. Create form bean with necessary properties
  – You skip this step with DynaActionForm
4. Create Action class
  – Extend or Action DispatchAction
5. Update struts-config.xml
  – Define global forward, form, and action mappings
6. Create JSP page to support application
                                                       22
Step 1: Create Entity Beans

●   Ideally entity beans are platform independent
●   It is usually best if domain model closely resembles
    underlying database
●   Hibernate and ibattis provide good support in this
    area
    public class Customer
        implements Serializable {
        /** persistent field */
        private long id;
        private String name;
        private Set orders;
    ...
        public Set getOrders() {
            return this.orders;
        }
        public void setOrders(Set orders) {
            this.orders = orders;
        }
                                                           23
Step 2: Create Logic Beans

●   These will sit between the controller and model
●   Service layer that provides APIs over domain
    model
●   Platform independent
     –  Don't pass in ServletRequest, etc. use helper classes if
        necessary
    public class CustomerService {
        public Customer addCustomer(BaseDao dao,Customer customer)
             throws DatabaseException {
             try {
                 dao.add(customer);
             } catch (DatabaseException de) {
                 //log error
                 throw de;
             }

             return customer;
         }

                                                                     24
Step 3: Create Form Beans

●   Form beans are a place holder for html form fields
●   Loaded by Action servlet prior to calling associated
    action's execute method
    public class CustomerForm extends ValidatorForm {
        private Customer customer;

        public void reset(ActionMapping mapping,
                           HttpServletRequest request) {
            if (customer == null) {
                customer = new Customer();
            }
        }

        public Customer getCustomer() {
            return customer;
        }

         public void setCustomer(Customer customer) {
             this.customer = customer;
         }
    }   }
                                                           25
Step 4: Create Action Class

●    Controller that directs work
●    Called by Action servlet (remember web.xml?)
    public final class CustomerDispatchAction extends BaseDispatchAction {
      public ActionForward add(
          ActionMapping mapping, ActionForm form, HttpServletRequest request,
          HttpServletResponse response)
          throws Exception {
          // Validate form for errors
          boolean valid = isFormValid(mapping, form, request);                               Controlled validation
          if (valid) {
              CustomerForm customerForm = (CustomerForm) form;
              CustomerService service = new CustomerService();
              service.addCustomer(getDao(request), customerForm.getCustomer());

              ActionMessages messages = new ActionMessages();                                Return messages
              ActionMessage message = new ActionMessage(
                    quot;message.customer.add.successquot;, customerForm.getCustomer().getName());
              messages.add(ActionMessages.GLOBAL_MESSAGE, message);
              saveMessages(request, messages);
              customerForm.setAction(quot;updatequot;);
              request.setAttribute(mapping.getAttribute(), customerForm);
          }

          return mapping.findForward(quot;refreshquot;);                                             What's next
      }
                                                                                                                     26
Step 5: Update struts-config.xml

Add form bean, global forward, and action mapping
<form-beans>
  <form-bean name=quot;customerFormquot; type=quot;org.iseriestoolkit.span.CustomerFormquot; />
</form-beans>

<!-- Global forwards -->
<global-forwards>
  <forward name=quot;customerquot; path=quot;/customerAction.doquot; redirect=quot;truequot; />
</global-forwards>

<!-- Action Mapping Definitions -->
<action-mappings>
  <action path=quot;/customerActionquot;
          type=quot;org.iseriestoolkit.span.CustomerDispatchActionquot;
          name=quot;customerFormquot; scope=quot;requestquot;
          validate=quot;falsequot;
          parameter=quot;actionquot;
          input=quot;/WEB-INF/jsp/customerForm.jspquot;>
    <exception key=quot;exception.database.errorquot;
               type=quot;org.iseriestoolkit.span.DatabaseExceptionquot;
               path=quot;/error.jspquot; />
    <forward name=quot;refreshquot; path=quot;/WEB-INF/jsp/customerForm.jspquot; />
  </action>
</action-mappings>
                                                                                  27
Step 6: Create JSP Page

●   Standard JSP works - favor Struts and JSTL tags
●   Buttons are important and a challenge
     –   One per page, javascript, or LookupDispatchAction
    <%@ taglib uri=quot;http://java.sun.com/jstl/corequot; prefix=quot;cquot; %>
    <html-el:html locale=quot;truequot; xhtml=quot;truequot;>

    <body>
    <html-el:errors/>

    <html-el:form action=quot;customerActionquot; focus=quot;customer.namequot;>
      <html-el:hidden name=quot;customerFormquot; property=quot;actionquot; />
      <html-el:hidden property=quot;customer.idquot;/>

     <bean-el:message key=quot;text.customer.namequot;/>
     <html-el:text property=quot;customer.namequot; size=quot;30quot; maxlength=quot;50quot;/>

      <html-el:submit styleClass=quot;buttonquot;>
        <bean-el:message key=quot;button.addquot; />
      </html-el:submit>
    </html-el:form>
    </body>
                                                                         28
Tips

●   Always start at .do – never show your .jsp
●   Use some javascript to submit forms onClick() or
    onChange()
    –   See CustomerListForm.jsp
●   Don't bother with /do/myAction notation
●   When back buttons and refresh won't work
    –   Place forms in session scope with nocache=quot;truequot;
    –   Always redirect to a get before presenting page
         (use xxx.do?action=quot;refreshquot;)
    –   This is an all or nothing solution that could impact
        performance due to memory utilization
                                                               29
Plug-in Custom Features

●   Startup initialization
●   Use for any class that takes some time to start
     –   Build a menu Map from XML documents, etc.
●   Any class that implements the
    org.apache.struts.action.PlugIn interface
     –   Must implement an init and destroy method

Configure in struts-config:
<plug-in className=quot;org.apache.struts.validator.ValidatorPlugInquot;>
  <set-property property=quot;pathnamesquot;
                value=quot;/WEB-INF/validator-rules.xml, /WEB-INF/validation.xmlquot; />
</plug-in>


                                                                                   30
Anatomy of an Application - CRUD

Span is a full function application supporting add
 update, and delete operations over an iSeries or
 MySQL database

Goals:
  Minimize coding time
  Support common use cases
  Provide the ability to target any database
  Use reasonable practices
  Demonstrate the capabilities of Struts
                                                     31
Span Overview

Obtain with CVS:
    cvs -z3 -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/iseries-toolkit co span
    There is no password

●   Two operations are supported – List customers and
    maintain customers
●   Hibernate provides database support
●   CustomerListAction passes add and update
    requests to CustomerAction
    –   Return point ActionForward is set in forwarding method


                                                                                        32
Advanced Topics

●   Building, Testing, and Debugging Struts
    applications
    –   Using Ant
    –   Testing with jUnit
    –   What to look for when debugging
    –   Chaining actions
●   Struts tools
    –   Setting up Eclipse and WDSc
●   Persistence
    –   Using a persistence layer

                                              33
Using Ant

●   Ant is a Java based scripting tool
●   Scripts defined using XML (build.xml)
●   Plugins for many IDEs including Eclipse, Net
    Beans, and WDSc
●   Use Ant to create, document, distribute, pack,
    package, test, shake, and stir Java applications
    <mkdir dir=quot;${build}quot; />
    <javac debug=quot;${debug}quot;
           deprecation=quot;${deprecation}quot;
           destdir=quot;${build-target}quot;
           failOnError=quot;truequot;
           includes=quot;**/*.javaquot;
           srcdir=quot;${src}quot;>

                                                       34
Testing with jUnit

●   jUnit is a Java-based unit testing tool
●   Available at www.junit.org
●   Tests should match package of tested class
●   Extend junit.framework.TestCase
    public class BaseDAOTest extends TestCase {
    ...
    public void testAddObject() {
        Customer customer = new Customer();
        customer.setName(quot;Fred Flintstonequot;);
        customer.setEmail(quot;fred.flintsone@toonville.comquot;);

        BaseDao bDAO = new BaseDao();

        try {
            bDAO.add(customer);
        } catch (Throwable t) {
            fail(quot;Unable to add new Customer quot; + customer);
        }
    }
                                                              35
Testing Struts Applications

●   jUnit provides an easy to use tool for unit testing
●   Execute jUnit tests via Ant to regression test
    applications
●   Use jMeter for load testing
●   Cactus provides support for testing Servlets,
    customer tags, etc
    jakarta.apache.org/cactus
●   Use Struts test case to test actions without a
    running servlet engine
    sourceforge.net/projects/strutsstestcase
                                                          36
Debugging Tips

●   Use an IDE like Eclipse
●   Download the source for Struts and bean-utils
●   With Eclipse:
    –   Add the source to struts.jar and bean-utils.jar
         Note: I have done this for in the span project jar files
●   Set a break point in your form's reset method to
    catch errors setting bean properties
●   Make sure that ActionForward is set on exiting
    Actions

                                                                    37
Common Errors

●   Check boxes are not set
    –   Use reset method in form to set check box properties to
        false
●   Session timeouts cause null pointer exceptions
    –   Use lazy lists in forms with factory
●   Properties not set on form
    –   Verify type is correct
    –   Make sure form values start with lower case and each
        separator (.) has a getter
●   Button does not call proper method
    –   Use javascript if necessary to set action for buttons
                                                                  38
More Common Errors

●   Action class is not being called on submit
    –   Verify action path in struts-config matches the form
        action name
    –   Remove validate=quot;truequot; from struts-config and call
        form.validate directly from action
●   When chaining actions form properties are lost
    –   The reset() action is called between actions so add
        properties to the request string



                                                               39
Chaining Actions

●   Used when more than one action will process a
    single request

    Challenges:
       ●   Different actions will have different forms – input may not
           match up
       ●   Setting return point is difficult

    Solutions:
       ●   Avoid chaining actions
       ●   Start with a clean slate each time – use redirect
       ●   Store return point in session
                                                                         40
Using Eclipse or WDSc

●   WDSc version 5
    –   Wizards that support common tasks
         Web project, ActionForm, Action class, and Struts JSP
    –   Editing support for struts-config
●   Run Tomcat within Eclipse
    www.sysdeo.com/eclipse/tomcatPlugin.html
●   Set up Tomcat as a simple project
●   Set up Struts applications as Java projects
●   Use CVS for source change control

                                                                 41
Persistence

The M in MVC
You have to do something!
  It can be as simple as data access beans that encapsulate
     JDBC calls
  It can be as complex as EJBs

One platform independent option is Hibernate
  Domain to object mapping
A simple but powerful mapping tool is ibattis
  Maps SQL statements to objects

                                                              42
Potential Alternatives

 ●   Use a commercial Struts package
     –   Expresso
     –   Basebeans
     –   WebSphere Development Studio
     –   Many others
 ●   Start simple
     –   Choose a framework after you are comfortable with
         Struts concepts
     –   Hire a mentor to guide you through the learning
         process
                                                             43
Recommendations

●   Download and install Struts
●   Extend or modify an example application
●   Identify areas that don't meet your needs and
    explore alternatives
●   Join the user list mailing list and ask questions
●   Contribute to Struts and other open source
    projects by offering feedback and help!

                                                        44
Resources

jakarta.apache.org/struts:
  The main Struts page containing links to resources,
   downloads, news, and bug resources
sourceforge.net/projects/struts:
  Example applications
sourceforge.net/projects/iseries-toolkit:
  iSeries applications and Struts Span example from this
    presentation
                                                           45
Support and Reference

Mailing lists:
   struts-user@jakarta.apache.org
   struts-dev@jakarta.apache.org

Books:
   See the Struts main page

Tutorials:
   www.reumann.net/do/struts/main
   Quite a few others are available

                                      46

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
 
Struts,Jsp,Servlet
Struts,Jsp,ServletStruts,Jsp,Servlet
Struts,Jsp,Servlet
 
Step by Step Guide for building a simple Struts Application
Step by Step Guide for building a simple Struts ApplicationStep by Step Guide for building a simple Struts Application
Step by Step Guide for building a simple Struts Application
 
Struts Introduction Course
Struts Introduction CourseStruts Introduction Course
Struts Introduction Course
 
Struts 2 Overview
Struts 2 OverviewStruts 2 Overview
Struts 2 Overview
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2
 
Struts introduction
Struts introductionStruts introduction
Struts introduction
 
Jsp with mvc
Jsp with mvcJsp with mvc
Jsp with mvc
 
Introduction to ejb and struts framework
Introduction to ejb and struts frameworkIntroduction to ejb and struts framework
Introduction to ejb and struts framework
 
Struts(mrsurwar) ppt
Struts(mrsurwar) pptStruts(mrsurwar) ppt
Struts(mrsurwar) ppt
 
Struts presentation
Struts presentationStruts presentation
Struts presentation
 
Jsf Framework
Jsf FrameworkJsf Framework
Jsf Framework
 
TY.BSc.IT Java QB U6
TY.BSc.IT Java QB U6TY.BSc.IT Java QB U6
TY.BSc.IT Java QB U6
 
Struts2
Struts2Struts2
Struts2
 
Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5Java Spring MVC Framework with AngularJS by Google and HTML5
Java Spring MVC Framework with AngularJS by Google and HTML5
 
Unit 07: Design Patterns and Frameworks (3/3)
Unit 07: Design Patterns and Frameworks (3/3)Unit 07: Design Patterns and Frameworks (3/3)
Unit 07: Design Patterns and Frameworks (3/3)
 
Servlets lecture1
Servlets lecture1Servlets lecture1
Servlets lecture1
 
Java server faces
Java server facesJava server faces
Java server faces
 
TY.BSc.IT Java QB U5
TY.BSc.IT Java QB U5TY.BSc.IT Java QB U5
TY.BSc.IT Java QB U5
 
Spring Framework -I
Spring Framework -ISpring Framework -I
Spring Framework -I
 

Similar a Struts An Open-source Architecture for Web Applications

Project Description Of Incident Management System Developed by PRS (CRIS) , N...
Project Description Of Incident Management System Developed by PRS (CRIS) , N...Project Description Of Incident Management System Developed by PRS (CRIS) , N...
Project Description Of Incident Management System Developed by PRS (CRIS) , N...varunsunny21
 
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Arun Gupta
 
Apache Wicket Web Framework
Apache Wicket Web FrameworkApache Wicket Web Framework
Apache Wicket Web FrameworkLuther Baker
 
Step By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppStep By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppSyed Shahul
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2divzi1913
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083Divyam Pateriya
 
Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02
Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02
Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02Rati Manandhar
 
JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0Arun Gupta
 
Multi Client Development with Spring
Multi Client Development with SpringMulti Client Development with Spring
Multi Client Development with SpringJoshua Long
 
Struts tutorial
Struts tutorialStruts tutorial
Struts tutorialOPENLANE
 
01 Struts Intro
01 Struts Intro01 Struts Intro
01 Struts Introsdileepec
 
Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts weili_at_slideshare
 
Strut2-Spring-Hibernate
Strut2-Spring-HibernateStrut2-Spring-Hibernate
Strut2-Spring-HibernateJay Shah
 
Developing Java Web Applications
Developing Java Web ApplicationsDeveloping Java Web Applications
Developing Java Web Applicationshchen1
 
Apachecon 2002 Struts
Apachecon 2002 StrutsApachecon 2002 Struts
Apachecon 2002 Strutsyesprakash
 
Spring Live Sample Chapter
Spring Live Sample ChapterSpring Live Sample Chapter
Spring Live Sample ChapterSyed Shahul
 

Similar a Struts An Open-source Architecture for Web Applications (20)

Project Description Of Incident Management System Developed by PRS (CRIS) , N...
Project Description Of Incident Management System Developed by PRS (CRIS) , N...Project Description Of Incident Management System Developed by PRS (CRIS) , N...
Project Description Of Incident Management System Developed by PRS (CRIS) , N...
 
Advanced Asp.Net Concepts And Constructs
Advanced Asp.Net Concepts And ConstructsAdvanced Asp.Net Concepts And Constructs
Advanced Asp.Net Concepts And Constructs
 
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
Servlets 3.0 - Asynchronous, Extensibility, Ease-of-use @ JavaOne Brazil 2010
 
Apache Wicket Web Framework
Apache Wicket Web FrameworkApache Wicket Web Framework
Apache Wicket Web Framework
 
Step By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppStep By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts App
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083
 
Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02
Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02
Stepbystepguideforbuidlingsimplestrutsapp 090702025438-phpapp02
 
Spring MVC 5 & Hibernate 5 Integration
Spring MVC 5 & Hibernate 5 IntegrationSpring MVC 5 & Hibernate 5 Integration
Spring MVC 5 & Hibernate 5 Integration
 
JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0JavaOne India 2011 - Servlets 3.0
JavaOne India 2011 - Servlets 3.0
 
Multi Client Development with Spring
Multi Client Development with SpringMulti Client Development with Spring
Multi Client Development with Spring
 
Struts tutorial
Struts tutorialStruts tutorial
Struts tutorial
 
01 Struts Intro
01 Struts Intro01 Struts Intro
01 Struts Intro
 
Struts Into
Struts IntoStruts Into
Struts Into
 
Os Haase
Os HaaseOs Haase
Os Haase
 
Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts
 
Strut2-Spring-Hibernate
Strut2-Spring-HibernateStrut2-Spring-Hibernate
Strut2-Spring-Hibernate
 
Developing Java Web Applications
Developing Java Web ApplicationsDeveloping Java Web Applications
Developing Java Web Applications
 
Apachecon 2002 Struts
Apachecon 2002 StrutsApachecon 2002 Struts
Apachecon 2002 Struts
 
Spring Live Sample Chapter
Spring Live Sample ChapterSpring Live Sample Chapter
Spring Live Sample Chapter
 

Más de elliando dias

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slideselliando dias
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScriptelliando dias
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structureselliando dias
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de containerelliando dias
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agilityelliando dias
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Librarieselliando dias
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!elliando dias
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Webelliando dias
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduinoelliando dias
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorceryelliando dias
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Designelliando dias
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makeselliando dias
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.elliando dias
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebookelliando dias
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Studyelliando dias
 

Más de elliando dias (20)

Clojurescript slides
Clojurescript slidesClojurescript slides
Clojurescript slides
 
Why you should be excited about ClojureScript
Why you should be excited about ClojureScriptWhy you should be excited about ClojureScript
Why you should be excited about ClojureScript
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
 
Nomenclatura e peças de container
Nomenclatura  e peças de containerNomenclatura  e peças de container
Nomenclatura e peças de container
 
Geometria Projetiva
Geometria ProjetivaGeometria Projetiva
Geometria Projetiva
 
Polyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better AgilityPolyglot and Poly-paradigm Programming for Better Agility
Polyglot and Poly-paradigm Programming for Better Agility
 
Javascript Libraries
Javascript LibrariesJavascript Libraries
Javascript Libraries
 
How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!How to Make an Eight Bit Computer and Save the World!
How to Make an Eight Bit Computer and Save the World!
 
Ragel talk
Ragel talkRagel talk
Ragel talk
 
A Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the WebA Practical Guide to Connecting Hardware to the Web
A Practical Guide to Connecting Hardware to the Web
 
Introdução ao Arduino
Introdução ao ArduinoIntrodução ao Arduino
Introdução ao Arduino
 
Minicurso arduino
Minicurso arduinoMinicurso arduino
Minicurso arduino
 
Incanter Data Sorcery
Incanter Data SorceryIncanter Data Sorcery
Incanter Data Sorcery
 
Rango
RangoRango
Rango
 
Fab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine DesignFab.in.a.box - Fab Academy: Machine Design
Fab.in.a.box - Fab Academy: Machine Design
 
The Digital Revolution: Machines that makes
The Digital Revolution: Machines that makesThe Digital Revolution: Machines that makes
The Digital Revolution: Machines that makes
 
Hadoop + Clojure
Hadoop + ClojureHadoop + Clojure
Hadoop + Clojure
 
Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.Hadoop - Simple. Scalable.
Hadoop - Simple. Scalable.
 
Hadoop and Hive Development at Facebook
Hadoop and Hive Development at FacebookHadoop and Hive Development at Facebook
Hadoop and Hive Development at Facebook
 
Multi-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case StudyMulti-core Parallelization in Clojure - a Case Study
Multi-core Parallelization in Clojure - a Case Study
 

Último

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Struts An Open-source Architecture for Web Applications

  • 1. Struts An Open-source Architecture for Web Applications David Morris dmorris@midrangeserver.com Fall COMMON 2003 450180 47GS Copyright © 2003 David Morris Consulting 1
  • 2. Overview ● Why should you use Struts ● Installing Struts ● Struts Concepts – What is MVC – Configuring Struts – Tag libraries ● Build a practical example ● What do you do when things go wrong? ● Advanced Topics 2
  • 3. Important Concepts Struts is a Servlet that supports your applications JSP Pages are also Servlets Struts provides a pluggable framework Model View Controller separates responsibilities Tools exist to simplify Struts development 3
  • 4. Developer Goals ● Leverage existing skills – The pool of Struts developers is large and growing – Struts is the standard Servlet/JSP Framework! ● Build on quality components – Use widely deployed technologies – Define interfaces, preconditions, post-conditions and test to prove it! ● Use known design patterns – Model View Controller pattern clearly seperates presentation, control, and business logic – Avoid vendor lock in! 4
  • 5. Fitting the Pieces ● Struts is the quot;plumbingquot; – plug in your pieces ● Familiar concepts to iSeries programmers: – Taglibs = DDS Keywords – Struts Form = Display File – Action Class = Main line program – Business logic beans = Service programs ● Struts runs on any platform – Standards based – Database can be DB2, MySQL, etc. – Servlet support can come from Tomcat, WebSphere, WebLogic, etc. – There are lots of examples and help available 5
  • 6. Model View Controller View Controller Model 6
  • 7. Requirements ● A Java Runtime Environment – Use latest stable version for best results – Add tools.jar for JSP pages ● A Servlet Container – Tomcat, WebSphere, WebLogic, etc. ● Memory and CPU – More is always better! 7
  • 8. Setup Download and install the following: – A JDK and Java Runtime Environment iSeries, Sun, JRocket, Blackdown, etc. – An application server Tomcat 4.1.27 will work just fine – Struts Currently Struts 1.1 8
  • 9. Tomcat Setup ● Download release from http://jakarta.apache.org – For iSeries get .zip, Linux .tar, Windows .exe ● Unpack zip files or run Windows executable ● Delete folders under webapps ● Eclipse - Import Tomcat as simple project ● Add web application context to conf/server.xml – Use no-examples as base – Remove Tomcat-Apache service – Setup security realm if necessary 9
  • 10. Running Tomcat On a PC: With .exe version Tomcat starts as a service With .zip version run ../bin/startup.bat On the iSeries: Submit using Qshell (QSH) command Modify iSeries-toolkit script sourceforge.net/projects/iseries-toolkit Within Eclipse: Use sysdeo or WDSc plugin 10
  • 11. Installing Applications ● Struts is a Servlet Application ● Drop a .war file into Tomcat's ../webapps directory ● Create application under ../webapps directory ● Add a context entry into ../conf/server.xml <Context path=quot;quot; docbase=quot;/webapps/spanquot; debug=quot;2quot; /> 11
  • 12. Struts Web Deployment Modify application's web.xml to add Struts Action: <web-app> <display-name>myApp</display-name> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>application</param-name> <param-value>ApplicationResources</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> ... <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> 12
  • 13. Configuring Struts Once: Add action servlet to web.xml Consider using multiple struts-config files Modify struts-config.xml Decide whether to use caching Add plugins Per application: Add form definition Add action-mappings 13
  • 14. Forms ● Struts Display files ● JSP corresponds to a form bean Basic Flow: Call reset Properties set on form bean using reflection The validate method is called The form is presented to an Action The action modifies form values The JSP page displays modified values from the form (or model) 14
  • 15. Taglibs ● Heart of JSP pages ● Encapsulate presentation logic – Can encapsulate business logic Warning ● Display file of the Servlet world ● Actually compile into a Servlet ● Easy to understand and separate form from function ● Customizable Unfortunately JSP Pages take a lot of time to develop Tag libraries are prone to bugs and difficult to keep up to date 15
  • 16. ApplicationResources.properties ● Used to store application constants – Example: text.customer.name=Name – Predefined values for errors like: errors.header, errors.footer, errors.prefix, and errors.suffix ● Used for error messages – Example: errors.required={0} is required. 16
  • 17. Accessing Bean Properties ● Struts tags use reflection to locate properties – Access nested properties with dot notation – The name of the bean is optional and will default to the form bean if not specified <bean:write name=quot;myBeanquot; property=quot;customer.namequot;/> In this example myBean is the name of an attribute set in request or session scope: SomeBean someBean = new SomeBean(); someBean.getCustomer().setName(quot;Davidquot;); request.getSession().setAttribute(quot;myBeanquot;, someBean); 17
  • 18. Actions ● Extend org.apache.struts.Action – You must implement execute method that receives mapping, form, request, and response ● Your execute method is called by Struts Action at the appropriate time (remember web.xml?) ● Process form data, invoke business rules, decide where to go next, and return status ● DispatchAction simplifies Action logic by calling targeted methods 18
  • 19. Returning Messages ● Struts contains built in support for messages – ActionMessages contains a set of messages – ActionMessage is a stores an individual message ● ActionErrors returned from a form's validate method will end processing ● There are two types of messages supported: ActionErrors.GLOBAL_MESSAGE ActionMessages.GLOBAL_MESSAGE ● Call saveMessages(request, messages) in Action to prepare messages for display 19
  • 20. Struts Flow 1. User select URL ending in .do ie: /myApp.do 2. Struts Action servlet receives control 3. URL is associated with an Action class The Action class is associated with a Form bean 4. The Form's reset method is called 5. Request parameters are mapped to the Form 6. The execute method on the Action class is called An ActionForward is returned ie: /myApp.jsp 20
  • 21. Visualizing Struts Flow Browser 9 my.jsp 8 1 3 Action Servlet MyForm 1 Browser invokes Struts Action 2 Action invokes reset on form 3 Action populates form from request parms 2 4 5 Form passed to MyAction Action invokes Logic 4 7 6 Logic return status 7 MyAction returns ActionForward 8 Action Servlet forwards to jsp 6 9 The jsp builds an HTML page MyAction Logic 5 21
  • 22. Creating a Struts Application 1. Create entity beans 2. Create logic beans to manipulate entity beans 3. Create form bean with necessary properties – You skip this step with DynaActionForm 4. Create Action class – Extend or Action DispatchAction 5. Update struts-config.xml – Define global forward, form, and action mappings 6. Create JSP page to support application 22
  • 23. Step 1: Create Entity Beans ● Ideally entity beans are platform independent ● It is usually best if domain model closely resembles underlying database ● Hibernate and ibattis provide good support in this area public class Customer implements Serializable { /** persistent field */ private long id; private String name; private Set orders; ... public Set getOrders() { return this.orders; } public void setOrders(Set orders) { this.orders = orders; } 23
  • 24. Step 2: Create Logic Beans ● These will sit between the controller and model ● Service layer that provides APIs over domain model ● Platform independent – Don't pass in ServletRequest, etc. use helper classes if necessary public class CustomerService { public Customer addCustomer(BaseDao dao,Customer customer) throws DatabaseException { try { dao.add(customer); } catch (DatabaseException de) { //log error throw de; } return customer; } 24
  • 25. Step 3: Create Form Beans ● Form beans are a place holder for html form fields ● Loaded by Action servlet prior to calling associated action's execute method public class CustomerForm extends ValidatorForm { private Customer customer; public void reset(ActionMapping mapping, HttpServletRequest request) { if (customer == null) { customer = new Customer(); } } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } } } 25
  • 26. Step 4: Create Action Class ● Controller that directs work ● Called by Action servlet (remember web.xml?) public final class CustomerDispatchAction extends BaseDispatchAction { public ActionForward add( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Validate form for errors boolean valid = isFormValid(mapping, form, request); Controlled validation if (valid) { CustomerForm customerForm = (CustomerForm) form; CustomerService service = new CustomerService(); service.addCustomer(getDao(request), customerForm.getCustomer()); ActionMessages messages = new ActionMessages(); Return messages ActionMessage message = new ActionMessage( quot;message.customer.add.successquot;, customerForm.getCustomer().getName()); messages.add(ActionMessages.GLOBAL_MESSAGE, message); saveMessages(request, messages); customerForm.setAction(quot;updatequot;); request.setAttribute(mapping.getAttribute(), customerForm); } return mapping.findForward(quot;refreshquot;); What's next } 26
  • 27. Step 5: Update struts-config.xml Add form bean, global forward, and action mapping <form-beans> <form-bean name=quot;customerFormquot; type=quot;org.iseriestoolkit.span.CustomerFormquot; /> </form-beans> <!-- Global forwards --> <global-forwards> <forward name=quot;customerquot; path=quot;/customerAction.doquot; redirect=quot;truequot; /> </global-forwards> <!-- Action Mapping Definitions --> <action-mappings> <action path=quot;/customerActionquot; type=quot;org.iseriestoolkit.span.CustomerDispatchActionquot; name=quot;customerFormquot; scope=quot;requestquot; validate=quot;falsequot; parameter=quot;actionquot; input=quot;/WEB-INF/jsp/customerForm.jspquot;> <exception key=quot;exception.database.errorquot; type=quot;org.iseriestoolkit.span.DatabaseExceptionquot; path=quot;/error.jspquot; /> <forward name=quot;refreshquot; path=quot;/WEB-INF/jsp/customerForm.jspquot; /> </action> </action-mappings> 27
  • 28. Step 6: Create JSP Page ● Standard JSP works - favor Struts and JSTL tags ● Buttons are important and a challenge – One per page, javascript, or LookupDispatchAction <%@ taglib uri=quot;http://java.sun.com/jstl/corequot; prefix=quot;cquot; %> <html-el:html locale=quot;truequot; xhtml=quot;truequot;> <body> <html-el:errors/> <html-el:form action=quot;customerActionquot; focus=quot;customer.namequot;> <html-el:hidden name=quot;customerFormquot; property=quot;actionquot; /> <html-el:hidden property=quot;customer.idquot;/> <bean-el:message key=quot;text.customer.namequot;/> <html-el:text property=quot;customer.namequot; size=quot;30quot; maxlength=quot;50quot;/> <html-el:submit styleClass=quot;buttonquot;> <bean-el:message key=quot;button.addquot; /> </html-el:submit> </html-el:form> </body> 28
  • 29. Tips ● Always start at .do – never show your .jsp ● Use some javascript to submit forms onClick() or onChange() – See CustomerListForm.jsp ● Don't bother with /do/myAction notation ● When back buttons and refresh won't work – Place forms in session scope with nocache=quot;truequot; – Always redirect to a get before presenting page (use xxx.do?action=quot;refreshquot;) – This is an all or nothing solution that could impact performance due to memory utilization 29
  • 30. Plug-in Custom Features ● Startup initialization ● Use for any class that takes some time to start – Build a menu Map from XML documents, etc. ● Any class that implements the org.apache.struts.action.PlugIn interface – Must implement an init and destroy method Configure in struts-config: <plug-in className=quot;org.apache.struts.validator.ValidatorPlugInquot;> <set-property property=quot;pathnamesquot; value=quot;/WEB-INF/validator-rules.xml, /WEB-INF/validation.xmlquot; /> </plug-in> 30
  • 31. Anatomy of an Application - CRUD Span is a full function application supporting add update, and delete operations over an iSeries or MySQL database Goals: Minimize coding time Support common use cases Provide the ability to target any database Use reasonable practices Demonstrate the capabilities of Struts 31
  • 32. Span Overview Obtain with CVS: cvs -z3 -d:pserver:anonymous@cvs.sourceforge.net:/cvsroot/iseries-toolkit co span There is no password ● Two operations are supported – List customers and maintain customers ● Hibernate provides database support ● CustomerListAction passes add and update requests to CustomerAction – Return point ActionForward is set in forwarding method 32
  • 33. Advanced Topics ● Building, Testing, and Debugging Struts applications – Using Ant – Testing with jUnit – What to look for when debugging – Chaining actions ● Struts tools – Setting up Eclipse and WDSc ● Persistence – Using a persistence layer 33
  • 34. Using Ant ● Ant is a Java based scripting tool ● Scripts defined using XML (build.xml) ● Plugins for many IDEs including Eclipse, Net Beans, and WDSc ● Use Ant to create, document, distribute, pack, package, test, shake, and stir Java applications <mkdir dir=quot;${build}quot; /> <javac debug=quot;${debug}quot; deprecation=quot;${deprecation}quot; destdir=quot;${build-target}quot; failOnError=quot;truequot; includes=quot;**/*.javaquot; srcdir=quot;${src}quot;> 34
  • 35. Testing with jUnit ● jUnit is a Java-based unit testing tool ● Available at www.junit.org ● Tests should match package of tested class ● Extend junit.framework.TestCase public class BaseDAOTest extends TestCase { ... public void testAddObject() { Customer customer = new Customer(); customer.setName(quot;Fred Flintstonequot;); customer.setEmail(quot;fred.flintsone@toonville.comquot;); BaseDao bDAO = new BaseDao(); try { bDAO.add(customer); } catch (Throwable t) { fail(quot;Unable to add new Customer quot; + customer); } } 35
  • 36. Testing Struts Applications ● jUnit provides an easy to use tool for unit testing ● Execute jUnit tests via Ant to regression test applications ● Use jMeter for load testing ● Cactus provides support for testing Servlets, customer tags, etc jakarta.apache.org/cactus ● Use Struts test case to test actions without a running servlet engine sourceforge.net/projects/strutsstestcase 36
  • 37. Debugging Tips ● Use an IDE like Eclipse ● Download the source for Struts and bean-utils ● With Eclipse: – Add the source to struts.jar and bean-utils.jar Note: I have done this for in the span project jar files ● Set a break point in your form's reset method to catch errors setting bean properties ● Make sure that ActionForward is set on exiting Actions 37
  • 38. Common Errors ● Check boxes are not set – Use reset method in form to set check box properties to false ● Session timeouts cause null pointer exceptions – Use lazy lists in forms with factory ● Properties not set on form – Verify type is correct – Make sure form values start with lower case and each separator (.) has a getter ● Button does not call proper method – Use javascript if necessary to set action for buttons 38
  • 39. More Common Errors ● Action class is not being called on submit – Verify action path in struts-config matches the form action name – Remove validate=quot;truequot; from struts-config and call form.validate directly from action ● When chaining actions form properties are lost – The reset() action is called between actions so add properties to the request string 39
  • 40. Chaining Actions ● Used when more than one action will process a single request Challenges: ● Different actions will have different forms – input may not match up ● Setting return point is difficult Solutions: ● Avoid chaining actions ● Start with a clean slate each time – use redirect ● Store return point in session 40
  • 41. Using Eclipse or WDSc ● WDSc version 5 – Wizards that support common tasks Web project, ActionForm, Action class, and Struts JSP – Editing support for struts-config ● Run Tomcat within Eclipse www.sysdeo.com/eclipse/tomcatPlugin.html ● Set up Tomcat as a simple project ● Set up Struts applications as Java projects ● Use CVS for source change control 41
  • 42. Persistence The M in MVC You have to do something! It can be as simple as data access beans that encapsulate JDBC calls It can be as complex as EJBs One platform independent option is Hibernate Domain to object mapping A simple but powerful mapping tool is ibattis Maps SQL statements to objects 42
  • 43. Potential Alternatives ● Use a commercial Struts package – Expresso – Basebeans – WebSphere Development Studio – Many others ● Start simple – Choose a framework after you are comfortable with Struts concepts – Hire a mentor to guide you through the learning process 43
  • 44. Recommendations ● Download and install Struts ● Extend or modify an example application ● Identify areas that don't meet your needs and explore alternatives ● Join the user list mailing list and ask questions ● Contribute to Struts and other open source projects by offering feedback and help! 44
  • 45. Resources jakarta.apache.org/struts: The main Struts page containing links to resources, downloads, news, and bug resources sourceforge.net/projects/struts: Example applications sourceforge.net/projects/iseries-toolkit: iSeries applications and Struts Span example from this presentation 45
  • 46. Support and Reference Mailing lists: struts-user@jakarta.apache.org struts-dev@jakarta.apache.org Books: See the Struts main page Tutorials: www.reumann.net/do/struts/main Quite a few others are available 46