Apache velocity IncludeTool - 条件包含
Apache velocity IncludeTool - conditional include
我有一个模板,它将包含在#parse 其他模板中。
问题是我不知道我正在尝试解析的文件始终存在。
我在 velocity-tools-2 中找到了 IncludeTool class。0.jar,我已经添加为变量,但是当他必须测试它时仍然失败。
谁能告诉我如何将 IncludeTool 添加到我的模板中?
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
如果这不起作用,可以尝试一些方法
- 如果文件存在于 fakerecord 文件夹中,你可以 运行 #parse("fakerecord/file.vm") 不进行存在性测试吗?
- 将 $incl.getClass().getCanonicalName() 放入您的模板中。它打印什么?
我用 exists 函数创建了一个新的 class(正如 David Vonka 所建议的)
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ResourceNotFoundException;
public class existTemplate {
private VelocityEngine engine;
public existTemplate(VelocityEngine e) {
engine =new VelocityEngine();
engine = e;
}
public boolean exists(String name)
{
try
{
// checks for both templates and static content
return engine.resourceExists(name);
}
// make sure about this...
catch (ResourceNotFoundException rnfe)
{
return false;
}
catch (NullPointerException rnfe)
{
return false;
}
}
}
然后在发送参数时使用它
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
我有一个模板,它将包含在#parse 其他模板中。 问题是我不知道我正在尝试解析的文件始终存在。
我在 velocity-tools-2 中找到了 IncludeTool class。0.jar,我已经添加为变量,但是当他必须测试它时仍然失败。 谁能告诉我如何将 IncludeTool 添加到我的模板中?
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
如果这不起作用,可以尝试一些方法
- 如果文件存在于 fakerecord 文件夹中,你可以 运行 #parse("fakerecord/file.vm") 不进行存在性测试吗?
- 将 $incl.getClass().getCanonicalName() 放入您的模板中。它打印什么?
我用 exists 函数创建了一个新的 class(正如 David Vonka 所建议的)
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ResourceNotFoundException;
public class existTemplate {
private VelocityEngine engine;
public existTemplate(VelocityEngine e) {
engine =new VelocityEngine();
engine = e;
}
public boolean exists(String name)
{
try
{
// checks for both templates and static content
return engine.resourceExists(name);
}
// make sure about this...
catch (ResourceNotFoundException rnfe)
{
return false;
}
catch (NullPointerException rnfe)
{
return false;
}
}
}
然后在发送参数时使用它
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