`

xwork学习(一)

 
阅读更多
基本上是从网上修改的一个demo
流程是 请求-》拦截-》结果的一个响应。
xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 2.0//EN" "http://www.opensymphony.com/xwork/xwork-2.0.dtd">
<xwork>
	<include file="xwork-default.xml" />
	<package name="myPackage"  namespace="/helloWorld">
		<result-types>
			<result-type name="myConsole" class="com.stx.result.ResultTest"/>
		</result-types>		
	    <interceptors>
  	      <interceptor name="helloInterceptor" class="com.stx.interceptor.InterceptorTest"/>
 	    </interceptors>
		<action name="actionTest" class="com.stx.action.ActionTest">
			<interceptor-ref name="helloInterceptor"/>
			<result type="myConsole"></result>
		</action>
	</package>
</xwork>

action 配置
package com.stx.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
public class ActionTest implements Action{	
	
	@Override
	public String execute() throws Exception {
		System.out.println("action="+ActionContext.getContext().get("name"));
		return SUCCESS;
	}
	
}

interceptor拦截器
package com.stx.interceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class InterceptorTest extends AbstractInterceptor{
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
				System.out.println("intercept:"+ActionContext.getContext().get("name")); 	
                       //返回值invocation.invoke();
//中止整个执行,直接返回一个字符串作为resultCode 
//通过递归调用负责调用堆栈中下一个Interceptor的执行invocation.invoke();
//这个参数其实是负责调度到action的类本身参数传进来 形成一个递归
//如果在堆栈内已经不存在任何的Interceptor,调用Action 
     			      return null;
	}

}

result类
package com.stx.result;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
public class ResultTest implements Result {
	@Override
	public void execute(ActionInvocation arg0) throws Exception {
		 System.out.println("result execute:"+ ActionContext.getContext().get("name"));  		
	}
}

test测试
package com.stx.test;
import java.util.HashMap;
import java.util.Map;
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.inject.Container;
public class TestXwork {
	public static void main(String[] args) {
		ConfigurationManager cm=new ConfigurationManager ()			Configuration cg=cm.getConfiguration();    
		Container c = cg.getContainer();  		
	   ActionProxyFactory actionProxyFactory = c.getInstance(ActionProxyFactory.class);  
	       Map<String,Object> m = new HashMap<String,Object>(); 	       	        m.put("name","bobo");  
		        ActionProxy actionProxy = actionProxyFactory.createActionProxy(  
		            "/helloWorld", "actionTest",null, m); 
		        for(int i=-2;i<actionProxy.getInvocation().getStack().size();i++){
		        	System.out.println(actionProxy.getInvocation().getStack().pop());
		        }		        
	        try {  
		           actionProxy.execute();  
		        } catch (Exception e) {  
		           // TODO Auto-generated catch block   
		            e.printStackTrace();  
		        }  
		   }  	
}

整个运行流程出来了。。。
中间的一些细节不明白,看源码的时候 接口看的眼花缭乱!
这是第一次源码感觉很吃力。
分享到:
评论

相关推荐

    xwork-2.0.4源码

    xwork-2.0.4源码.不错的struts2学习资料之一

    struts学习笔记(3)

    当客户端向action发送请求并且最后跳转到另外一个页面的时候,在跳转的同时,struts2框架会帮我们自动把需要传到页面的值放这两个对象当中去,然后我们在页面就可以使用固定的方式把值从这个两个对象里面取出来了。...

    struts2(1-7)源码 struts2学习入门 源码学习

    struts7 制作一个表单,对表单中的内容进行自定义验证。 struts8 全局性的验证xwork.default.invalid.fieldvalue={0} error struts9 进行局部性的验证,使用struts2标签库对register.jsp进行改进 strrts10 第七讲...

    struts自我学习过程程序以及说明

    本资源包括struts的学习程序,能够完全运行,当然,我是完全调试运行出来的,都是源代码原封上传,还有说明文档。还有自我总结资料,放到下一个文件夹当中上传,这里压缩只有这么多了,希望能去下载,那个全部是文档...

    struts项目学习笔记

    Struts2 是一个非常优秀的MVC框架,基于Model2 设计模型 由传统Struts1和WebWork两个经典框架发展而来 Struts2框架=Struts2+XWork Strust2 核心功能 允许POJO(Plain Old Java Objects,简单javabean对象,没有继承,...

    struts2必须JAR包(6个)

    struts2必须JAR包 ...让我们一起来学习struts2吧。 struts2-spring-plugin-2.0.11.2.jar xwork-2.0.6.jar struts2-core-2.0.12.jar freemarker-2.3.8.jar ognl-2.6.11.jar commons-logging-1.0.4.jar

    struts2最常用的一组包

    包含xwork-core-2.3.34.jar,struts2-core-2.3.34.jar等等,刚学习struts2开发时特别实用。

    struts2 PPT教程

    自我感觉不错的sturts2教程,比较着struts1进行讲解,标示出很多需要注意饿地方,简单易懂,很适合新手学习;希望对你有帮助,下面是xwork-2.16的源代码的下载地址http://release.opensymphony.com/xwork/2.1.6/

    Struts2教程

    Struts2 Struts2 Struts2 教程 1 1 1 :第一个 Struts2 Struts2 Struts2 程序 在本系列教程中我们将学习到 Struts2 的各种技术。在本教程中使用的工具和程序库的版本 如下: 开发工具: MyEclipse6 Web 服务器: ...

    spring in action英文版

     4.1 学习Spring的DAO理念  4.1.1 理解Spring的DataAccessException  4.1.2 与DataSource一起工作  4.1.3 一致的DAO支持  4.2 在Spring中使用JDBC  4.2.1 JDBC代码的问题  4.2.2 使用...

    struts2框架包

    struts2必须JAR包 自己在网上找了好长时间,终于让我找到。 浪费自己好长时间,为了让更多想学struts2的同学 能够更快的找到jar,我这里... 让我们一起来学习struts2吧。 struts2-spring-plugin-2.0.11.2.jar xwork-2.

    (2.0版本)自己写的struts2+hibernate+spring实例

    该项目使用的jar包为以下. spring-beans.jar xwork-2.0.4.jar spring-context.jar ognl-2.6.11.jar spring-web.jar spring-core.jar struts2-core-2.0.11.1.jar commons-logging-...

    struts项目搭建

    import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; public String execute(){ return ...

    java课程设计(通讯录管理系统).doc

    希望能通过对java课程设计的学习和掌握 来编写一个通讯录管理系统。 二.设计要求 (1)只能使用Java语言,采用面向对象方法进行设计,要求源程序要有适当的注释,使 程序容易阅读。 (2)程序必须与数据库进行通信...

Global site tag (gtag.js) - Google Analytics