25
Introducing Struts Introducing Struts 2 2 JaMU 07.02 – February 24, 2007 JaMU 07.02 – February 24, 2007 Thomas Wiradikusuma Thomas Wiradikusuma ([email protected]) ([email protected]) Java User Group Indonesia Java User Group Indonesia

Introducing Struts 2

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Introducing Struts 2

Introducing Struts Introducing Struts 22JaMU 07.02 – February 24, 2007JaMU 07.02 – February 24, 2007

Thomas WiradikusumaThomas Wiradikusuma([email protected])([email protected])

Java User Group IndonesiaJava User Group Indonesia

Page 2: Introducing Struts 2

Struts 2 definedStruts 2 defined

An elegant, extensible framework for An elegant, extensible framework for building enterprise-ready Java web building enterprise-ready Java web applicationsapplications

An MVC web frameworkAn MVC web framework Action-basedAction-based Successor of the famous Struts Successor of the famous Struts

framework (and WebWork 2, technically framework (and WebWork 2, technically speaking)speaking)

Page 3: Introducing Struts 2

Sweet spotsSweet spots Simple architectureSimple architecture Interceptors, Actions, Results from pluggable ObjectFactoryInterceptors, Actions, Results from pluggable ObjectFactory Controller-based or page-based navigationController-based or page-based navigation Support for POJO, Annotation, JSFSupport for POJO, Annotation, JSF Cool, customizable tag library with OGNL supportCool, customizable tag library with OGNL support Value stackValue stack Spring as default inversion of control containerSpring as default inversion of control container QuickStartQuickStart Built-in Ajax supportBuilt-in Ajax support Many convention-over-configuration and sensible defaultsMany convention-over-configuration and sensible defaults Easier to test (out of container)Easier to test (out of container) Brings the best of Struts 1 and WebWork 2, including their Brings the best of Struts 1 and WebWork 2, including their

fanatic followers ;-)fanatic followers ;-)

Page 4: Introducing Struts 2

ArchitectureArchitecture

Request arrivesRequest arrives FilterDispatcher finds FilterDispatcher finds

appropriate Actionappropriate Action Interceptors get Interceptors get

appliedapplied Method in Action Method in Action

executes (usually executes (usually doing “core” stuff)doing “core” stuff)

Result renders outputResult renders output

Page 5: Introducing Struts 2

Architecture, cont’dArchitecture, cont’d

Page 6: Introducing Struts 2

ActionMapperActionMapper

Provide a mapping between HTTP requests Provide a mapping between HTTP requests and action invocation requests and vice-versaand action invocation requests and vice-versa

Default implementation uses standard *.[ext] Default implementation uses standard *.[ext] (usually "action") pattern. Extension is defined (usually "action") pattern. Extension is defined in Struts configuration key in Struts configuration key struts.action.exection. Prefixes:struts.action.exection. Prefixes: method: method: <a:submit name=“method:bar” value=“Bar”/><a:submit name=“method:bar” value=“Bar”/> action: action: <a:submit name=“action:foo” value=“Foo”/><a:submit name=“action:foo” value=“Foo”/>

redirect: redirect: <a:submit name=“redirect:http://www.google.com” <a:submit name=“redirect:http://www.google.com” value=“Google”/>value=“Google”/>

redirect-action: redirect-action: <a:submit name=“redirect-action:foo” value=“Foo”/><a:submit name=“redirect-action:foo” value=“Foo”/>

Page 7: Introducing Struts 2

InterceptorsInterceptors<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 thisWillRunNextInterceptorthisWillRunFirstInterceptor

Page 8: Introducing Struts 2

Interceptors, cont’dInterceptors, cont’dInterceptorInterceptor NameName DescriptionDescription

CheckboxCheckbox checkbox checkbox Adds automatic checkbox handling code that detect an unchecked Adds automatic checkbox handling code that detect an unchecked checkbox and add it as a parameter with a default (usually 'false') checkbox and add it as a parameter with a default (usually 'false') value. Uses a specially named hidden field to detect unsubmitted value. Uses a specially named hidden field to detect unsubmitted checkboxes. checkboxes.

Execute and Execute and WaitWait

execAndWait execAndWait Executes the Action in the background and then sends the user off to Executes the Action in the background and then sends the user off to an intermediate waiting page. an intermediate waiting page.

ParametersParameters params params Sets the request parameters onto the Action. Sets the request parameters onto the Action.

PreparePrepare prepare prepare If the Action implements Preparable, calls its prepare method. If the Action implements Preparable, calls its prepare method.

RolesRoles roles roles Action will only be executed if the user has the correct JAAS role.Action will only be executed if the user has the correct JAAS role.

