Introducing Struts 2


Published on

Introductory presentation to action-based Java MVC framework Struts 2.

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introducing Struts 2

  1. 1. Introducing Struts 2 JaMU 07.02 – February 24, 2007 Thomas Wiradikusuma ( Java User Group Indonesia
  2. 2. Struts 2 defined <ul><li>An elegant, extensible framework for building enterprise-ready Java web applications </li></ul><ul><li>An MVC web framework </li></ul><ul><li>Action-based </li></ul><ul><li>Successor of the famous Struts framework (and WebWork 2, technically speaking) </li></ul>
  3. 3. Sweet spots <ul><li>Simple architecture </li></ul><ul><li>Interceptors, Actions, Results from pluggable ObjectFactory </li></ul><ul><li>Controller-based or page-based navigation </li></ul><ul><li>Support for POJO, Annotation, JSF </li></ul><ul><li>Cool, customizable tag library with OGNL support </li></ul><ul><li>Value stack </li></ul><ul><li>Spring as default inversion of control container </li></ul><ul><li>QuickStart </li></ul><ul><li>Built-in Ajax support </li></ul><ul><li>Many convention-over-configuration and sensible defaults </li></ul><ul><li>Easier to test (out of container) </li></ul><ul><li>Brings the best of Struts 1 and WebWork 2, including their fanatic followers ;-) </li></ul>
  4. 4. Architecture <ul><li>Request arrives </li></ul><ul><li>FilterDispatcher finds appropriate Action </li></ul><ul><li>Interceptors get applied </li></ul><ul><li>Method in Action executes (usually doing “core” stuff) </li></ul><ul><li>Result renders output </li></ul>
  5. 5. Architecture, cont’d
  6. 6. ActionMapper <ul><li>Provide a mapping between HTTP requests and action invocation requests and vice-versa </li></ul><ul><li>Default implementation uses standard *.[ext] (usually &quot;action&quot;) pattern. Extension is defined in Struts configuration key struts.action.exection. Prefixes: </li></ul><ul><ul><li>method: <a:submit name=“method:bar” value=“Bar”/> </li></ul></ul><ul><ul><li>action: <a:submit name=“action:foo” value=“Foo”/> </li></ul></ul><ul><ul><li>redirect: <a:submit name=“redirect:” value=“Google”/> </li></ul></ul><ul><ul><li>redirect-action: <a:submit name=“redirect-action:foo” value=“Foo”/> </li></ul></ul>
  7. 7. Interceptors <interceptor-stack name=&quot;xaStack&quot;> <interceptor-ref name=&quot;thisWillRunFirstInterceptor&quot;/> <interceptor-ref name=&quot;thisWillRunNextInterceptor&quot;/> <interceptor-ref name=&quot;followedByThisInterceptor&quot;/> <interceptor-ref name=&quot;thisWillRunLastInterceptor&quot;/> </interceptor-stack> thisWillRunFirstInterceptor thisWillRunNextInterceptor followedByThisInterceptor thisWillRunLastInterceptor MyAction1 MyAction2 (chain) MyPreResultListener MyResult (result) thisWillRunLastInterceptor followedByThisInterceptor thisWillRunNextInterceptor thisWillRunFirstInterceptor
  8. 8. Interceptors, cont’d Checks for valid token presence in Action, prevents duplicate form submission. token Token And many more… Action will only be executed if the user has the correct JAAS role. roles Roles If the Action implements Preparable, calls its prepare method. prepare Prepare Sets the request parameters onto the Action. params Parameters Executes the Action in the background and then sends the user off to an intermediate waiting page. execAndWait Execute and Wait Adds automatic checkbox handling code that detect an unchecked checkbox and add it as a parameter with a default (usually 'false') value. Uses a specially named hidden field to detect unsubmitted checkboxes. checkbox Checkbox Description Name Interceptor
  9. 9. Actions <ul><li>All actions may implement this interface, which exposes the execute() method. However, as of XWork 1.1, this is not required and is only here to assist users. You are free to create POJOs that honor the same contract defined by this interface without actually implementing the interface. </li></ul><ul><li>ActionSupport class provides a default implementation for the most common actions. </li></ul>
  10. 10. Results Used for Velocity integration Velocity And many more… Used for Action Chaining Chain Used to stream an InputStream back to the browser (usually for file downloads) Stream Used to redirect to another action mapping Redirect Action Used to redirect to another URL (web resource) Redirect Used for web resource integration, including JSP integration Dispatcher Usage Result
  11. 11. Type conversion <ul><li>Everything is String in HTTP </li></ul><ul><li>Built-in: boolean, char, numeric types, dates, arrays, collections </li></ul><ul><li> </li></ul><ul><ul><li>foo = package.FooConverter </li></ul></ul><ul><li>Globally in in the root of your class path (typically WEB-INF/classes) </li></ul><ul><ul><li>package.Foo = package.FooConverter </li></ul></ul><ul><li>Extend StrutsTypeConverter to simplify creating a converter </li></ul><ul><li>Throw TypeConversionException when conversion exception happens. Information will be displayed as specified in Struts configuration struts.default.invalid.fieldvalue </li></ul>
  12. 12. Localization <ul><li>Resource bundles are searched in the following order: </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> (all the way to </li></ul></ul><ul><ul><li> (every interface and sub-interface) </li></ul></ul><ul><ul><li>ModelDriven's model (if implements ModelDriven), for the model object repeat from 1 </li></ul></ul><ul><ul><li> (of the directory where class is located and every parent directory all the way to the root directory) </li></ul></ul><ul><ul><li>search up the i18n message key hierarchy itself </li></ul></ul><ul><ul><li>global resource properties </li></ul></ul><ul><li>Accessing key: </li></ul><ul><ul><li>getText: <s:property value=&quot;getText('some.key')&quot; /> </li></ul></ul><ul><ul><li>text tag: <s:text name=&quot;some.key&quot; /> </li></ul></ul><ul><ul><li>I18n tag to push an arbitrary resource bundle on to the value stack: </li></ul></ul><ul><ul><li><s:i18n name=&quot;some.package.bundle&quot; > </li></ul></ul><ul><ul><li><s:text name=&quot;some.key&quot; /> </li></ul></ul><ul><ul><li></s:i18n> </li></ul></ul>
  13. 13. Configuration files Override the default Velocity configuration /WEB-INF/classes/ Yes Optional configuration files for plug-ins in the same format as struts.xml. Root of a plug-in JAR Yes struts-plugin.xml Default macros referenced by /WEB-INF/classes/ Yes struts-default.vm Default configuration provided by Struts /WEB-INF/lib/struts2-core.jar Yes struts-default.xml Framework properties /WEB-INF/classes/ Yes Main configuration, contains result/view types, action mappings, interceptors, and so forth /WEB-INF/classes/ Yes struts.xml Deployment descriptor /WEB-INF/ No web.xml Purpose Location Optional? File
  14. 14. Taglib <% User user = ActionContext.getContext() %> <form action=&quot;Profile_update.action&quot; method=&quot;post&quot;> <table> <tr> <td> align=&quot;right&quot;><label>First name:</label></td> <td><input type=&quot;text&quot; name=&quot;user.firstname&quot; value=&quot;<%=user.getFirstname() %> /></td> </tr> <tr> <td> <input type=&quot;radio&quot; name=&quot;user.gender&quot; value=&quot;0&quot; id=&quot;user.gender0&quot; <% if (user.getGender()==0) { %> checked=&quot;checked&quot; %> } %> /> <label for=&quot;user.gender0&quot;>Female</label> ... Without taglib (JSP): <s:actionerror/> <s:form action=&quot;Profile_update&quot; validate=&quot;true&quot;> <s:textfield label=&quot;Username&quot; name=&quot;username&quot;/> <s:password label=&quot;Password&quot; name=&quot;password&quot;/> <s:password label=&quot;(Repeat) Password&quot; name=&quot;password2&quot;/> <s:textfield label=&quot;Full Name&quot; name=&quot;fullName&quot;/> <s:textfield label=&quot;From Address&quot; name=&quot;fromAddress&quot;/> <s:textfield label=&quot;Reply To Address&quot; name=&quot;replyToAddress&quot;/> <s:submit value=&quot;Save&quot; name=&quot;Save&quot;/> <s:submit action=&quot;Register_cancel&quot; value=&quot;Cancel&quot; name=&quot;Cancel&quot; onclick=&quot;form.onsubmit=null&quot;/> </s:form> With taglib (JSP):
  15. 15. Taglib, cont’d <ul><li><%@ taglib prefix=&quot;s&quot; uri=&quot;/struts-tags&quot; %> </li></ul><ul><li>Tags category: </li></ul><ul><ul><li>Control </li></ul></ul><ul><ul><li>Data </li></ul></ul><ul><ul><li>UI (form and non-form) </li></ul></ul><ul><ul><li>Ajax </li></ul></ul>
  16. 16. Servlet/JSP scoped object <ul><li>Application </li></ul><ul><ul><li><s:property value=&quot;%{}&quot; /> </li></ul></ul><ul><li>Session </li></ul><ul><ul><li><s:property value=&quot;%{}&quot; /> </li></ul></ul><ul><li>Request </li></ul><ul><ul><li><s:property value=&quot;%{}&quot; /> </li></ul></ul><ul><li>Parameter </li></ul><ul><ul><li><s:property value=&quot;%{}&quot; /> </li></ul></ul><ul><li>Context </li></ul><ul><ul><li><s:property value=&quot;%{#foo}&quot; /> </li></ul></ul>
  17. 17. OGNL <ul><li>Object Graph Navigation Language </li></ul><ul><li>More powerful than JSTL EL </li></ul>
  18. 18. Spring integration <ul><li>By default, the framework will at least try to use Spring to create all its objects. If the object cannot be created by Spring, then the framework will create the object itself. </li></ul>
  19. 19. Testing <ul><li>Direct Action invocation </li></ul><ul><li>Out of container testing </li></ul><ul><li>Testing Interceptors and/or Results </li></ul>
  20. 20. Struts 1 to Struts 2 Despite the similar name (Struts), migrating from Struts 1 is harder than migrating from WebWork 2. struts.xml struts-config.xml Action or POJO ActionForm Action or POJO Action (singleton) {action}-validation.xml validation.xml Result ActionForward Interceptors RequestProcessor FilterDispatcher ActionServlet Struts 2 Struts 1
  21. 21. WebWork 2 to Struts 2 Pretty much the same, only naming/namespace changes. <s:*/> <ww:*/> struts.xml xwork.xml com.opensymphony.xwork2.* com.opensymphony.xwork.* “ struts” “ webwork” org.apache.struts2.* com.opensymphony.webwork.* Struts 2 WebWork 2
  22. 22. Requirements <ul><li>Servlet API 2.4 </li></ul><ul><li>JSP API 2.0 </li></ul><ul><li>Java 5 (or 1.4 with Retroweaver) </li></ul>
  23. 23. Integration with open source libraries <ul><li>Spring 2.0 </li></ul><ul><li>Velocity </li></ul><ul><li>Freemarker </li></ul><ul><li>JasperReports </li></ul><ul><li>DWR </li></ul><ul><li>Apache Pluto </li></ul><ul><li>dojo </li></ul><ul><li>and many more… </li></ul>
  24. 24. Not discussed <ul><li>Annotation support </li></ul><ul><li>JSF, Ajax support </li></ul><ul><li>Plug-ins </li></ul><ul><li>QuickStart </li></ul><ul><li>Validation </li></ul><ul><li>Value stack </li></ul><ul><li>Wildcard mappings </li></ul><ul><li>Zero configuration </li></ul>
  25. 25. Where to go from here <ul><li>Struts (1 and 2) </li></ul><ul><ul><li> </li></ul></ul><ul><li>AppFuse 2 </li></ul><ul><ul><li> </li></ul></ul>