Spring Webwork2 Integration

Trying to work out the best way to integrate Spring and Webwork2? One fellow discovered multiple ways in which you can use Spring-managed beans from webwork actions.

I'm trying to work out the best way to integrate Spring and Webwork2.

Specifically I'm looking at the multiple ways in which you can use Spring-managed beans from webwork actions. The best document I could find on it is here, which got me started but also left me a little confused.

As far as I can tell, there are two options.

One method is to get a hold of the spring context from within your action and lookup spring beans by name. I'll call this method A, which looks something like this:

 ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext( ServletActionContext.getServletContext()); Object springBean = ctx.getBean("Foo");

The other way, method B, is to configure which spring beans you want to use in your action definitions in xwork.xml. Using external-ref you can set spring bean references on your action just like you would set any bean property using <param>. The action definition looks something like this:

 <action name="HelloAction" class="com.adigio.actions.HelloAction"> <external-ref name="actionDAO">MainDAO</external-ref> <interceptor-ref name="defaultStack"/> </action>

The action java looks something like this:

 public class HelloAction extends ActionSupport { private Object actionDAO; public void setActionDAO(Object dao) { ... } }

I see method A as a bit simpler than B. With this method you are simple getting a handle on your spring context from within an action and looking up your bean. This is similar to how you would lookup a spring-bean from any class except that if you're running in a servlet container (and if you're using actions I assume you are) then you get the spring application context via the servletContext (as in the example above).

For this to work you'll only need to add the ContextLoaderListener to your web.xml so you can get the spring application context from the servlet context. Add this to your web.xml:

 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>

then lookup your spring beans in your actions as in the method A action example above. You would need this regardless of which MVC framework you're using.

You'll only need spring.jar on you classpath for this method.

With method B you are pre-configuring everything in xwork.xml. Maybe this is better than figuring out which spring beans you want to use at runtime. This certainly makes the dependencies more clear. Your mileage may vary.

To get method B to work you'll need to do the following, as Pat Lightbody explained earlier in this post:

1. add the ContextLoaderListener to web.xml as in method A

2. also add ResolverSetupServletContextListener in web.xml like this:

 <listener> <listener-class>com.atlassian.xwork.ext.ResolverSetupServletContextListener</listener-class> </listener>

3. in webwork-default.xml add this to your list of interceptors:

 <interceptor name="reference-resolver" class="com.opensymphony.xwork.interceptor.ExternalReferencesInterceptor"/>

then make sure to add this interceptor to the list of interceptors you're using, perhaps like this:

 <interceptor-stack name="defaultStack"> <interceptor-ref name="reference-resolver"/> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="static-params"/> <interceptor-ref name="params"/> </interceptor-stack>

4. in xwork.xml your package definition needs to look like this:

 <package name="default" extends="webwork-default" externalReferenceResolver="com.atlassian.xwork.ext.SpringServletContextReferenceResolver">

On your classpath, make sure you have:

 webwork2-spring.jar - for ResolverSetupServletContextListener.class webwork2-spring.jar - for SpringServletContextReferenceResolver You'll find this jar here. I can't find SpringServletContextReferenceResolver in either the latest release of spring, xwork, or webwork. I haven't checked CVS head. xwork-1.0.1.jar - for ExternalReferencesInterceptor

So far I'm using method A and liking it just fine, and I don't like going through the extra setup.

About the author

Christian Parker christian@adigio.com
Blog: http://adigio.com/blogs/christian/

Christian Parker recently left his Senior Software Engineering position in the interactive TV world to co found Adigio Inc. (http://adigio.com). Christian has 10 years of software development experience and is currently active in the Java community. He is enjoying hacking away on Groovy, Spring, WebWork2, and other technology.



Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: