Your SlideShare is downloading. ×
  • Like
Struts2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Struts2

  • 11,953 views
Published

This is a presentation I gave recently at the Apache conference

This is a presentation I gave recently at the Apache conference

Published in Technology , Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
11,953
On SlideShare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
909
Comments
0
Likes
12

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • I expect student has a fair understanding of Struts Classic

Transcript

  • 1. For Struts Developers
  • 2. Me
    • Scott Stanlick
      • Drummer
      • Techie
      • Instructor
      • Connoisseur of cereal malt beverages
  • 3. Agenda
    • Hello Struts 2
    • Action/Interceptor/Result
    • Configuration
    • OGNL/Value Stack
    • Tag Libraries
    • Business Validation
    • Plug-ins
    • Wrap up
  • 4. Hello Struts 2 The History
  • 5. What is Struts 2?
    • Not Struts 1.x!
    • EE5 Web Framework
    • Originally OpenSymphony WebWork
    • WebWork + Struts + Apache Incubator
      • Struts 2 was hatched in ‘07
  • 6. 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!
  • 7. Configuration Styles
    • Packaged actions (namespace)
    • Package inheritance
    • Wildcard mappings
    • Generic actions
    • Annotations
    • Ruby-On-Rails Rest-style mappings
      • Convention over configuration
  • 8. 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
  • 9. The Core Components
    • Actions
    • Interceptors
    • Results
  • 10. Differences from Struts Classic
    • No form beans
    • Actions are no longer singletons
    • HTTP-free
    • Intelligent defaults
    • Easy to extend with
      • Interceptors
      • Results
      • Plug-ins
  • 11. Action/Interceptor/Result The Request Pipeline
  • 12. ActionContext (ThreadLocal)
  • 13. MVC 2
    • Where C=Cool!
    • Request
      • FilterDispatcher
        • Dispatcher
          • StrutsActionProxy
            • ActionInvocation [ThreadLocal]
  • 14. 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!
  • 15. 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>
  • 16. 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>
  • 17. HREmployeeManager Action
    • class HREmployeeManager {
      • private Employee model;
      • private EmployeeService service;
      • public List getList(){…}
    • }
  • 18. 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!
  • 19. 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
  • 20. Interceptor Configuration
    • Easy to configure wrong!
    • config-browser plug-in provides a glimpse into the runtime
      • More about this later
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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>
  • 25. 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
  • 26. 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>
  • 27. 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
  • 28. 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
  • 29. Configuration Declaring the Web Site Capabilities
  • 30. 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>
  • 31. 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
  • 32. Package Inheritance
    • <package name=“hr&quot; extends=“base“ namespace=&quot;/hr&quot;>
      • <action name=“…”/>
    • </package>
    • struts-default
      • base
        • hr
  • 33. Intelligent Configuration
    • Declarative architecture
    • Configurable defaults
    • Extendable
      • Framework extension points
      • Sub-packages
    • Shipped in the struts2-core-2.m.n.jar
      • struts-default.xml
  • 34. 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>
  • 35. 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
  • 36. 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
  • 37. OGNL/ValueStack Dynamic Data/Object Graph
  • 38. 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
  • 39. 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()
  • 40. 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)
  • 41. ValueStack
    • Request “Object Stack”
    • OGNL expressions search it top down
    • Can specify object level
    • Common operations
      • peek
      • pop
      • push
  • 42. Type Conversions
    • OGNL uses reflection to convert types
    • JavaBeans convention provides “hint”
    • Custom converters can be added
      • Scope
        • Bean/Model
        • Action
        • Application
  • 43. Tag Libraries The UI
  • 44. 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
  • 45. Struts 2 Tags
    • Decoupled from underlying view technology
      • JSP
      • Velocity
      • FreeMarker
    • Markup via Freemarker templates
  • 46. 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
  • 47. Tag Templates
    • The markup generated is your choice
      • HTML
      • XML
      • JSON
      • WML
  • 48. 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
  • 49. Base select Component
    • View technology agnostic
    • org.apache.struts2.components.Select
    • Generates markup via TemplateEngine
    • S2 provides FreeMarkerTemplateEngine
      • Unites ValueStack & FreeMarker
  • 50. <s:select/> JSP Custom Tag
    • org.apache.struts2.views.jsp.ui.SelectTag
    • Renders a select widget
  • 51. <@s.select/> FreeMarker Tag
    • org.apache.struts2.views.freemarker.tags.SelectModel
    • Renders a select widget
  • 52. 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
  • 53. 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; }
  • 54. 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
  • 55. 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>
  • 56. 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>
  • 57. Business Validation Rules and Regulations
  • 58. Validation Styles
    • Annotations
    • XML
    • Java
  • 59. 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>
  • 60. 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>
  • 61. 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>
  • 62. 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>
  • 63. Plug-ins Extensions Simplified
  • 64. 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
  • 65. Configuration Browser
    • Exposes the runtime configuration
    • Accessed via a browser
      • http://.../config-browser/index.action
  • 66. 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
  • 67. Wrap up Odds & Ends
  • 68. 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!
  • 69. Resources
    • Manning
    • Nabble
    • Google
    • Also watch for
      • Struts 2 in Practice (Wes Wannemacher)