Configuring XWork2 centers around the following classes:-

  • ConfigurationManager
  • ConfigurationProvider
  • Configuration

where by both ConfigurationProvider and Configuration are java interface.

ConfigurationManager

This is the center for configuring XWork2. It allows ConfigurationProvider to be pluged in and setting a custom Configuration to be used. Typically one would like to just create one instance of ConfigurationManager for a XWork2 usage.

Code

To create a ConfigurationManager :-

ConfigurationManager confManager = new ConfigurationManager();

ConfigurationProvider

ConfigurationProvider helps configuring a Configuration, populating it with information regarding what actions it has, how does the results get mapped and what interceptors are there and how they are related to each action etc. A default ConfigurationProvider that comes with XWork2 would be XmlConfigurationProvider and as the name suggest populate information into a Configuration object according the the xml provided.

Code

To create a plug in a custom configuration provider

ConfigurationManager confManager = new ConfigurationManager();
  confManager.addConfigurationProvider(
    new MyCustomConfigurationProvider(....));

To create an XmlConfigurationProvider that points to a particular xml file in the classpath:-

ConfigurationManager confManager = new ConfigurationManager();
  confManager.addConfigurationProvider(
    new XmlConfigurationProvider("foo/bar/myConf.xml"));

Configuration

Configuration is a typical value object that contains configuration information. There's only one instance of it for each ConfigurationManager, where it is passed to different ConfigurationProvider in order for information to get stoted in it. The default implementation would be DefaultConfiguration.

Code

To plug in a custom Configuration

ConfigurationManager confManager = new ConfigurationManager();
  confManager.setConfiguration(new MyCustomConfiguration(...));

The following illustrates typically how XWork2 is configured in the xwork.xml configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xwork PUBLIC 
	"-//OpenSymphony Group//XWork 1.1.1//EN"
	"http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">


<xwork>
	<include file="xwork-default.xml" />
	<package name="default-hello-world" extends="xwork-default" namespace="/helloWorld">
		<result-types>
			<result-type name="printToConsole" class="com.opensymphony.xwork2.showcase.PrintToConsoleResult" />
		</result-types>
		
		<action name="helloWorld" class="com.opensymphony.xwork2.showcase.helloworld.HelloWorldAction">
			<result type="printToConsole">
				<param name="param">${message}</param>
			</result>
		</action>
	</package>
</xwork>