Integration with WebWork is possible using the XWork SpringObjectFactory. This object factory is part of the XWork-optional package and resolves any dependencies for your actions, before WebWork takes over control and executes your actions.
You can initialize a SpringObjectFactory in one of two ways: by using a Servlet listener or by using an initializing bean defined in your Spring application context. The former relies on the fact that the Spring application context(s) are already loaded by, for example, a ContextLoaderListener, as explained in the next section.
Enabling the SpringObjectFactory using a listener is quite simple:
<web-app> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> com.opensymphony.spring.xwork.SpringObjectFactoryListener </listener-class> </listener> </web-app>
The first listener loads the Spring application context(s), just as we've seen in other parts of this book. The second listener initializes the XWork object factory. XWork manages all WebWork actions and XWork creates these actions using the specified object factory. The SpringObjectFactory inspects the Spring application context to look for actions. After the actions are created, XWork passes them on to WebWork.
Note that we've defined the Spring listener before the XWork listener. This is important because the XWork listener relies on Spring context(s) being loaded first! The order in which you define listeners in web.xml is also the order in which they are executed, so in this example, the XWork listener is executed after the Spring one.
The second option we have to configure in the SpringObjectFactory is to define it in the Spring application context. Which option to use is essentially a matter of taste. A Spring-managed SpringObject Factory has to be defined as follows:
<bean id="springObjectFactory" class="com.opensymphony.xwork.spring.SpringObjectFactory" init-method="initObjectFactory"/>
If you get all this set up, defining WebWork actions is a matter of including the action reference in your xwork.xml file and defining the actual action itself in the Spring context.
# xwork.xml <action name="showOrder" class="showOrderAction"> <result name="success">showOrder.jsp</result> </action> # applicationContext.xml <bean name="showOrderAction" class="org.springframework.prospring.ch14.ShowOrderAction4" singleton="false"> <property name="orderService"> <ref bean="orderService"/> </property> </bean>
Note that the name of the XWork defined action matches the bean name of the action defined in Spring and is no longer reflecting the action's class name. Furthermore, you have to make sure the Spring-defined action is declared to a prototype (singleton="false") because WebWork demands all actions to be non-singletons.