如何从groovy返回值到java [英] how to return value from groovy to java
问题描述
我有一个绑定到运行groovy脚本的servlet的jsp页面。我可以从servlet中获得groovy脚本。但是在脚本运行后,我该如何将groovy脚本的响应返回给servlet以显示在jsp页面中?
I have a jsp page tied to a servlet that runs groovy scripts. I am able to get to the groovy script from the servlet. But after the script runs how do I return the response from the groovy script back to the servlet to be displayed in the jsp page?
我的java servlet代码如下所示:
My java servlet code is as follows:
File file = new File("TestScript.groovy");
ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(file);
Object[] args = {};
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
groovyObject.invokeMethod("runTest", args);
更新:
groovyscriptengine和grovyshell,但试图运行这些时,我得到以下异常:
org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack:没有方法的签名:
runTest
Update:
I also looked into groovyscriptengine and grovyshell but when trying to run those i get the following exception: org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: runTest
这是我在我的测试脚本中所具有的。
This what i have in my test script. Maybe I am not returning it properly.
class TestScript
{
@Test
public String runTest()
{
//run test
return response
}
}
推荐答案
使 runTest
groovy方法返回您想传递给servlet的值,并将其作为 groovyObject.invokeMethod
的返回值捕获:
Have the runTest
groovy method return the value you want to pass to the servlet, and capture it as the return value of groovyObject.invokeMethod
:
Object ret = groovyObject.invokeMethod("runTest", args1);
System.out.println("In Java " + ret);
这篇关于如何从groovy返回值到java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!