关于Xwork的配置对象和XWork第一个程序
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"
"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="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);
}
}
参考:
Xwork的doc文档 http://wiki.opensymphony.com/display/XW/XWork2+Hello+World+Tutoria