TokenToken token token Checks for valid token presence in Action, prevents duplicate form Checks for valid token presence in Action, prevents duplicate form submission.submission.

And many more…And many more…

Page 9: Introducing Struts 2

ActionsActions

All actions may implement this interface, which All actions may implement this interface, which exposes the execute() method. However, as of exposes the execute() method. However, as of XWork 1.1, this is not required and is only here XWork 1.1, this is not required and is only here to assist users. You are free to create POJOs to assist users. You are free to create POJOs that honor the same contract defined by this that honor the same contract defined by this interface without actually implementing the interface without actually implementing the interface.interface.

ActionSupport class provides a default ActionSupport class provides a default implementation for the most common actions. implementation for the most common actions.

Page 10: Introducing Struts 2

ResultsResults

ResultResult UsageUsage

DispatcherDispatcher Used for web resource integration, including Used for web resource integration, including JSP integrationJSP integration

RedirectRedirect Used to redirect to another URL (web resource) Used to redirect to another URL (web resource)

Redirect ActionRedirect Action Used to redirect to another action mappingUsed to redirect to another action mapping

StreamStream Used to stream an InputStream back to the Used to stream an InputStream back to the browser (usually for file downloads)browser (usually for file downloads)

ChainChain Used for Action ChainingUsed for Action Chaining

VelocityVelocity Used for Velocity integrationUsed for Velocity integration

And many more…And many more…

Page 11: Introducing Struts 2

Type conversionType conversion

Everything is String in HTTPEverything is String in HTTP Built-in: boolean, char, numeric types, dates, arrays, collectionsBuilt-in: boolean, char, numeric types, dates, arrays, collections ClassName-conversion.properties:ClassName-conversion.properties:

foo = package.FooConverterfoo = package.FooConverter Globally in struts-conversion.properties in the root of your class Globally in struts-conversion.properties in the root of your class

path (typically WEB-INF/classes) path (typically WEB-INF/classes) package.Foo = package.FooConverterpackage.Foo = package.FooConverter

Extend StrutsTypeConverter to simplify creating a converterExtend StrutsTypeConverter to simplify creating a converter Throw TypeConversionException when conversion exception Throw TypeConversionException when conversion exception

happens. Information will be displayed as specified in Struts happens. Information will be displayed as specified in Struts configuration struts.default.invalid.fieldvalueconfiguration struts.default.invalid.fieldvalue

Page 12: Introducing Struts 2

LocalizationLocalization

Resource bundles are searched in the following order:Resource bundles are searched in the following order: ActionClass.properties ActionClass.properties BaseClass.properties (all the way to Object.properties) BaseClass.properties (all the way to Object.properties) Interface.properties (every interface and sub-interface) Interface.properties (every interface and sub-interface) ModelDriven's model (if implements ModelDriven), for the model object repeat ModelDriven's model (if implements ModelDriven), for the model object repeat

from 1 from 1 package.properties (of the directory where class is located and every parent package.properties (of the directory where class is located and every parent

directory all the way to the root directory) directory all the way to the root directory) search up the i18n message key hierarchy itself search up the i18n message key hierarchy itself global resource properties global resource properties

Accessing key:Accessing key: getText: <s:property value="getText('some.key')" /> getText: <s:property value="getText('some.key')" /> text tag: <s:text name="some.key" /> text tag: <s:text name="some.key" /> I18n tag to push an arbitrary resource bundle on to the value stack:I18n tag to push an arbitrary resource bundle on to the value stack:

<s:i18n name="some.package.bundle" ><s:i18n name="some.package.bundle" > <s:text name="some.key" /><s:text name="some.key" />

</s:i18n></s:i18n>

Page 13: Introducing Struts 2

Configuration filesConfiguration filesFile File OptionalOptional

??LocationLocation Purpose Purpose

web.xmlweb.xml NoNo /WEB-INF/ /WEB-INF/ Deployment descriptorDeployment descriptor

struts.xmlstruts.xml YesYes /WEB-INF/classes/ /WEB-INF/classes/ Main configuration, contains result/view Main configuration, contains result/view types, action mappings, interceptors, types, action mappings, interceptors, and so forthand so forth

struts.propertiesstruts.properties YesYes /WEB-INF/classes/ /WEB-INF/classes/ Framework propertiesFramework properties

struts-default.xmlstruts-default.xml YesYes /WEB-INF/lib/struts2-core.jar /WEB-INF/lib/struts2-core.jar Default configuration provided by StrutsDefault configuration provided by Struts

struts-default.vmstruts-default.vm YesYes /WEB-INF/classes/ /WEB-INF/classes/ Default macros referenced by Default macros referenced by velocity.propertiesvelocity.properties

struts-plugin.xml struts-plugin.xml YesYes Root of a plug-in JAR Root of a plug-in JAR Optional configuration files for plug-ins Optional configuration files for plug-ins in the same format as struts.xml.in the same format as struts.xml.

velocity.propertiesvelocity.properties YesYes /WEB-INF/classes/ /WEB-INF/classes/ Override the default Velocity Override the default Velocity configurationconfiguration

Page 14: Introducing Struts 2

TaglibTaglib<% 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>...

Without 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>

With taglib (JSP):

Page 15: Introducing Struts 2

Taglib, cont’dTaglib, cont’d

<%@ taglib prefix="s" uri="/struts-tags" %> <%@ taglib prefix="s" uri="/struts-tags" %> Tags category:Tags category:

ControlControl DataData UI (form and non-form)UI (form and non-form) AjaxAjax

Page 16: Introducing Struts 2

Servlet/JSP scoped objectServlet/JSP scoped object

ApplicationApplication<s:property value="%{#application.foo}" /><s:property value="%{#application.foo}" />

SessionSession<s:property value="%{#session.foo}" /><s:property value="%{#session.foo}" />

RequestRequest<s:property value="%{#request.foo}" /><s:property value="%{#request.foo}" />

ParameterParameter<s:property value="%{#parameter.foo}" /><s:property value="%{#parameter.foo}" />

ContextContext<s:property value="%{#foo}" /><s:property value="%{#foo}" />

Page 17: Introducing Struts 2

OGNLOGNL

Object Graph Navigation LanguageObject Graph Navigation Language More powerful than JSTL ELMore powerful than JSTL EL

Page 18: Introducing Struts 2

Spring integrationSpring integration

By default, the framework will at least try By default, the framework will at least try to use Spring to create all its objects. If to use Spring to create all its objects. If the object cannot be created by Spring, the object cannot be created by Spring, then the framework will create the object then the framework will create the object itself. itself.

Page 19: Introducing Struts 2

TestingTesting

Direct Action invocationDirect Action invocation Out of container testingOut of container testing Testing Interceptors and/or ResultsTesting Interceptors and/or Results

Page 20: Introducing Struts 2

Struts 1 to Struts 2Struts 1 to Struts 2

Struts 1Struts 1 Struts 2Struts 2struts-config.xmlstruts-config.xml struts.xmlstruts.xml

ActionServletActionServlet FilterDispatcherFilterDispatcher

Action (singleton)Action (singleton) Action or POJOAction or POJO

ActionFormActionForm Action or POJOAction or POJO

RequestProcessorRequestProcessor InterceptorsInterceptors

ActionForwardActionForward ResultResult

validation.xmlvalidation.xml {action}-validation.xml{action}-validation.xml

Despite the similar name (Struts), migrating from Struts 1 is harder than migrating from WebWork 2.

Page 21: Introducing Struts 2

WebWork 2 to Struts 2WebWork 2 to Struts 2

WebWork 2WebWork 2 Struts 2Struts 2xwork.xmlxwork.xml struts.xmlstruts.xml

com.opensymphony.webwork.*com.opensymphony.webwork.* org.apache.struts2.*org.apache.struts2.*

com.opensymphony.xwork.*com.opensymphony.xwork.* com.opensymphony.xwork2.*com.opensymphony.xwork2.*

webwork.propertieswebwork.properties struts.propertiesstruts.properties

<ww:*/><ww:*/> <s:*/><s:*/>

““webwork”webwork” ““struts”struts”

Pretty much the same, only naming/namespace changes.

Page 22: Introducing Struts 2

RequirementsRequirements

Servlet API 2.4Servlet API 2.4 JSP API 2.0JSP API 2.0 Java 5 (or 1.4 with Retroweaver)Java 5 (or 1.4 with Retroweaver)

Page 23: Introducing Struts 2

Integration with open source Integration with open source librarieslibraries

Spring 2.0Spring 2.0 VelocityVelocity FreemarkerFreemarker JasperReportsJasperReports DWRDWR Apache PlutoApache Pluto dojodojo and many more…and many more…

Page 24: Introducing Struts 2

Not discussedNot discussed

Annotation supportAnnotation support JSF, Ajax supportJSF, Ajax support Plug-insPlug-ins QuickStartQuickStart ValidationValidation Value stackValue stack Wildcard mappingsWildcard mappings Zero configurationZero configuration

Page 25: Introducing Struts 2

Where to go from hereWhere to go from here

Struts (1 and 2)Struts (1 and 2)http://struts.apache.orghttp://struts.apache.org

AppFuse 2AppFuse 2http://www.appfuse.orghttp://www.appfuse.org