Struts Basics - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

Struts Basics

Description:

Facilitates communication between HTML designers and developers ... Collections iterate. Template Tags. Templates are JSP pages that include parameterized content ... – PowerPoint PPT presentation

Number of Views:967
Avg rating:3.0/5.0
Slides: 51
Provided by: triton8
Category:
Tags: basics | struts

less

Transcript and Presenter's Notes

Title: Struts Basics


1
Struts Basics
  • COSC 617
  • Created by Marc Zhang
  • Lianjiang Zong
  • Yedong Tang

2
Struts Basics
  • What is and Why Struts?
  • Struts architecture
  • Controller
  • View
  • Model
  • Struts application
  • Example

3
Overview of Struts
  • open source
  • A framework
  • For building web application
  • Advantage

4
What is Struts?
  • Struts Frame work is the implementation of
    Model-View-Controller (MVC) design pattern for
    the JSP.
  • Struts is maintained as a part of Apache Jakarta
    project and is open source.
  • Struts Framework is suited for the application of
    any size.

5
Why Struts?
  • Takes much of the complexity out of building
    your own MVC framework
  • Encourages good design practice and modeling
  • Easy to learn and use
  • Feature-rich
  • Many supported 3rd-party tools

6
Why Struts? Cont.
  • Flexible and extensible
  • Large user community
  • Stable and mature
  • Open source
  • Integrates well with J2EE
  • Good taglib support

7
Why Struts? Cont.
  • Works with existing web apps
  • Easy to retain form state
  • Unified error handling programmatically and
    declaratively
  • Integration with Tiles framework
  • Clear delineation of responsibility makes long
    term maintenance easier (more modular)

8
Struts architecture

controller
model
view
9
Struts architecture
  • Base on MVC design pattern
  • 3 Major Components in Struts
  • Servlet controller (Controller)
  • Java Server Pages or any other presentation
    technology (View)
  • Application Business Logic in the form of
    whatever suits the application (Model)

10
Advantages of Struts architecture
  • dividing application components into three
    different categories Model
  • component is independent of the other component
  • Changes in one component will have no or less
    impact on other component

11
  • Components of Controller, Model and view

12
Responsibilities and components of Model
  • Model
  • providing the data from the database
  • saving the data into the data store
  • content
  • Data access
  • Data validation
  • data saving logic

13
Responsibilities and components of View
  • content
  • HTML
  • JSPs
  • Custom Tag Libraries
  • Resources files

14
Responsibilities and components of View cont.
  • View
  • represents view of users application
  • taking the input from the user
  • dispatching the request to the controller
  • receiving response from the controller
  • displaying the result to the user

15
Responsibilities and content of Controller.
  • Content
  • ActionServlet(Provided by Struts)
  • Action(Provided by developer)
  • RequestProcessor(Provided by Struts)
  • Action Mapping(Specified by developer)

16
Responsibilities and components of Controller
  • Controller
  • receiving the request from client
  • executing the appropriate business logic from
    the Model
  • producing the output to the user using the View
    component


17
ActionServlet
  • Performs the role of Controller
  • Delegates most of this grunt work to Action
    classes
  • Is responsible for initialization and clean-up
    of resources

18
(No Transcript)
19
RequestProcessor
  • processPath
  • processLocale
  • processContent
  • processNoCache
  • processPreprocess
  • processMapping
  • processRoles

20
RequestProcessor cont.
  • processActionForm
  • processPopulate
  • processValidate
  • processForward
  • processInclude
  • processActionCreate
  • processActionPerform
  • processForwardConfig

21
Action Mapping
  • three important elements of struts-config.xml
    used to describe actions
  • ltform-beansgt
  • ltaction-mappinggt
  • ltglobal-forwardsgt

22
ActionForm Bean
  • Define an ActionForm bean for the input form in
    struts-config.xml file
  • Contains only property getter and property
    setter methods for each field-no business logic
  • Provides validation mechanism

23
Action
  • a Java class that does the work of your
    application
  • Handle request
  • Perform business logic
  • Simple action class
  • Sophisticated action class

24
execute() method of Action class
  • Perform the processing required to deal with the
    request
  • Update the server-side objects (Scope variables)
    that will be used to create the next page of the
    user interface
  • Return an appropriate ActionForward object

25
Model Components
  • Model divided into concepts
  • Internal state of the system
  • Business logic that can change that state
  • Internal state of system represented by
  • JavaBeans
  • Enterprise JavaBeans
  • POJO's
  • JDO
  • JDBC
  • Whatever

26
Model Components (cont.)
  • JavaBeans,Enterprise JavaBeans

27
Model Components (cont.)
  • JavaBeans and Scope
  • Page visible within a single JSP page, for the
    lifetime of the current request
  • Request visible within a single JSP page, as
    well as to any page or servlet that is included
    in this page, or forwarded to by this page
  • Session visible to all JSP pages and servlets
    that participate in a particular user session,
    across one or more requests
  • Application - visible to all JSP pages and
    servlets that are part of a web application

28
Model Components (cont.)
  • JSP pages and servlets in the same web
    application share the same sets of bean
    collections
  • Example
  • Servlet code
  • MyCart mycart new MyCart(...)
  • request.setAttribute("cart", mycart)
  • JSP page
  • ltjspuseBean id"cart" scope"request"
  • class"com.mycompany.MyApp.MyCart"/gt

29
Model Components in StrutsFramework
  • SystemState Bean
  • This is a conceptual term Struts does not
    provide any programming API.
  • Struts does not define formal class of this
  • Defines the current state
  • Could be represented as a set of one or
    more
  • JavaBeans classes, whose properties define
    the
  • current state

30
Model Components in StrutsFramework(cont.)
  • Example a shopping cart system
  • Contains a bean that represents the cart being
    maintained for each individual shopper
  • Includes the set of items that the shopper has
    currently selected for purchase
  • BusinessLogic Bean
  • This is a conceptual term Struts does not
    provide any programming API

31
Model Components in StrutsFramework(cont.)
  • Struts does not define formal class of this
  • Can be an ordinary JavaBean
  • Can be stateful or stateless EJB
  • Encapsulates functional logic of an application
    using method calls
  • Action object should translate the HTTP request
    then call BusinessLogic bean
  • Ideally should be designed so that they do not
    know they are being executed in a web application
    environment
  • should not refer any Web application objects
  • enhanced reusability

32
View Components
33
View Components cont.
  • JSP files which you write for your specific
    application
  • Set of JSP custom tag libraries
  • Resource files for internationalization
  • Allows for fast creation of forms for an
    application
  • Works in concert with the controller Servlet

34
View Components cont.
  • ActionForward object tells Servlet controller
    which JSP page is to be dispatched to
  • JSP pages use ActionForm beans to get output
    Model data to display
  • Struts contains a series of tag libraries
  • Facilitates communication between HTML
    designers and developers
  • Facilitates dynamic Web content

35
Forms and FormBean Interactions
  • How do you reflect the previous value a user has
    entered in a subsequent page?
  • If a user makes an error in only one field
    in page among the multiple fields, the
    application should allow him to fix just that
    field
  • With just JSP, you have to do
  • ltinput type"text" name"username"
  • value"lt loginBean.getUsername() gt"/gt
  • With Struts, you can do
  • lthtmltext property"username"/gt

36
Example submit.jsp
  • 1 lt_at_ page language"java" gt
  • 2 lt_at_ taglib uri"/WEB-INF/struts-bean.tld"
    prefix"bean" gt
  • 3 lt_at_ taglib uri"/WEB-INF/struts-html.tld"
    prefix"html" gt
  • 4 lt_at_ taglib uri"/WEB-INF/struts-logic.tld"
    prefix"logic" gt
  • 5
  • 6 lthtmlgt
  • 7 ltheadgtlttitlegtSubmit examplelt/titlegtlt/headgt
  • 8 ltbodygt
  • 9
  • 10 lth3gtExample Submit Pagelt/h3gt
  • 11
  • 12 lthtmlerrors/gt
  • 13
  • 14 lthtmlform action"submit.do"gt
  • 15 Last Name lthtmltext property"lastName"/gtltbrgt
  • 16 Address lthtmltextarea property"address"/gtltbr
    gt
  • 17 Sex lthtmlradio property"sex"
    value"M"/gtMale
  • 18 lthtmlradio property"sex" value"F"/gtFemaleltbr
    gt
  • 19 Married lthtmlcheckbox property"married"/gtltbr
    gt

37
Struts Tags
  • Tag Libraries Overview
  • Number of taglibs included as part of Struts
    Usage is not required, but helpful
  • Bean tags Tags for accessing Beans and their
    properties
  • Html tags Form bridge between JSP view and
    other components
  • Logic tags Provides presentation logic tags
    that eliminate need for scriptlets
  • Template tags Tags to form JSP templates that
    include parameterized content
  • Nested Tags Allows for object hierarchy and
    helpful for rendering lists of lists

