Configuring Struts Components - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Configuring Struts Components

Description:

It is used to load and configure various components used by ... Typically, the bundles are either in a jar file or under WEB-INF/classes folder. Plug-in element ... – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 10
Provided by: hvdos
Category:

less

Transcript and Presenter's Notes

Title: Configuring Struts Components


1
Configuring Struts Components
  • web.xml
  • The web application deployment descriptor
    required by Java Servlet specification.
  • struts-config.xml
  • frameworks deployment descriptor. It is used to
    load and configure various components used by
    struts framework.
  • application.properties
  • Provides message resources to struts based web
    application

2
The web application deployment descriptor
  • Struts framework includes two components that
    need to be configured through web.xml file the
    ActionServlet and optionally, tag libraries.
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • lt!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
    Inc.//DTD Web Application 2.3//EN"
    "http//java.sun.com/dtd/web-app_2_3.dtd"gt
  • ltweb-app id"WebApp"gt
  • ltdisplay-namegtPRO617Weblt/display-namegt
  • ltservletgt
  • ltservlet-namegtactionlt/servlet-namegtlt!- only one
    action servlet per application --gt
  • ltservlet-classgtorg.apache.struts.action.ActionServ
    letlt/servlet-classgt
  • ltinit-paramgt
  • ltparam-namegtconfiglt/param-namegtlt! context
    relative path to xml resource containing config
    info --gt
  • ltparam-valuegtWEB-INF/struts-config.xmllt/param-valu
    egt
  • lt/init-paramgt
  • ltinit-paramgt
  • ltparam-namegtdebuglt/param-namegtlt!- debug level
    for this servlet 0 least serious, 6 most
    serious --gt
  • ltparam-valuegt2lt/param-valuegt
  • lt/init-paramgt
  • ltinit-paramgt
  • ltparam-namegtdetaillt/param-namegtlt!- debug level
    for Digester to process application config files
    0 to 6 -- gt
  • ltparam-valuegt2lt/param-valuegt

3
struts-config.xml file
  • Struts config file is a living blueprint of
    application
  • Struts configuration and Action Servlet work
    together to form a control layer
  • Every component in Struts configuration is a Java
    object
  • Struts framework use reflection and introspection
    to automate loading and configuring java objects
  • Struts config file helps your application react
    to changes quickly with minimal effort.
  • In practice, it is separating things that rarely
    change the underlying java classes from things
    that often change the java objects deployed at
    runtime. Principle of protected variationLarman
  • Example file
  • ltstruts-configgt
  • lt!-- Data Sources --gt
  • ltdata-sourcesgt
  • lt/data-sourcesgt
  • lt!-- Form Beans --gt
  • ltform-beansgt
  • lt/form-beansgt
  • lt!-- Global Exceptions --gt
  • ltglobal-exceptionsgt
  • lt/global-exceptionsgt
  • lt!-- Global Forwards --gt
  • ltglobal-forwardsgt

4
Struts configuration elements
  • Data-sources Element
  • Contains zero or more data-source element
  • lt!ELEMENT data-sources (data-source)gt
  • Data-source Element
  • lt!ELEMENT data-source (set-property)gt
  • Acts as a factory for database connections and
    provides a single point of control
  • set-property allows to configure properties that
    are specific to data source implementation
  • Example
  • ltdata-sourcesgt
  • ltdata-sourcegt
  • ltset-property property"driverClass" value""/gt
  • ltset-property property"user" value""/gt
  • ltset-property property"password" value""/gt
  • ltset-property property"url" value""/gt
  • lt/data-sourcegt
  • lt/data-sourcesgt
  • Form-Beans Element
  • Configures multiple ActionForm classes that are
    used by views
  • Within form-beans you can configure zero or more
    form bean child elements

