dev-blog

Tuesday, February 01, 2005

How to access the Spring Business Layer from Struts Web Layer

As usual with software there is not one right way to do things (Especially with open source software).
One question is still "How to access the Spring Business Layer from the Struts Web Layer".

I currently can see the following options:

Option 1: Hand code lookups by name



ServletContext context = request.getSession().getServletContext();
BeanFactory factory = WebApplicationContextUtils.
getRequiredWebApplicationContext(context);
AccountService accountService = (

AccountService) factory.getBean("accountService");
//...
//use the accountService



- This allows only access to the root WebApplicationContext loaded by the ContextLoaderListener

Option 2: Extend Spring's ActionSupport classes instead of Struts Action classes
Spring offers 4 extensions for Struts Action classes just to make the above mentioned access more convenient:
- Spring ActionSupport extends Struts Action
- Spring DispatchActionSupport extends Struts DispatchAction
- Spring LookupDispatchActionSupport extends Struts LookupDispatchAction
- Spring MappingDispatchActionSupport extends Struts MappingDispatchAction



BeanFactory factory = getWebApplicationContext();
AccountService accountService =
(AccountService) factory.getBean("accountService");
//...
//use the accountService



+ Allows to define individual WebApplicationContext per ActionServlet (do we need that?) and falls back to the root WebApplicationContext

Option 3: Use Spring managed Actions by using DelegetingActionProxy
Spring offers the DelegatingActionProxy
This allows to delegate the Action creation to Spring and allows to wire up the Actions with the business components directly by using Spring configuration

In Struts.xml:


<action path="/doit"
type="org.springframework.web.struts.DelegatingActionProxy">



In the Spring context:


<bean name="/doit" class="my.action">
<property name="...">...</property>
</bean>



- Some generation tools do want to generate "Origanl Actions" not the Proxy Action.
+ Any request processor can be used with this


Option 4: Use Spring managed Actions by using DelegetingRequestProcessor
Similar to Option 3 but not using delegating Actions but using a Request processor.

In Struts.xml:


<action path="/doit" type="my.Action">



In the Spring context:


<bean name="/doit" class="my.Action">
<property name="...">...</property>
</bean>



- only one RequestProcessor is allowed -> there is "out-of-the-box" DelegatingRequestProcessor and a TilesDelegatingRequestProcessor in Spring


0 Comments:

Post a Comment

<< Home