【问题标题】:Velocity: how to partially parse html template?速度:如何部分解析 html 模板?
【发布时间】:2013-10-23 00:17:45
【问题描述】:
Apache的velocity模板引擎是否可以部分解析html模板?
例如:
如果我有这样的模板:
<div class="container">
<div id="section1">Some content of section 1....</div>
<div id="section2">Some content of section 2....</div>
</div>
我只想解析 section1 div 的内容。我怎样才能做到这一点?
我正在使用 Spring MVC 3.0。
【问题讨论】:
标签:
java
html
spring
spring-mvc
velocity
【解决方案1】:
没有直接的方法可以实现这一点,但是可以使用变量来定义部件并辅助部分解析:
<div class="container">
#if ($model.part1)
<div id="section1">Some content of section 1....</div>
#end
#if (model.part2)
<div id="section2">Some content of section 2....</div>
#end
</div>
型号
public class PartialDef {
boolean part1;
boolean part2;
//setters and getters
}
因此,根据您要包含/排除的 html 部分,相应地定义和打开/关闭变量。