Apache Struts Framework

download Apache Struts Framework

of 43

Transcript of Apache Struts Framework

  • 8/2/2019 Apache Struts Framework

    1/43

    Copyright 2006 Celeritas Technologies, LLC

    StrutsWhat they are, and How we use em

    PEG Lunch-n-LearnNov 9, 2005

  • 8/2/2019 Apache Struts Framework

    2/43

    Copyright 2006 Celeritas Technologies, LLC

    Struts are support mechanisms

  • 8/2/2019 Apache Struts Framework

    3/43

    Copyright 2006 Celeritas Technologies, LLC

    What is Struts?

    MVC-based (Model-View-Controller)open source software

    Hosted by Apache Software Foundation

    Used to support the building of businessapplications

  • 8/2/2019 Apache Struts Framework

    4/43

    Copyright 2006 Celeritas Technologies, LLC

    The Big Picture

    Struts-config.xml - provides configurationinformation to the Action Servlet for all Struts objectsthat will be used by the application.

    Action Servlet - controls navigational flow. Action Class - accesses the business classes.

    Action Form - validates form inputs; provides accessfor the action class to the request parameters; usedby action class to store and retrieve data.

    ActionForward - forwards the request to thespecified path.

  • 8/2/2019 Apache Struts Framework

    5/43

    Copyright 2006 Celeritas Technologies, LLC

    The Big Picture

    Struts-config.xml

    Action

    Servlet

    Action

    Form

    JSP

    JSP

    submit

    respond

    start up

  • 8/2/2019 Apache Struts Framework

    6/43

    Copyright 2006 Celeritas Technologies, LLC

    Application Set-up

    Jar files Tag libraries

    Struts-config dtdApplication.properties

    Apache vhost file

    Web.xml

    Struts-config.xml

  • 8/2/2019 Apache Struts Framework

    7/43

    Copyright 2006 Celeritas Technologies, LLC

    Application Set-up (cont.)

    Jar files Place in \WEB-INF\lib

    Tag Libraries

    Place in \WEB-INF\taglibs

    Struts-config dtd

    Place in \WEB-INF

  • 8/2/2019 Apache Struts Framework

    8/43

    Copyright 2006 Celeritas Technologies, LLC

    Application Set-up (cont.)

    Application.properties Default location: \WEB-INF\classes\resources

    Somewhat configurable

    Name can be name.properties

    Location can be \WEB-INF\classes\folder name

    Currently using bundles folder name

    Contains messages for display within theapplication

  • 8/2/2019 Apache Struts Framework

    9/43

    Copyright 2006 Celeritas Technologies, LLC

    Application Set-up (cont.)

    Apache vhost file:RewriteRule ^/TicketManagement/?$ /TicketManagement/soComLogin.jsp [R]

    DirectoryIndex soComLogin.jspOrder deny,allow

    Allow from all

    ### These JkMount directives will determine what requests will be sent### to Tomcat.JkMount / TicketManagement/ * .do aj p13

    JkMount /TicketManagement/*.jsp ajp13

  • 8/2/2019 Apache Struts Framework

    10/43

    Copyright 2006 Celeritas Technologies, LLC

    Application Set-up (cont.)

    Web.xml fileAction servlet configuration

    Servlet request mapping

    Struts tag libraries configuration

  • 8/2/2019 Apache Struts Framework

    11/43

    Copyright 2006 Celeritas Technologies, LLC

    Web.xml Set-up

    Servlet configuration:

    actionorg.apache.struts.action.ActionServlet

    config/WEB-INF/conf/struts-config.xml

    debug2

    detail2

    2

  • 8/2/2019 Apache Struts Framework

    12/43

    Copyright 2006 Celeritas Technologies, LLC

    Web.xml Set-up (cont.)

    Servlet request mapping:

    action*.do

  • 8/2/2019 Apache Struts Framework

    13/43

    Copyright 2006 Celeritas Technologies, LLC

    Web.xml Set-up (cont.)

    Struts tag libraries configuration:

    /taglibs/struts-beantaglibs/struts-bean.tld

    /taglibs/struts-htmltaglibs/struts-html.tld

    /taglibs/struts-logictaglibs/struts-logic.tld

    /taglibs/struts-nestedtaglibs/struts-nested.tld

  • 8/2/2019 Apache Struts Framework

    14/43

    Copyright 2006 Celeritas Technologies, LLC

    Application Set-up (cont.)

    Struts-config.xml Location: \WEB-INF\conf

    Basic configuration tags

  • 8/2/2019 Apache Struts Framework

    15/43

    Copyright 2006 Celeritas Technologies, LLC

    Struts-config.xml

    Global-exceptionsAllows exceptions to be handled in a

    consistent way by all Action objects.

    Define exception handler to handleexceptions thrown by an Action object.

    Format:

  • 8/2/2019 Apache Struts Framework

    16/43

    Copyright 2006 Celeritas Technologies, LLC

    Struts-config.xml (cont.)

    Form-beans Defines what ActionForm objects can be

    created by the ActionServlet, and what to

    call them. Format:

  • 8/2/2019 Apache Struts Framework

    17/43

    Copyright 2006 Celeritas Technologies, LLC

    Struts-config.xml (cont.)

    Global-forwards Defines ActionForward paths that are

    available to all Actions in an application.

    Format:

  • 8/2/2019 Apache Struts Framework

    18/43

    Copyright 2006 Celeritas Technologies, LLC

    Struts-config.xml (cont.)

    Action-mappings Define what operations the application can

    undertake.

  • 8/2/2019 Apache Struts Framework

    19/43

    Copyright 2006 Celeritas Technologies, LLC

    Struts-config.xml (cont.)

    Message-resources Deploys the message bundle files needed

    by the application (xyz.properties).

    Default file: \WEB-INF\classes\resources\application.properties

    Format:

  • 8/2/2019 Apache Struts Framework

    20/43

    Copyright 2006 Celeritas Technologies, LLC

    Struts-config.xml (cont.)

    Plug-in Defines special resources that are available

    to the Action classes.

    Format:

  • 8/2/2019 Apache Struts Framework

    21/43

  • 8/2/2019 Apache Struts Framework

    22/43

    Copyright 2006 Celeritas Technologies, LLC

    Simple Example Request

    Request is sent to the ActionServlet/Request.do?param1=value&param2=

    Servlet checks the ActionMapping forRequest.do

    Servlet instantiates the ActionForm

    Form is populated with request parametersvia setter methods, i.e. setParam1(String pValue)

    Form performs any validation on the

    supplied values

  • 8/2/2019 Apache Struts Framework

    23/43

    Copyright 2006 Celeritas Technologies, LLC

    Simple Example Request (cont.)

    Servlet executes the specified Actionclass, passing it the ActionForm

    Action class does its duty, gettingand/or setting data in the ActionForm

    Action forwards to the ActionServlet

    Servlet executes a JSP JSP displays data from the ActionForm

  • 8/2/2019 Apache Struts Framework

    24/43

    Copyright 2006 Celeritas Technologies, LLC

    A Brief Look at

    Struts HTML Tags

    A wide range of tags are available Complete list and description found at:

    http://struts.apache.org/struts-taglib/index.html orhttp://struts.apache.org/struts-doc-1.2.7/userGuide/index.html

    Use can replace Java code in a JSP

    Can be somewhat clunky to use

    Not always applicable to coding needs

  • 8/2/2019 Apache Struts Framework

    25/43

    Copyright 2006 Celeritas Technologies, LLC

    A Brief Look at

    Struts HTML Tags (cont.)

    Example of form and message tags

    . . .

  • 8/2/2019 Apache Struts Framework

    26/43

    Copyright 2006 Celeritas Technologies, LLC

    A Brief Look at

    Struts HTML Tags (cont.)

    Example of submit and button tags

  • 8/2/2019 Apache Struts Framework

    27/43

    Copyright 2006 Celeritas Technologies, LLC

    A Brief Look at

    Struts HTML Tags (cont.)

    Example of logic and select-box tags

    Ticket History:

  • 8/2/2019 Apache Struts Framework

    28/43

    Copyright 2006 Celeritas Technologies, LLC

    Thread Safety

    Servlet creates one instance of eachAction class per application.

    Only use local variables, not instance

    (class) variables.

    Shared values must be passed through

    the method signatures.

  • 8/2/2019 Apache Struts Framework

    29/43

    Copyright 2006 Celeritas Technologies, LLC

    Modular Applications

    Multiple modules can run within thesame application space

    Each module has its own prefix

    Prefix corresponds to the folder namewhere the module files reside

    Module configuration is in web.xml

  • 8/2/2019 Apache Struts Framework

    30/43

    Copyright 2006 Celeritas Technologies, LLC

    Modular Applications (cont.)

    Web.xml setup

    config/WEB-INF/conf/struts-config.xml

    config/adminModule/WEB-INF/conf/struts-adminModule.xml

    config/loginModule/WEB-INF/conf/struts-loginModule.xml

  • 8/2/2019 Apache Struts Framework

    31/43

    Copyright 2006 Celeritas Technologies, LLC

    Modular Applications (cont.)

    Struts-loginModule.xml setup

  • 8/2/2019 Apache Struts Framework

    32/43

    Copyright 2006 Celeritas Technologies, LLC

    Modular Applications (cont.)

    Modules are referenced bymoduleName/action

    Example of vhost file:

    ### do a redirect to the entry point for this applicationRewriteEngine OnRewriteRule /TicketManagement/?$ /TicketManagement/loginModule/Login.do [R]### RewriteRule ^/TicketManagement/?$ /TicketManagement/soComLogin.jsp [R]

  • 8/2/2019 Apache Struts Framework

    33/43

    Copyright 2006 Celeritas Technologies, LLC

    Modular Applications (cont.)

    Pros Modules can be developed/modified

    independently

    Smaller, cleaner configuration files

    Cons

    Global items (forwards, exception handlers,plug-ins) must be defined in each modulethat needs access to them

  • 8/2/2019 Apache Struts Framework

    34/43

    Copyright 2006 Celeritas Technologies, LLC

    Modular Applications (cont.)

    Possible work-around #1config

    /WEB-INF/conf/struts-config-common.xml,/WEB-INF/conf/struts-config.xml

    config/module2

    /WEB-INF/conf/struts-config-common.xml,/WEB-INF/conf/struts-config-module2.xml

  • 8/2/2019 Apache Struts Framework

    35/43

    Copyright 2006 Celeritas Technologies, LLC

    Modular Applications (cont.)

    Possible work-around #2config

    /WEB-INF/conf/struts-config.xml,/WEB-INF/conf/struts-config-module1.xml,

    /WEB-INF/conf/struts-config-module2.xml

  • 8/2/2019 Apache Struts Framework

    36/43

    Copyright 2006 Celeritas Technologies, LLC

    Struts Tiles

  • 8/2/2019 Apache Struts Framework

    37/43

    Copyright 2006 Celeritas Technologies, LLC

    What is Struts Tiles

    Tiles is a templating libraryAllows construction of pages in

    interchangeable parts (tiles)

    Facilitates the creation of reusableview components

  • 8/2/2019 Apache Struts Framework

    38/43

    Copyright 2006 Celeritas Technologies, LLC

    Page Construction

    A page can bebroken into sectionswhich are

    implemented as tiles Each tile may have

    its own Action and

    Form class or maytake part in others

  • 8/2/2019 Apache Struts Framework

    39/43

    Copyright 2006 Celeritas Technologies, LLC

    A WebFast Example

    Webfast UserRegistration, UserAdministration and

    SecurityAdministration alluse tiles (in variouscombinations)

  • 8/2/2019 Apache Struts Framework

    40/43

    Copyright 2006 Celeritas Technologies, LLC

    Adding Tiles to thestruts-config.xml

  • 8/2/2019 Apache Struts Framework

    41/43

    Copyright 2006 Celeritas Technologies, LLC

    The Tile Configuration

  • 8/2/2019 Apache Struts Framework

    42/43

    Copyright 2006 Celeritas Technologies, LLC

    Grouting the Tiles

  • 8/2/2019 Apache Struts Framework

    43/43

    Copyright 2006 Celeritas Technologies, LLC

    Celeritas Technologies

    www.celeritas.com

    http://java.celeritas.com7101 College Blvd, Sixth FloorOverland Park, KS 66210

    913.491.9000