可以把动作当作具有参数的函数和方法,调用时则需要向其传入所需要的参数。
函数动作示意代码:
void printFn(name){ println "hello " + name; }
测试函数动作的代码:
def printFn= world.getAction("example.action.ParameterExample/@printFn"); //没有参数 //printFn.run(actionContext); //相当于:printFn() //传入参数,参数为Map<String, Object> printFn.run(actionContext, ["name": "Parameter by map"]); //相当于:printFn("Parameter by map"); //通过新栈传参 def bindings = actionContext.push(); try{ bindings.put("name", "Parameter by push"); printFn.run(actionContext); }finally{ //push和pop要成对 actionContext.pop(); } //通过peek()也可以 actionContext.peek().put("name", "Parameter by peek"); printFn.run(actionContext); //上面两种方式相当于 def name = "Parameter by push(peek)"; printFn(); //此时printFn中的name变量是def name = xxx;的这个name变量,是隐式的传参方法。
Copyright © 2007-2019 XWorker.org 版权所有 沪ICP备08000575号