XWork2中的配置主要是有以下幾個類完成:
ConfigurationManager
Configuration
ConfigurationProvider
其中:Configuration和ConfigurationProvider是兩個接口。
ConfigurationManager:
是XWork2配置的核心, 它支持ConfigurationProvider的動態加入,進而可以生成自設定的Configuration。通常情況下,實例化一個ConfigurationManager來完成XWork2的應用。
ConfigurationProvider:
ConfigurationProvider的作用是對配置Configuration對象進行支持、幫助,將該Configuration對象擁有那些Action、Result對象是如何和Action映射、使用了那些Interceptors以及它們和Action的映射關係等信息提供給Configuration對象。XWork2的默認的ConfigurationProvider是XmlConfigurationProvider, 該Provider通過參數中指定的XML文件包含的信息來配置Configuration對象。
Configuration:
Configuration 是一個典型的值對象, 它包含了配置的信息, 對於每個ConfigurationManager,僅對應於一個Configuration接口的實例, 它被傳遞個每一個Provider,每個Provider將自己存儲的配置信息賦給Configuration對象。
ConfigurationManager confManager = new ConfigurationManager();
confManager.setConfiguration(new MyCustomConfiguration(...));
第一個XWork程序
XWork的配置文件xwork-hello-world.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xwork PUBLIC
"-//OpenSymphony Group//XWork 1.1.1//EN"
<xwork>
<include file="xwork-default.xml" />
<package name="default-hello-world" extends="xwork-default" namespace="/helloWorld">
<result-types>
<result-type name="printToConsole" class="example.helloworld.PrintToConsoleResult" />
</result-types>
<action name="helloWorld" class="example.helloworld.HelloWorldAction">
<result type="printToConsole">
<param name="param">${message}</param>
</result>
</action>
</package>
</xwork>
啓動程序:
package example.helloworld;
package example.helloworld;
import java.util.LinkedHashMap;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.ActionProxyFactory;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.config.providers.XWorkConfigurationProvider;
import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider;
/**
* @author Jackie
*
*/
public class HelloWorldTutorial {
public static void main(String[] args) throws Exception {
ConfigurationManager confManager = new ConfigurationManager();
//將自己定義xwork配置加入到Configuration Manager
confManager.addContainerProvider(
new XmlConfigurationProvider(
"example/helloworld/xwork-hello-world.xml",
true));
//將xwork的默認xml配置加入到Configuration Provider
confManager.addContainerProvider(new XWorkConfigurationProvider());
//得到Configuration對象
Configuration conf = confManager.getConfiguration();
ActionProxyFactory actionProxyFactory = conf.getContainer().getInstance(ActionProxyFactory.class);
ActionProxy actionProxy = actionProxyFactory.createActionProxy(
"/helloWorld", "helloWorld", null, new LinkedHashMap());
actionProxy.execute();
}
HelloWorldAction.java
package example.helloworld;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Jackie
*
*/
public class HelloWorldAction extends ActionSupport{
private static final long serialVersionUID = 6874543345469426109L;
private static final Log _log = LogFactory.getLog(HelloWorldAction.class);
private String message ;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() {
_log.debug("execute...");
message = "Hello World";
return SUCCESS;
}
}
HelloWorldAction對應的Result:
package example.helloworld;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.util.TextParseUtil;
/**
* @author Jackie
*
*/
public class PrintToConsoleResult implements Result{
private static final Log _log = LogFactory.getLog(PrintToConsoleResult.class);
private static final long serialVersionUID = -6173326554804520601L;
private String param = "whatsoever";
public void setParam(String param) { this.param = param; }
public String getParam() { return this.param; }
@Override
public void execute(ActionInvocation invocation) throws Exception {
_log.debug("execute...");
String result = TextParseUtil.translateVariables(param, invocation.getStack());
System.out.println(result);
}