其他类型动作的实现方法

1.实现其类型动作的方法

  1. 定义一个动作模型。
  2. 实现它的run方法,可以用JavaAction模型或其它动作模型来实现。
  3. 把动作模型作为描述者来实例化具体的动作模型。

2.GroovyAction的示例

2.1.Groovy简介

    Groovy是一个Java的脚本语言,在动态模型中大量使用。

2.2.定义GroovyAction

    如上图,主要定义GroovyAction的run方法以及code(代码属性)。

2.3.GroovyAction的Java静态方法实现

    下面是GroovyAction的Java静态方法实现的示意代码。

public class GroovyAction {
	
	public static Object run(ActionContext context) throws Exception{				
		//获取Groovy动作实例模型
		Bindings bindings = context.getScope(context.getScopesSize() - 2);
		
		World world = bindings.world;
		Action action = null;
		if(bindings.getCaller() instanceof Thing){
			Thing actionThing = (Thing) bindings.getCaller();
			action = actionThing.getAction();
			action.checkChanged();
		}else{
			action = (Action) bindings.getCaller();
		}
		
		if(action.actionClass == null || action.changed){
			//查看代码是否需要重新编译			
			......省略
		}
				
		if(action.actionClass  != null){			
		  //执行Groovy加拨嗯
			Script script = (Script) action.actionClass .newInstance();
			
			Bindings bindings1 = context.push(null);			
			try{
				context.pushAction(action);
				
				Binding binding = new Binding(context);
				script.setBinding(binding);								
				Object result = script.run();			
				return result;
			}finally{
				context.popAction();
				bindings1.remove("actionContext");
				
				//回填groovy脚本中定义的全局变量
				Bindings varBindings = UtilAction.getVarScope(action.getThing(), context);
				if(varBindings != null){
					varBindings.putAll(bindings1);
				}
                
				context.pop();								
			}
		}
		
		return null;
	}
}

2.4.GroovyAction的示例

 

 

Copyright ©  2007-2019 XWorker.org  版权所有  沪ICP备08000575号