英文:
Apache Velocity: Verify if all variables in a context are present in velocity template
问题
在Apache Velocity 1.7中,是否可以检查指定在VelocityContext
中的所有键是否对模板有效?例如,假设我有一个名为card.vm
的模板,如下所示:
card {
type: CREDIT
company: VISA
name: "${firstName} ${lastName}"
}
如果我执行以下代码,由于cardNumber
未在card.vm
模板中出现,它应该会引发错误:
VelocityContext context = new VelocityContext();
context.put("firstName", "tuk");
context.put("lastName", "man");
context.put("cardNumber", "1234");
StringWriter writer = new StringWriter();
t.merge(context, writer);
(注意:我已经将双引号改为了中文引号“”以避免混淆)
英文:
Is it possible in Apache Velocity 1.7 to check if all keys specified in a VelocityContext
are valid for a template? For example, let's say I have a template, card.vm
, that looks like this:
card {
type: CREDIT
company: VISA
name: "${firstName} ${lastName}"
}
If I then execute the following code, it should throw an error since cardNumber
is not present in the card.vm
template:
VelocityContext context = new VelocityContext();
context.put("firstName", "tuk");
context.put("lastName", "man");
context.put("cardNumber", "1234");
StringWriter writer = new StringWriter();
t.merge(context, writer);
答案1
得分: 0
如克里斯托弗在速度邮件列表中提到的,针对你之前的问题,你可以随时创建VelocityContext的子类来自定义行为。
例如,这将跟踪哪些键已放入上下文并跟踪上下文中使用了哪些键。然后在合并模板后,您将调用context.checkForUnreferencedValues()。
根据哪些辅助对象向您的上下文添加键以及您是否在重复使用,您可能需要定制代码。
static class ReferenceCountingVelocityContext extends VelocityContext {
private Set<String> keysToCheck = new HashSet<>();
private Set<String> keysUsed = new HashSet<>();
@Override
public Object put(String key, Object value) {
keysToCheck.add(key);
return super.put(key, value);
}
@Override
public Object get(String key) {
keysUsed.add(key);
return super.get(key);
}
public void checkForUnreferencedValues() throws UnusedVelocityContextKeyException {
if (!keysToCheck.containsAll(keysUsed)) {
throw new UnusedVelocityContextKeyException();
}
}
}
英文:
As Christopher mentioned on the velocity mailing list for your earlier question, you can always create a subclass of VelocityContext to customize behavior like this.
For instance, this will track what keys have been put into the context and track what keys were used in the context. You would then call context.checkForUnreferencedValues() after you merge the template.
You will likely need to customize the code depending on what helper objects are adding keys to your context and whether you are reusing it.
static class ReferenceCountingVelocityContext extends VelocityContext {
private Set<String> keysToCheck = new HashSet<>();
private Set<String> keysUsed = new HashSet<>();
@Override
public Object put(String key, Object value) {
keysToCheck.add(key);
return super.put(key, value);
}
@Override
public Object get(String key) {
keysUsed.add(key);
return super.get(key);
}
public void checkForUnreferencedValues() throws UnusedVelocityContextKeyException {
if (!keysToCheck.containsAll(keysUsed)) {
throw new UnusedVelocityContextKeyException();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论