You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

There are a number of ways to integrate WebWork with Spring.

Use SpringObjectFactory

The xwork-optional package from dev.java.net contains a module xwork-spring that contains all the necessary code to use Spring in WebWork. It contains primarily a SpringObjectFactory to wire up the dependencies for an Action before passing it to WebWork. Each action should be configured within a Spring application context as a prototype (because WebWork assumes a new instance of a class for every action invocation). Specify something like this in applicationContext.xml:

<bean name="some-action" class="fully.qualified.class.name" singleton="false">
    <property name="someProperty"><ref bean="someOtherBean"/></property>
</bean>

and in xwork.xml:

<action name="myAction" class="some-action">
    <result name="success">view.jsp</result>
</action>

Notice that the WebWork Action's class name some-action is the bean name defined in the Spring application context.

Another advantage of the SpringObjectFactory approach is that it can also be used to load interceptors using the same sort of logic. If the interceptor is stateless, then it's possible to create the interceptor as a singelton instance, but otherwise it's best to create it as a Spring prototype.
In order to be used, the default ObjectFactory that WebWork uses should be replaced with an instance of the SpringObjectFactory. The xwork-optional package ships with a ContextListener that does this, assuming that the Spring application context has already been configured. Add the following to web.xml:

<!-- This needs to be after Spring ContextLoaderListener -->
<listener>
  <listener-class>com.opensymphony.xwork.spring.SpringObjectFactoryListener</listener-class>
</listener>

Note: this is actually an XWork configuration but for simplicity, I just mention WebWork.

  • No labels