webwork / docs / wikidocs / Comparison to Struts.html

        <title>WebWork 2 : Comparison to Struts</title>
	    <link rel="stylesheet" href="styles/site.css" type="text/css" />
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8">	    

	    <table class="pagecontent" border="0" cellpadding="0" cellspacing="0" width="100%" bgcolor="#ffffff">
			    <td valign="top" class="pagebody">
				    <div class="pageheader">
					    <span class="pagetitle">
                            WebWork 2 : Comparison to Struts
				    <div class="pagesubheading">
					    This page last changed on Jun 18, 2004 by <font color="#0050B2">plightbo</font>.

				    <p class="paragraph"><h2 style="margin: 4px 0px 4px 0px;" class="heading2"><a name="ComparisontoStruts-FeatureComparison"> Feature Comparison</a></h2></p><table class="wiki-table" cellpadding="0" cellspacing="0" border="0"><tr><th>Feature</th><th>Struts</th><th>WebWork 1.x</th><th>WebWork 2.x</th></tr><tr class="table-odd"><td><b class="strong">Action classes</b></td><td>Struts requires Action classes to extend an Abstract base class. This shows a common problem in Struts of programming to abstract classes instead of interfaces.</td><td>Action classes must implement the webwork.Action Interface. There are other Interfaces which can be implemented for other services, such as storing error messages, getting localized texts, etc. The ActionSupport class implements many of these Interfaces and can act as a base class. WebWork is all written to Interfaces, which allows for plugging in your own implementations.</td><td>An Action must implement the com.opensymphony.xwork.Action Interface, with a series of other Interfaces for other services, like in WebWork 1.x. WebWork2 has its own ActionSupport to implement these Interfaces.</td></tr><tr class="table-even"><td><b class="strong">Threading Model</b></td><td>Struts Actions must be thread-safe because there will only be one instance to handle all requests. This places restrictions on what can be done with Struts Actions as any resources held must be thread-safe or access to them must be synchronized.</td><td>WebWork Actions are instantiated for each request, so there are no thread-safety issues. In practice, Servlet containers generate many throw-away objects per request, and one more Object does not prove to be a problem for performance or garbage collection.</td><td>ditto</td></tr><tr class="table-odd"><td><b class="strong">Servlet Dependency</b></td><td>Struts Actions have dependencies on Servlets because they get the ServletRequest and ServletResponse (not HttpServletRequest and HttpServletResponse, I&#039;ve been told) when they are executed. This tie to Servlets (although not Http*) is a defacto tie to a Servlet container, which is an unneeded dependency. Servlets may be used outside a Web context, but it&#039;s not a good fit for JMS, for instance.</td><td>WebWork Actions are not tied to the web or any container. WebWork actions CAN choose to access the request and response from the ActionContext, but it is not required and should be done only when ABSOLUTELY neccessary to avoid tieing code to the Web.</td><td>ditto</td></tr><tr class="table-even"><td><b class="strong">Testability</b></td><td>Many strategies have sprung up around testing Struts applications, but the major hurdle is the fact that Struts Actions are so tightly tied to the web (receiving a Request and Response object). This often leads people to test Struts Actions inside a container, which is both slow and NOT UNIT TESTING. There is a Junit extension : Struts TestCase (<a href="" title="Visit page outside Confluence">&#104;ttp://</a>)</td><td>WebWork actions can be tested by  instantiating your action, setting the properties, and executing them</td><td>ditto, but the emphasis on Inversion of Control makes testing even simpler, as you can just set a Mock implementation of your services into your Action for testing, instead of having to set up service registries or static singletons</td></tr><tr class="table-odd"><td><b class="strong">FormBeans</b></td><td>Struts requires the use of FormBeans for every form, necessitating either a lot of extra classes or the use of DynaBeans, which are really just a workaround for the limitation of requiring FormBeans</td><td>WebWork 1.x allows you to have all of your properties directly accessible on your Action as regular Javabeans properties, including rich Object types which can have their own properties which can be accessed from the web page. WebWork also allows the FormBean pattern, as discussed in &quot;<a href="" title="Populate Form Bean and access its value">WW1:Populate Form Bean and access its value</a>&quot;</td><td>WebWork 2 allows the same features as WebWork 1, but adds ModelDriven Actions, which allow you to have a rich Object type or domain object as your form bean, with its properties directly accessible to the web page, rather than accessing them as sub-properties of a property of the Action.</td></tr><tr class="table-even"><td><b class="strong">Expression Language</b></td><td>Struts 1.1 integrates with JSTL, so it uses the JSTL EL. This EL has basic object graph traversal, but relatively weak collection and indexed property support.</td><td>WebWork 1.x has its own Expression language which is built for accessing the ValueStack. Collection and indexed property support are basic but good. WebWork can also be made to work directly with JSTL using the Filter described in <a href="" title="Using JSTL seamlessly with WebWork">WW1:Using JSTL seamlessly with WebWork</a></td><td>WebWork 2 uses <a href="" title="Ognl">XW:Ognl</a> which is a VERY powerful expression language, with additions for accessing the value stack. Ognl supports very powerful collection and indexed property support. Ognl also supports powerful features like projections (calling the same method on each member of a collection and building a new collection of the results), selections (filtering a collection with a selector expression to return a subset), list construction, and lambda expressions (simple functions which can be reused). Ognl also allows access to static methods, static fields, and constructors of classes. WebWork2 may also use JSTL as mentioned in <a href="" title="Using JSTL seamlessly with WebWork">WW1:Using JSTL seamlessly with WebWork</a></td></tr><tr class="table-odd"><td><b class="strong">Binding values into views</b></td><td>Struts uses the standard JSP mechanism for binding objects into the page context for access, which tightly couples your view to the form beans being rendered</td><td>WebWork sets up a ValueStack which the WebWork taglibs access to dynamically find values very flexibly without tightly coupling your view to the types it is rendering. This allows you to reuse views across a range of types which have the same properties.</td><td>ditto</td></tr><tr class="table-even"><td><b class="strong">Type Conversion</b></td><td>Struts FormBeans properties are usually all Strings. Struts uses Commons-Beanutils for type conversion. Converters are per-class, and not configurable per instance. Getting a meaningful type conversion error out and displaying it to the user can be difficult.</td><td>WebWork 1.x uses PropertyEditors for type conversion. PropertyEditors are per type and not settable per Action, but field error messages are added to the field error map in the Action to be automatically displayed to the user with the field.</td><td>WebWork2 uses Ognl for type conversion with added converters provided for all basic types. Type converters default to these converters, but type conversion can be specified per field per class. Type conversion errors also have a default error message but can be set per field per class using the localization mechanism in WW2 and will be set into the field error messages of the Action.</td></tr><tr class="table-odd"><td><b class="strong">Modular Before &amp; After Processing</b></td><td>Class hierarchies of base Actions must be built up to do processing before and after delegating to the Action classes, which can lead deep class hierarchies and limitations due to the inability to have multiple inheritance <em class="emphasis"><a href="#ComparisontoStruts-1" title="1 on Comparison to Struts">WW:Comparison to Struts#1</a></em></td><td>Class hierarchies</td><td>WebWork 2 allows you to modularize before and after processing in Interceptors. Interceptors can be applied dynamically via the configuration without any coupling between the Action classes and the Interceptors.</td></tr><tr class="table-even"><td><b class="strong">Validation</b></td><td>Struts calls validate() on the FormBean. Struts users often use Commons Validation for validation. I don&#039;t know a lot about this, so I&#039;ll put some questions here: <br clear="all" />&#160;<br clear="all" /> Because FormBean properties are usually Strings, some types of validations must either be duplicated (checking type conversion) or cannot be done? <br clear="all" />&#160;<br clear="all" /> Can Commons Validation have different validation contexts for the same class? (I&#039;ve been told yes, so that&#039;s a good thing) <br clear="all" />&#160;<br clear="all" /> Can Commons Validation chain to validations on sub-objects, using the validations defined for that object properties class?</td><td>WebWork1.x calls the validate() method on Actions, which can either do programmatic validations or call an outside validation framework (this is apparently the same as Struts)</td><td>WebWork2 can use the validate() method of WebWork and Struts and / or use the <a href="" title="Validation Framework">XW:Validation Framework</a>, which is activated using an XWork Interceptor. The Xwork Validation Framework allows you to define validations in an XML format with default validations for a class and custom validations added for different validation contexts. The Xwork Validation Framework is enabled via an Interceptor and is therefore completely decoupled from your Action class. The Xwork Validation Framework also allows you to chain the validation process down into sub-properties using the VisitorFieldValidator which will use the validations defined for the properties class type and the validation context.</td></tr><tr class="table-odd"><td><b class="strong">Control Of Action Execution</b></td><td>As far as I know Struts sets up the Action object for you, and you have very little control over the order of operations. To change them I think <img class="rendericon" src="./icons/emoticons/help_16.gif" height="16" width="16" align="absmiddle" alt="" border="0"/> you need to write your own Servlet to handle dispatching as you want</td><td>The ActionFactory chain controls the order in which an Action is constructed and initialised, but this requires writing a class</td><td>The interceptor stacks in WebWork 2 are hugely powerful in this regard. All aspects of Action setup have been moved into Interceptor implementations (ie setting paramters from the web, validation etc), so you can control on a per action basis the order in which they are performed. For example you might want your IOC framework to setup the action before the parameters are set from the request or vice versa - you can thusly control this on a per package or per action basis with interceptor stacks.</td></tr></table><p class="paragraph"><h2 class="heading2"><a name="ComparisontoStruts-References"> References</a></h2></p><ul class="star">
<li> <a href="" title="Visit page outside Confluence">&#104;ttp://</a> - compares Struts development to WebWork 1.x development from the point of view of a Stuts developer who switched to WebWork</li>
<li> <a href="" title="Visit page outside Confluence">&#104;ttp://</a> - Kind of the first draft of this comparison</li>
<h2 class="heading2"><a name="ComparisontoStruts-Footnotes"> Footnotes</a></h2>
<li> <a name="ComparisontoStruts-1"></a> Some Stuts users have built the beginnings of an Interceptor framework for Struts (<a href="" title="Visit page outside Confluence">&#104;ttp://</a>). It currently has some serious limitations (no &quot;around&quot; processing, just before and after) and is not part of the main Struts project</li>

	    <table border="0" cellpadding="0" cellspacing="0" width="100%">
				<td height="12" background="border/border_bottom.gif"><img src="border/spacer.gif" width="1" height="1" border="0"/></td>
			    <td align="center"><font color="grey">Document generated by Confluence on Oct 15, 2004 02:03</font></td>