Spring with Webwork1

Following is some of the experiences learnt from using Spring with Webwork1 (http://www.opensymphony.com/webwork).

I know many developers are trying out Webwork2 and XWork (http://wiki.opensymphony.com/space/WebWork2), and it's generally agreed that Webwork2/XWork combo can make a much more clean decoupling between XWorkActions, validators, and interceptors; but given its ease of configuration and learning curve, Webwork1 should still have its value.

-cptechno


Getting Spring's ApplicationContext in Webwork1 Actions

For Webwork, the ServletContext can be retrieved via ServletActionContext.getServletContext ();

in code it will be



import webwork.action.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
....

ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());



For simplicity it may be a good idea to make a base class that extends webwork.actions.ActionSupport, and make a method that will return ApplicationContext, like this:



protected final ApplicationContext getApplicationContext()
{
   return WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
}



then any Actions can extend this base class and use
getApplicationContext() directly.


Facilitate JUnit testing of Webwork action using Spring-managed JavaBeans outside the server

ApplicationContext can not only be configured via org.springframework.web.context.ContextLoaderListener, but also from org.springframework.context.support.ClassPathXmlApplicationContext, and org.springframework.context.support.FileSystemXmlApplicationContext as well. In this way, if Webwork actions can have a way to give them the ApplicationContext, Webwork actions, together with the JavaBeans configured into Spring, can also perform unit tests with JUnit easily.

Here is one of the way - give actions a way for others to inject the ApplicationContext to it.



import org.springframework.context.ApplicationContext;
....
private ApplicationContext context;
....
public void setApplicationContext(ApplicationContext context)
{
   this.context = context;
}



The
getApplicationContext() method used above can make modification so that ApplicationContext will be found from ServletContext, if it is not set externally.



protected final ApplicationContext getApplicationContext()
{
   if(context == null)
   {
      return WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
   }
   else
   {
      return context;
   }
}



So in the unit test code the action can be given the ApplicationContext via



ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext-test.xml");
MyAction myAction = new MyAction();
myAction.setApplicationContext(context);



usually you'll do this at
setUp() method. And you can check whether the command execution is successful with something like



public void testCommandExecution() throws Exception
{
   String result = myAction.execute();
   assertEquals("The action should return SUCCESS!", webwork.actions.ActionSupport.SUCCESS, result);
}



The above methods may not be the best one.