5
Struts Configuration Elements
  • Global forwards element
  • Logical name for forwarding or redirecting to a
    view.
  • The forward element maps a logical name to an
    application relative URI
  • Hard coding of literal URI is avoided using
    forward element
  • Example
  • ltglobal-forwardsgt
  • ltforward nameloginForward
    path/jsp/login.jsp redirecttruegt
  • lt/global-forwardsgt
  • Message resources element
  • Specifies characteristics of the message resource
    bundles that contain the localized messages for
    an application.
  • Each struts-config file can define one or more
    message resource bundles
  • E.g. ltmessage-resources parameter"pro617web.resou
    rces.ApplicationResources"/gt
  • Typically, the bundles are either in a jar file
    or under WEB-INF/classes folder
  • Plug-in element
  • Specifies a fully qualified class name of a
    general purpose application plug-in module that
    receives notification of application startup and
    shutdown events.
  • Plug-in element may contain zero or more
    set-property elements, so that extra
    configuration information may be passed to Plugin
    class
  • E.g.
  • ltplug-in className"org.apache.struts.tiles.TilesP
    lugin"gt
  • ltset-property property"" value""/gt

6
Struts Configuration Elements
  • Action-mappings Element
  • Contains a set of zero or more action elements
    for Struts application
  • Action element describes a mapping from a
    specific request path to a corresponding action
    class
  • Controller selects a particular mapping by
    matching a URI path in the request with the path
    attribute in one of the action elements
  • E.g.
  • ltaction path/signin typecom.actions.LoginAct
    ion scoperequest nameloginForm
    validatetrue input/jsp/login.jspgt
  • ltforward nameSuccess pathjsp/login.jsp /gt
  • ltforward nameFailure pathjsp/invalidlogin.j
    sp /gt
  • lt/actiongt
  • Path attribute, in other words is the name of the
    action. It is the application relative path to
    the submitted request, starting with a /
    character and without filename extension
  • Scope is the scope in which form bean is placed
    either request or session. Default value is
    session. It is specified only when name attribute
    is specified.
  • Name is the name of the form bean element as
    specified in the form beams element
  • Validate attribute determines if validate method
    of ActionForm is called before invoking execute
    method of the action class
  • Type is the type of the action class
  • Unknown attribute determines whether this action
    should be configured as the default for this
    application. It is optional and default is false

7
The application resources file
  • Capable and flexible messaging system.
  • Text for the messages is stored in this
    properties file.
  • In java an jsp code, a key for the message is
    given the text of the message is retrieved from
    the properties file at runtime.
  • If you want to localize your application, you can
    include additional application resources file for
    the locales you would like to support.
  • The application name is only a convention. There
    is no framework default.

8
ActionForms
  • ActionForms are versatile objects that play the
    role of field harvester, data butter, type
    transformer and transfer object all within the
    span of a single request.
  • ActionForm as field harvester
  • To harvest request parameters, all the Struts
    developers needs to do is provide an ActionForm
    with JavaBean properties that match the names of
    HTTP parameters. The rest is automatic
  • ActionForm as data buffer
  • ActionForm fields are not the destination of the
    input, but a buffer to be validated before the
    input is committed.
  • ActionForm as a data validator
  • Prima facie validation of property values and
    type
  • ActionForm as a type transformer
  • Helper methods could be included on ActionForms
    to help with type conversions and
    transformations.
  • ActionForm as a transfer object
  • An ActionForm is a carrier for data to be used by
    another bean or process.

9
ActionClass
  • Action objects does the actual work.
  • Actions are multithreaded.
  • There is a single instance of any given Action
    subclass per application.
  • General Responsibilities
  • Validate preconditions
  • Call any needed business logic methods
  • Detect any other processing errors
  • Route control to the appropriate view
  • It is an adaptor between HTTP and the rest of the
    application. This way it provides means for
    simpler, broader, flexible and a robust design
  • The standard Actions
  • DispatchAction
  • Action class to handle several related tasks
  • Keeping related operations together simplifies
    maintenance and flow control
  • LookupDispatchAction
  • A convenient way to select a dispatch method in
    case of multiple submit.
Write a Comment
User Comments (0)
About PowerShow.com