Java Apache velocity IncludeTool-条件包含
我有一个模板,其中包括#解析其他模板。 问题是我不知道我试图解析的alwais文件是否存在 我已经在velocity-tools-2.0.jar中创建了IncludeTool类,我已经添加了一个变量,但是当他必须测试它失败时,仍然是这样。 有人能告诉我如何将IncludeTool添加到我的模板中吗Java Apache velocity IncludeTool-条件包含,java,parsing,include,conditional,velocity,Java,Parsing,Include,Conditional,Velocity,我有一个模板,其中包括#解析其他模板。 问题是我不知道我试图解析的alwais文件是否存在 我已经在velocity-tools-2.0.jar中创建了IncludeTool类,我已经添加了一个变量,但是当他必须测试它失败时,仍然是这样。 有人能告诉我如何将IncludeTool添加到我的模板中吗 private VelocityContext transmitParameters(params prm){ VelocityContext c = new VelocityCont
private VelocityContext transmitParameters(params prm){
VelocityContext c = new VelocityContext();
//transmit parameters one by one
c.put("program_name", prm.getProgram_name());
c.put("date", new DateTool());
c.put("incl", new IncludeTool());
return c;
}
public generate(params prm) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, constants.TEMPLATE_PATH);
ve.init();
context = new VelocityContext(transmitParameters(p));
writer = new StringWriter();
t.merge(context, writer);
}
模板呢
#if($incl.exists("templates/$record.name/file.vm"))
#parse("$record.name/file.vm")
#end
谢谢。我觉得很奇怪的是路径的不同。其中一个包含模板/,另一个不包含。我倾向于尝试
#if($incl.exists("$record.name/file.vm"))
#parse("$record.name/file.vm")
#end
如果这不起作用,有一些事情可以尝试
对我来说,最奇怪的是路径的不同。其中一个包含模板/,另一个不包含。我倾向于尝试
#if($incl.exists("$record.name/file.vm"))
#parse("$record.name/file.vm")
#end
如果这不起作用,有一些事情可以尝试
我用exists函数创建了一个新类(正如David Vonka所建议的) 然后在发送参数时使用它
VelocityEngine ve = new VelocityEngine();
ve.init();
incl = new existTemplate(ve);
VelocityContext c = new VelocityContext();
c.put("date", new DateTool());
c.put("incl", incl);
内部使用的tempalte如下所示:
#if($incl.exists("$record.name/file.vm"))
#parse("$record.name/file.vm")
#end
我用exists函数创建了一个新类(正如David Vonka所建议的) 然后在发送参数时使用它
VelocityEngine ve = new VelocityEngine();
ve.init();
incl = new existTemplate(ve);
VelocityContext c = new VelocityContext();
c.put("date", new DateTool());
c.put("incl", incl);
内部使用的tempalte如下所示:
#if($incl.exists("$record.name/file.vm"))
#parse("$record.name/file.vm")
#end
1.如果文件存在,解析工作正常。但是我不想创建空模板,这样#parse函数就可以工作了。它会打印:org.apache.velocity.tools.view.IncludeTool,原因是:org.apache.velocity.tools.view.IncludeTool.exists(IncludeTool.java:192)IncludeTool.java
public boolean exists(字符串名){try{//检查模板和静态内容返回引擎。resourceExists(name);}//确保此…catch(ResourceNotFoundException rnfe){return false;}}
以下是引擎的初始化:VelocityEngine ve=new VelocityEngine();ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,constants.TEMPLATE_PATH);ve.init()
这是一个初始化问题。首先需要获取ViewToolContext vtc=new ViewToolContext(engine、httpRequest、httpResponse、servletContext),然后通过调用incl.configure(vtc)配置IncludeTool在将incl放入velocity上下文之前。查看源代码,在我看来,即使将httpRequest、httpResponse和servletContext全部设置为null,如果您所需要的只是exist方法,如果您无法访问它们的值,也应该可以工作。1.如果文件存在,解析工作正常。但我不想创建空模板o解析函数是否工作2.它打印:org.apache.velocity.tools.view.IncludeTool导致:java.lang.NullPointerException位于org.apache.velocity.tools.view.IncludeTool.exists(IncludeTool.java:192)IncludeTool.java内部公共布尔存在(字符串名称){try{//检查模板和静态内容返回引擎。resourceExists(name);}//确保此…catch(ResourceNotFoundException rnfe){return false;}}
以下是引擎的初始化:VelocityEngine ve=new VelocityEngine();ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,constants.TEMPLATE_PATH);ve.init()
这是一个初始化问题。首先需要获取ViewToolContext vtc=new ViewToolContext(engine、httpRequest、httpResponse、servletContext),然后通过调用incl.configure(vtc)配置IncludeTool在您将incl放入velocity上下文之前。查看源代码,在我看来,如果您所需要的只是exist方法,如果您无权访问它们的值,那么即使将httpRequest、httpResponse和servletContext全部设置为null,也应该可以工作。