如何从servlet中运行java中的groovy脚本?使用下面的代码,我有一个
MissingPropertyException:没有这样的属性:请求类:Script1
这是我的剧本
<script type="server/groovy">
import com.daimler.Car
def id = request.getParameter("id")
car = Car.lookup(id)
</script>
这是我的Java代码
public void runScript(HttpServletRequest request) {
Script script = groovyShell.parse("def id = request.getParameter(\"id\")\n" +
" car = Car.lookup(id)");
Map bindings = script.getBinding().getVariables();
bindings.put("id",1);
Object ret = script.run(); //a+b+3
//and if you changed variables in script you can get their values
Object aAfter = bindings.get("car");
}
答案 0 :(得分:0)
您还需要在绑定中传递HttpServletRequest请求对象:
Map bindings = script.getBinding().getVariables();
bindings.put("id",1);
bindings.put("request", request);
Object ret = script.run(); //a+b+3 (...)