Slideshare.net (beta)

 
Post to TwitterPost to Twitter
Post: 
Myspace Hi5 Friendster Xanga LiveJournal Facebook Blogger Tagged Typepad Freewebs BlackPlanet gigya icons

All comments

Add a comment on Slide 1

If you have a SlideShare account, login to comment; else you can comment as a guest


Showing 1-50 of 4 (more)

Introducing Struts 2

From wiradikusuma, 8 months ago

Introductory presentation to action-based Java MVC framework Strut

7793 views  |  0 comments  |  3 favorites  |  377 downloads  |  2 embeds (Stats)
 

Categories

Add Category
 
 

Groups / Events

 
Embed
options

More Info

This slideshow is Public
Total Views: 7793
on Slideshare: 7786
from embeds: 7

Slideshow transcript

Slide 1: Introducing Struts 2 JaMU 07.02 – February 24, 2007 Thomas Wiradikusuma (thomas@wiradikusuma.com) Java User Group Indonesia

Slide 2: Struts 2 defined  An elegant, extensible framework for building enterprise-ready Java web applications  An MVC web framework  Action-based  Successor of the famous Struts framework (and WebWork 2, technically speaking)

Slide 3: Sweet spots  Simple architecture  Interceptors, Actions, Results from pluggable ObjectFactory  Controller-based or page-based navigation  Support for POJO, Annotation, JSF  Cool, customizable tag library with OGNL support  Value stack  Spring as default inversion of control container  QuickStart  Built-in Ajax support  Many convention-over-configuration and sensible defaults  Easier to test (out of container)  Brings the best of Struts 1 and WebWork 2, including their fanatic followers ;-)

Slide 4: Architecture  Request arrives  FilterDispatcher finds appropriate Action  Interceptors get applied  Method in Action executes (usually doing “core” stuff)  Result renders output

Slide 5: Architecture, cont’d

Slide 6: ActionMapper  Provide a mapping between HTTP requests and action invocation requests and vice-versa  Default implementation uses standard *.[ext] (usually \"action\") pattern. Extension is defined in Struts configuration key struts.action.exection. Prefixes:  method: <a:submit name=“method:bar” value=“Bar”/>  action: <a:submit name=“action:foo” value=“Foo”/>  redirect: <a:submit name=“redirect:http://www.google.com” value=“Google”/>  redirect-action: <a:submit name=“redirect-action:foo” value=“Foo”/>

Slide 7: Interceptors <interceptor-stack name=\"xaStack\"> <interceptor-ref name=\"thisWillRunFirstInterceptor\"/> <interceptor-ref name=\"thisWillRunNextInterceptor\"/> <interceptor-ref name=\"followedByThisInterceptor\"/> <interceptor-ref name=\"thisWillRunLastInterceptor\"/> </interceptor-stack> thisWillRunFirstInterceptor thisWillRunNextInterceptor followedByThisInterceptor thisWillRunLastInterceptor MyAction1 MyAction2 (chain) MyPreResultListener MyResult (result) thisWillRunLastInterceptor followedByThisInterceptor thisWillRunNextInterceptor thisWillRunFirstInterceptor

Slide 8: Interceptors, cont’d Interceptor Name Description Checkbox checkbox 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. Execute and Wait execAndWait Executes the Action in the background and then sends the user off to an intermediate waiting page. Parameters params Sets the request parameters onto the Action. Prepare prepare If the Action implements Preparable, calls its prepare method. Roles roles Action will only be executed if the user has the correct JAAS role. Token token Checks for valid token presence in Action, prevents duplicate form submission. And many more…

Slide 9: Actions  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.  ActionSupport class provides a default implementation for the most common actions.

Slide 10: Results Result Usage Dispatcher Used for web resource integration, including JSP integration Redirect Used to redirect to another URL (web resource) Redirect Action Used to redirect to another action mapping Stream Used to stream an InputStream back to the browser (usually for file downloads) Chain Used for Action Chaining Velocity Used for Velocity integration And many more…

Slide 11: Type conversion  Everything is String in HTTP  Built-in: boolean, char, numeric types, dates, arrays, collections  ClassName-conversion.properties:  foo = package.FooConverter  Globally in struts-conversion.properties in the root of your class path (typically WEB-INF/classes)  package.Foo = package.FooConverter  Extend StrutsTypeConverter to simplify creating a converter  Throw TypeConversionException when conversion exception happens. Information will be displayed as specified in Struts configuration struts.default.invalid.fieldvalue

Slide 12: Localization  Resource bundles are searched in the following order:  ActionClass.properties  BaseClass.properties (all the way to Object.properties)  Interface.properties (every interface and sub-interface)  ModelDriven's model (if implements ModelDriven), for the model object repeat from 1  package.properties (of the directory where class is located and every parent directory all the way to the root directory)  search up the i18n message key hierarchy itself  global resource properties  Accessing key:  getText: <s:property value=\"getText('some.key')\" />  text tag: <s:text name=\"some.key\" />  I18n tag to push an arbitrary resource bundle on to the value stack: <s:i18n name=\"some.package.bundle\" > <s:text name=\"some.key\" /> </s:i18n>

Slide 13: Configuration files File Optional? Location Purpose web.xml No /WEB-INF/ Deployment descriptor struts.xml Yes /WEB-INF/classes/ Main configuration, contains result/view types, action mappings, interceptors, and so forth struts.properties Yes /WEB-INF/classes/ Framework properties struts-default.xml Yes /WEB-INF/lib/struts2-core.jar Default configuration provided by Struts struts-default.vm Yes /WEB-INF/classes/ Default macros referenced by velocity.properties struts-plugin.xml Yes Root of a plug-in JAR Optional configuration files for plug-ins in the same format as struts.xml. velocity.properties Yes /WEB-INF/classes/ Override the default Velocity configuration

Slide 14: Taglib Without taglib (JSP): <% User user = ActionContext.getContext() %> <form action=\"Profile_update.action\" method=\"post\"> <table> <tr> <td> align=\"right\"><label>First name:</label></td> <td><input type=\"text\" name=\"user.firstname\" value=\"<%=user.getFirstname() %> /></td> </tr> <tr> <td> <input type=\"radio\" name=\"user.gender\" value=\"0\" id=\"user.gender0\" <% if (user.getGender()==0) { %> checked=\"checked\" %> } %> /> <label for=\"user.gender0\">Female</label> ... With taglib (JSP): <s:actionerror/> <s:form action=\"Profile_update\" validate=\"true\"> <s:textfield label=\"Username\" name=\"username\"/> <s:password label=\"Password\" name=\"password\"/> <s:password label=\"(Repeat) Password\" name=\"password2\"/> <s:textfield label=\"Full Name\" name=\"fullName\"/> <s:textfield label=\"From Address\" name=\"fromAddress\"/> <s:textfield label=\"Reply To Address\" name=\"replyToAddress\"/> <s:submit value=\"Save\" name=\"Save\"/> <s:submit action=\"Register_cancel\" value=\"Cancel\" name=\"Cancel\" onclick=\"form.onsubmit=null\"/> </s:form>

Slide 15: Taglib, cont’d  <%@ taglib prefix=\"s\" uri=\"/struts-tags\" %>  Tags category:  Control  Data  UI (form and non-form)  Ajax

Slide 16: Servlet/JSP scoped object  Application <s:property value=\"%{#application.foo}\" />  Session <s:property value=\"%{#session.foo}\" />  Request <s:property value=\"%{#request.foo}\" />  Parameter <s:property value=\"%{#parameter.foo}\" />  Context <s:property value=\"%{#foo}\" />

Slide 17: OGNL  Object Graph Navigation Language  More powerful than JSTL EL

Slide 18: Spring integration  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.

Slide 19: Testing  Direct Action invocation  Out of container testing  Testing Interceptors and/or Results

Slide 20: Struts 1 to Struts 2 Struts 1 Struts 2 struts-config.xml struts.xml ActionServlet FilterDispatcher Action (singleton) Action or POJO ActionForm Action or POJO RequestProcessor Interceptors ActionForward Result validation.xml {action}-validation.xml Despite the similar name (Struts), migrating from Struts 1 is harder than migrating from WebWork 2.

Slide 21: WebWork 2 to Struts 2 Pretty much the same, only naming/namespace changes. WebWork 2 Struts 2 xwork.xml struts.xml com.opensymphony.webwork.* org.apache.struts2.* com.opensymphony.xwork.* com.opensymphony.xwork2.* webwork.properties struts.properties <ww:*/> <s:*/> “webwork” “struts”

Slide 22: Requirements  Servlet API 2.4  JSP API 2.0  Java 5 (or 1.4 with Retroweaver)

Slide 23: Integration with open source libraries  Spring 2.0  Velocity  Freemarker  JasperReports  DWR  Apache Pluto  dojo  and many more…

Slide 24: Not discussed  Annotation support  JSF, Ajax support  Plug-ins  QuickStart  Validation  Value stack  Wildcard mappings  Zero configuration

Slide 25: Where to go from here  Struts (1 and 2) http://struts.apache.org  AppFuse 2 http://www.appfuse.org