在grails框架中导入groovy脚本方法(四)
前面都写了三个方法了,都不是能用的方法。这是最后的方法了……
以上三种方法都不支持导入功能,例如:在你的外部script中加入:
import MyProject.domain.User;
这时你就可以把user作为一个对象 开始应用了!
这是我的script:
def query1={String dd=User.findByName("license")?.value //在这里我使用了grails里domain的方法: println dd } def query2={ println "this is query3" query1.run() //这是闭包之间的调用; }File scriptFile = new File("controllers\\${scriptName}") Script script = new GroovyClassLoader().parseClass(scriptFile).newInstance() script.binding = new Binding(["service2": new PluginService(), "params": params]) return script.run()File scriptFile = new File("controllers\\${scriptName}") Script script = new GroovyClassLoader().parseClass(scriptFile).newInstance() script.binding = new Binding(["service2": new PluginService(), "params":params])if(methodName){ //如果调用脚本时有method传入,则……return script.invokeMethod(methodName,binding) }else{return script.run()}