• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Struts2
 

Struts2

on

  • 13,046 views

This is a presentation I gave recently at the Apache conference

This is a presentation I gave recently at the Apache conference

Statistics

Views

Total Views
13,046
Views on SlideShare
13,011
Embed Views
35

Actions

Likes
12
Downloads
881
Comments
0

4 Embeds 35

http://www.slideshare.net 32
http://thebestsolutions.cn 1
http://www.mefeedia.com 1
http://newitfarmer.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • I expect student has a fair understanding of Struts Classic

Struts2 Struts2 Presentation Transcript

  • For Struts Developers
  • Me
    • Scott Stanlick
      • Drummer
      • Techie
      • Instructor
      • Connoisseur of cereal malt beverages
  • Agenda
    • Hello Struts 2
    • Action/Interceptor/Result
    • Configuration
    • OGNL/Value Stack
    • Tag Libraries
    • Business Validation
    • Plug-ins
    • Wrap up
  • Hello Struts 2 The History
  • What is Struts 2?
    • Not Struts 1.x!
    • EE5 Web Framework
    • Originally OpenSymphony WebWork
    • WebWork + Struts + Apache Incubator
      • Struts 2 was hatched in ‘07
  • Built Specifically For…
    • Developer Productivity
      • Simple HTTP-free code
      • Easy Spring integration
      • Reusable UI templates
      • Plug-in architecture
      • Crazy cool type conversions
      • Rich validation
      • Easy to test
      • And so much more!
  • Configuration Styles
    • Packaged actions (namespace)
    • Package inheritance
    • Wildcard mappings
    • Generic actions
    • Annotations
    • Ruby-On-Rails Rest-style mappings
      • Convention over configuration
  • Plug-in Architecture
    • Similar to Firefox and Eclipse
    • Add features by dropping in a jar
    • Several bundled plug-ins
      • Tiles
      • JFreeChart
      • JasperReports
      • REST-Style URI mappings
    • Plug-in registry growing steadily
  • The Core Components
    • Actions
    • Interceptors
    • Results
  • Differences from Struts Classic
    • No form beans
    • Actions are no longer singletons
    • HTTP-free
    • Intelligent defaults
    • Easy to extend with
      • Interceptors
      • Results
      • Plug-ins
  • Action/Interceptor/Result The Request Pipeline
  • ActionContext (ThreadLocal)
  • MVC 2
    • Where C=Cool!
    • Request
      • FilterDispatcher
        • Dispatcher
          • StrutsActionProxy
            • ActionInvocation [ThreadLocal]
  • Action
    • Packaged according to like kind
      • Sort of like Java packages
    • ThreadLocal (safe)
    • Typically extend ActionSupport
    • Contain your domain model
      • Can be model driven
    • May contain multiple methods
    • Not tangled up with Servlet/API
    • Easy to test!
  • Action Mapping
    • <package name=&quot;hr&quot; namespace=&quot;/hr&quot; extends=&quot;starter&quot;>
      • <action
      • name=“ uri “ class=“ class “ method=“ method &quot;>
      • <result>destination</result>
      • </action>
    • <package>
  • Wildcard Action Mapping
    • <package name=&quot;hr&quot; namespace=&quot;/hr&quot; extends=&quot;starter&quot;>
      • <action
      • name=“employeeAction_*&quot;
      • class=“HREmployeeManager&quot;
      • method=“{1} &quot;>
      • <result>/employee/{1}.jsp</result>
      • </action>
    • <package>
  • HREmployeeManager Action
    • class HREmployeeManager {
      • private Employee model;
      • private EmployeeService service;
      • public List getList(){…}
    • }
  • Interceptor
    • Intercepts request
      • Think AOP
    • Called before/after your action
    • Useful for cross-cutting concerns
    • Built-ins cover 98+% of all use cases
    • Configured at package or action level
    • Is central to the framework itself
      • Eat your own dog food!
  • Interceptor Stack
    • An interceptor has a specific role
      • Parameter interception
      • Validation
      • Workflow
    • Named “stacks” provide pluggable lists pre-assembled for goal specific cases
      • defaultStack
      • fileUploadStack
      • modelDrivenStack
      • paramsPrepareParamsStack
  • Interceptor Configuration
    • Easy to configure wrong!
    • config-browser plug-in provides a glimpse into the runtime
      • More about this later
  • Timer Interceptor
    • Stopwatch feature for action execution
      • “ starts” before your action
      • “ stops” afterward
    • Writes performance timings to log
    • Order of interceptors is interesting
      • See next slide
  • Timer Interceptor (cont.)
    • <interceptors>
    • <interceptor-stack name=&quot;stackWithTimer&quot;>
    • <interceptor-ref name=&quot; timer &quot;/>
    • <interceptor-ref name=&quot;defaultStack&quot;/>
    • </interceptor-stack>
    • </interceptors>
    • records action's execution with interceptors
  • Timer Interceptor (cont.)
    • <interceptors>
    • <interceptor-stack name=&quot;stackWithTimer&quot;>
    • <interceptor-ref name=&quot;defaultStack&quot;/>
    • <interceptor-ref name=&quot; timer &quot;/>
    • </interceptor-stack>
    • </interceptors>
    • records only the action's execution time
  • Default Stack
    • <interceptor-stack name=&quot;defaultStack&quot;>
    • <interceptor-ref name=&quot;exception&quot;/>
    • <interceptor-ref name=&quot;alias&quot;/>
    • <interceptor-ref name=&quot;servletConfig&quot;/>
    • <interceptor-ref name=&quot;prepare&quot;/>
    • <interceptor-ref name=&quot;i18n&quot;/>
    • <interceptor-ref name=&quot;chain&quot;/>
    • <interceptor-ref name=&quot;debugging&quot;/>
    • <interceptor-ref name=&quot;profiling&quot;/>
    • <interceptor-ref name=&quot;scopedModelDriven&quot;/>
    • <interceptor-ref name=&quot;modelDriven&quot;/>
    • <interceptor-ref name=&quot;fileUpload&quot;/>
    • <interceptor-ref name=&quot;checkbox&quot;/>
    • <interceptor-ref name=&quot;staticParams&quot;/>
    • <interceptor-ref name=&quot;params&quot;>
    • <param name=&quot;excludeParams&quot;>dojo..*</param>
    • </interceptor-ref>
    • <interceptor-ref name=&quot;conversionError&quot;/>
    • <interceptor-ref name=&quot;validation&quot;>
    • <param name=&quot;excludeMethods&quot;>input,back,cancel,browse</param>
    • </interceptor-ref>
    • <interceptor-ref name=&quot;workflow&quot;>
    • <param name=&quot;excludeMethods&quot;>input,back,cancel,browse</param>
    • </interceptor-ref>
    • </interceptor-stack>
  • Result
    • What should be returned to requester?
    • Many provided out of the box
    • It’s easy to write your own
      • Graphics
      • JSON
      • PDF
    • It’s another configurable object
  • Result Types
    • <result-types>
    • <result-type name=&quot;chain&quot; class=“…&quot;/>
    • <result-type name=&quot; dispatcher &quot; class=“…&quot; default=&quot;true&quot; />
    • <result-type name=&quot;freemarker&quot; class=“…&quot;/>
    • <result-type name=&quot;httpheader&quot; class=“…&quot;/>
    • <result-type name=&quot;redirect&quot; class=“…&quot;/>
    • <result-type name=&quot;redirectAction&quot; class=“…&quot;/>
    • <result-type name=&quot;stream&quot; class=“…&quot;/>
    • <result-type name=&quot;velocity&quot; class=“…&quot;/>
    • <result-type name=&quot;xslt&quot; class=“…&quot;/>
    • <result-type name=&quot;plainText&quot; class=“…&quot; />
    • <result-type name=&quot;plaintext&quot; class=“…&quot; />
    • </result-types>
  • Result Example
    • <action …>
    • <result name =&quot;success&quot; type =&quot;dispatcher&quot;>
    • /employee/list.jsp
    • </result>
    • </action>
    • name and type are optional
      • name defaults to success
      • type default can be set by package
  • Connecting the Wildcard Dots
    • <action name=“employeeAction_*“ class=“HREmployeeManager“ method=“{1} &quot;>
      • <result>/employee/{1}.jsp</result>
    • </action>
    • www.foo.com/employeeAction_list
      • list()
      • list.jsp
  • Configuration Declaring the Web Site Capabilities
  • struts.xml
    • Core configuration file for framework
    • Can contain multiple packages
    • <struts>
      • <package name=“base&quot; extends=&quot;struts-default&quot;>
      • <action...>
      • </package>
      • <include file=“hr.xml&quot;/>
      • <include file=“logon.xml&quot;/>
      • <include file=“preferences.xml&quot;/>
    • </struts>
  • struts-default.xml
    • Most of your packages will extend this
      • Directly/Indirectly
    • Contains about everything you need
    • Packages form inheritance hierarchies
    • The key sections are
      • package
      • result-types
      • interceptors/stacks
  • Package Inheritance
    • <package name=“hr&quot; extends=“base“ namespace=&quot;/hr&quot;>
      • <action name=“…”/>
    • </package>
    • struts-default
      • base
        • hr
  • Intelligent Configuration
    • Declarative architecture
    • Configurable defaults
    • Extendable
      • Framework extension points
      • Sub-packages
    • Shipped in the struts2-core-2.m.n.jar
      • struts-default.xml
  • Sample Declaration
    • <package name=“hr“ extends=“base“ namespace=&quot;/hr&quot;>
    • <action name=&quot;list“
    • method=“preload” class=“HREmployeeManager”>
      • <result>/pages/hr/employee/list.jsp</result>
    • </action>
    • </package>
  • Constant Configuration
    • Modify framework and plug-in behavior
    • Can be declared at multiple levels
    • Searched in the following order
      • struts-default.xml
      • struts-plugin.xml
      • struts.xml
      • struts.properties
      • web.xml
    • Subsequent files override previous ones
  • struts.properties
    • struts.devMode=true
    • struts.objectFactory=spring
    • struts.configuration.xml.reload=true
    • struts.freemarker.templatesCache=true
    • struts.ui.theme=simple
    • struts.enable.DynamicMethodInvocation=false
    • struts.custom.i18n.resources=resources.Messages
  • OGNL/ValueStack Dynamic Data/Object Graph
  • Dynamic Data
    • Where is dynamic data stored?
      • Data to generate pages
      • User input from pages
    • Form beans are gone
    • Dependence on Servlet/API is bad
    • Welcome to the ValueStack
  • ValueStack
    • Leverages the OGNL framework
      • Object Graph Navigation Language
    • Extends OGNL to support searching stack
      • Top down to locate object/property
    • Where Interceptors put/get object data
    • Thread Safe
    • ActionContext . getContext ().getValueStack()
  • Struts 2 and OGNL OGNL OGNL Context ( ActionContext ) Value Stack (OGNL root) |___Action |___other objects… #parameters #request #session #application #attr (searches p, r, s, then a)
  • ValueStack
    • Request “Object Stack”
    • OGNL expressions search it top down
    • Can specify object level
    • Common operations
      • peek
      • pop
      • push
  • Type Conversions
    • OGNL uses reflection to convert types
    • JavaBeans convention provides “hint”
    • Custom converters can be added
      • Scope
        • Bean/Model
        • Action
        • Application
  • Tag Libraries The UI
  • Struts 2 Tags
    • Tags are divided into 2 sets
      • UI
        • Form/Non-Form
        • Ajax
          • Leverages dojo
      • Generic
        • Control
          • if, else, iterator
        • Data
          • bean, text, url
  • Struts 2 Tags
    • Decoupled from underlying view technology
      • JSP
      • Velocity
      • FreeMarker
    • Markup via Freemarker templates
  • S2 Tag Syntax
    • The value attribute is not a string
      • It’s an OBJECT
    • What you pass is evaluated as an expression!
    • <s:sometag … value=“foo”/>
      • Looks for getFoo()
    • <s:sometag … value=“{‘foo’}”/>
      • String literal is passed
  • Tag Templates
    • The markup generated is your choice
      • HTML
      • XML
      • JSON
      • WML
  • UI Tag Templates
    • Template technology is FreeMarker
    • CSS classes can be used to control L&F
    • You may tweak provided templates
    • Tight affinity with ValueStack
  • Base select Component
    • View technology agnostic
    • org.apache.struts2.components.Select
    • Generates markup via TemplateEngine
    • S2 provides FreeMarkerTemplateEngine
      • Unites ValueStack & FreeMarker
  • <s:select/> JSP Custom Tag
    • org.apache.struts2.views.jsp.ui.SelectTag
    • Renders a select widget
  • <@s.select/> FreeMarker Tag
    • org.apache.struts2.views.freemarker.tags.SelectModel
    • Renders a select widget
  • StrutsRequestWrapper
    • JSTL does not know OGNL
    • This class wraps the Struts request
    • Delegates to the Value Stack
    • ${customer.address.city}
      • Resolves to stack.find under the covers
  • S2 and JSTL
    • OGNL values are exposed to JSP/JSTL
    • Expression syntax is a little different
      • JSTL: ${a.b.c}
      • OGNL: %{a.b.c}
    • Beware of JSP 2.1 Unified EL JSR 245
      • EL has been expanded to interpret #{}
      • Clashes with OGNL usage for Maps
        • #{ &quot;foo&quot; : &quot;foo value&quot;, &quot;bar&quot; : &quot;bar value&quot; }
  • Themes
    • S2 UI tags grouped into themes
      • simple - minimal with no &quot;bells and whistles&quot;
      • xhtml - uses common HTML practices
      • css_xhtml - xhtml theme re-implemented using strictly CSS for layout
      • Ajax - based on the xhtml theme that provides advanced AJAX features
    • Specified globally or at the tag level
  • Simple Theme <H2>Employee Save</H2> <s:form> <s:textfield key=&quot;employee.name“ required=&quot;true&quot;/> <s:textfield key=&quot;employee.department“ required=&quot;true&quot;/> </s:form>
  • XHTML Theme <H2>Employee Save</H2> <s:form> <s:textfield key=&quot;employee.name“ required=&quot;true&quot;/> <s:textfield key=&quot;employee.department“ required=&quot;true&quot;/> </s:form>
  • Business Validation Rules and Regulations
  • Validation Styles
    • Annotations
    • XML
    • Java
  • Simple Field Example
    • Form
      • <s:textfield key=&quot;age&quot;/>
    • Action
      • private int age; get/set
    • Validator <ActionClassName>-validation.xml
      • <field name=&quot;age&quot;>
        • <field-validator type=&quot;int&quot;>
          • <param name=&quot;min&quot;>13</param>
          • <param name=&quot;max&quot;>19</param>
          • <message>Only people ages 13 to 19 allowed</message>
        • </field-validator>
      • </field>
  • Validator Types
    • conversion
    • date
    • email
    • short – int – long - double
    • regex <takes a regular expression>
    • required
    • requiredstring
    • stringlength
    • url
    • visitor - conditionalvisitor
    • fieldexpression <resolves an OGNL expression>
    • expression <resolves an OGNL expression>
  • Another Field Example
    • <field name=“password&quot;>
    • <field-validator type=&quot;expression&quot;>
    • <param name=&quot;expression&quot;>
    • password.equals(password2)
    • </param>
    • <message>
    • Password 2 must equal ${password}
    • </message>
    • </field-validator>
    • </field>
  • Non-Field Validation
    • <validators>
      • <validator type=&quot;expression&quot;>
      • <param name=&quot;expression“> ( (a==b) && (b==c) )</param>
      • <message>all three fields must be the same></message>
      • </validator>
    • </validators>
  • Plug-ins Extensions Simplified
  • Benefits of plug-ins
    • Strategic points in the framework where you can
      • Provide you own implementations
      • Add new features
    • Add functionality without modifying code
      • Open-Closed Principle
  • Configuration Browser
    • Exposes the runtime configuration
    • Accessed via a browser
      • http://.../config-browser/index.action
  • OGNL Viewer
    • Provides debugging screens to provide insight into the data behind the page
    • Accessed via a browser
      • http://..?debug={flag}
    • Flag values
      • xml, console, command, browser
    • Make sure
      • DebuggingInterceptor is included
      • struts.devMode=true
  • Wrap up Odds & Ends
  • My Web Site
    • www.struts2inaction.com
    • The cobbler’s children have no shoes
    • Too many paying gigs to lace up my own site!
    • Vacation time coming
      • It will rock!
  • Resources
    • Manning
    • Nabble
    • Google
    • Also watch for
      • Struts 2 in Practice (Wes Wannemacher)