38
When to JSTL in your Strutsapplication?
  • Developers should evaluate when to use the JSTL
    (JavaServer Pages Standard Tag Library )
  • Many of the Struts taglib features are now
    available in the JSTL
  • Its simple If the tag exists in the JSTL use
    it
  • Continue using the Struts tags where appropriate,
    they will continue to be supported

39
Interaction with JSTL
  • Struts-el taglibs allow for using expression
    values instead of just rtexprvalue
  • Runtime ltbeanmessage key'lt stringvar
    gt'/gt
  • Expression ltbean-elmessage
    key"stringvar"/gt
  • Set of optional taglibs that can be used with
    the JSTL expression language (EL)
  • Implements many (but not all) of the Struts tags.
  • Located in the contrib folder of the Struts
    release
  • Container with servlet 2.3 support required

40
Access to Tag Libraries
  • All tag libraries are defined in web.xml using
  • lttaglibgt element
  • lt!-- Struts Tag Library Descriptors --gt
  • lttaglibgt
  • lttaglib-urigt/WEB-INF/struts-bean.tldlt/taglib-uri
    gt
  • lttaglib-locationgt/WEB-INF/struts-bean.tldlt/tagli
    b-locationgt
  • lt/taglibgt
  • lttaglibgt
  • lttaglib-urigt/WEB-INF/struts-html.tldlt/taglib-uri
    gt
  • lttaglib-locationgt/WEB-INF/struts-html.tldlt/tagli
    b-locationgt
  • lt/taglibgt

41
Bean Tags
  • Tags for accessing beans and their properties
  • Enhancements to ltjspuseBeangt
  • Convenient mechanisms to create new beans based
    on the value of
  • Cookies
  • Request Headers
  • Parameters

42
HTML Tags
  • Form bridge between JSP view and other components
  • Input forms are important for gathering
    userentered data
  • Most of the actions of the HTML taglib involve
    HTML forms
  • Error messages, hyperlinking, internationalization
  • HTML tags must be nested within a form tag
  • inform tag handler which bean to use for
    initializing displayed values

43
Logic Tags
  • Provides presentation logic tags that eliminate
    need for scriptlets
  • Value comparisons Include ! lt gt lt gt
  • Substring matching match, not match
  • Presentation logic forward, redirect
  • Collections iterate

44
Template Tags
  • Templates are JSP pages that include
    parameterized content
  • Useful for creating dynamic JSP templates for
    pages that share a common format
  • Functionality provided is similar to what can be
    achieved using the standard JSP include
    directive, but these tags allow for dynamic
    rather than static content

45
Template Tags cont.
  • Three template tags work in an interrelated
    function
  • Get - Gets the content from request scope
    that was put there by a put tag.
  • Insert - Includes a template
  • Put - Puts content into request scope

46
DynaActionForm
  • Issues with using ActionForm
  • ActionForm class has to be created in Java
    programming language
  • For each HTML form page, a new ActionForm
    class has to be created
  • Every time HTML form page is modified (a
    property is added or removed), ActionForm class
    has to be modified and recompiled
  • DynaActionForm support is added to Struts 1.1 to
    address these issues

47
What is DynaActionForm?
  • org.apache.struts.action.DynaActionForm
  • extends ActionForm class
  • In DynaActionForm scheme,
  • Properties are configured in configuration
    file rather than coding
  • reset() method resets all the properties
    back to their initial values
  • You can still subclass DynaActionForm to
    override reset() and/or validate() methods
  • Version exists that works with Validator
    framework to provide automatic validation

48
Types Supported byDynaActionForm
  • java.lang.BigDecimal, java.lang.BigInteger
  • boolean and java.lang.Boolean
  • byte and java.lang.Byte
  • char and java.lang.Character
  • java.lang.Class, double and java.lang.Double
  • float and java.lang.Float
  • int and java.lang.Integer
  • long and java.lang.Long
  • short and java.lang.Short
  • java.lang.String
  • java.sql.Date, java.sql.Time, java.sql.Timestamp

49
Struts Validator
  • The Struts Validator, in some form, has been
    available since the days of Struts 0.5. It was
    originally packaged as a developer contribution.
    Later, the core code was moved to the Jakarta
    Commons and a Struts specific extension became
    part of Struts since 1.1.

50
How to perform Validation withDynaActionForm?
  • DynaActionForm does not provide default behavior
    for validate() method
  • You can subclass and override validate()
    method but it is not recommended
  • Use Validator Framework
  • Use DynaValidatorForm class (instead of
    DynaActionForm class)
  • DynaValidatorForm class extends
    DynaActionForm and provides basic field
    validation based on an XML file
Write a Comment
User Comments (0)
About PowerShow.com