我有一个 jsp 页面绑定(bind)到运行 groovy 脚本的 servlet。我能够从 servlet 获取 groovy 脚本。但是在脚本运行后,如何将 groovy 脚本的响应返回到 servlet 以显示在 jsp 页面中?
我的java servlet代码如下:
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:没有方法签名: 运行测试
这是我的测试脚本中的内容。也许我没有正确返回它。
class TestScript
{
@Test
public String runTest()
{
//run test
return response
}
}
最佳答案
让 runTest
groovy 方法返回您要传递给 servlet 的值,并将其捕获为 groovyObject.invokeMethod
的返回值:
Object ret = groovyObject.invokeMethod("runTest", args1);
System.out.println("In Java " + ret);
关于java - 如何将值从 groovy 返回到 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5267481/