关于Xwork的配置对象和XWork第一个程序
2008-08-01 16:41
337 查看
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+Tutorial
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+Tutorial
相关文章推荐
- 关于Xwork的配置对象和XWork第一个程序
- VS2010中关于OpenCV 2.3.1开发环境配置及第一个OpenCV程序
- 第一个oc程序,关于创建对象,声明,点语法,self关键字
- VS2010中关于OpenCV 2.3.1开发环境配置及第一个OpenCV程序
- 【JAVA初学者】配置JDK的详细过程,以及第一个JAVA程序的编写。
- C/C++程序使用lu对象作为配置文件
- 关于使用Spring和hibernate开发web程序的配置说明和简单实例的详细说明
- OpenGL与红宝书第八版第一个程序配置
- vs2005中第一个opengl程序配置过程
- anjuta的安装、配置以及第一个hello程序
- 关于面向对象思想在程序开发中的应用
- (1)第一个程序(使用配置文件和注解)
- OK6410与Ubuntu 11.10 NFS配置与运行第一个程序
- java环境变量配置及第一个java程序
- OpenGL学习-------visual studio 2010配置和第一个OpenGL程序讲解
- 我的第一个关于类的c++程序
- 01.关于c3p0的配置,以及后期快速得到数据库中查询出来的对象
- 我的第一个关于数据类型的程序
- 由一段程序引发关于对象的思考
- OpenCV学习笔记(一)——安装配置、第一个程序