Apache Velocity:验证是否已完成所有替换。

huangapple go评论54阅读模式
英文:

Apache Velocity : Verify if all substitutions are made

问题

Sure, here's the translated portion of your text:

"有人能告诉我如何验证速度上下文与模板的匹配吗?基本上,我想要在速度模板中未替换所有变量时引发一些错误。

例如,假设我有一个速度模板,card.vm

  card {
    type: CREDIT
    company: VISA
    name: "${firstName} ${lastName}"
  }

替换如下进行:

VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init();
   
Template t = velocityEngine.getTemplate("card.vm");
    
VelocityContext context = new VelocityContext();
context.put("firstName", "tuk");
StringWriter writer = new StringWriter();
t.merge( context, writer );

在这种情况下,我想引发一些错误,指明lastName在模板中没有被替换。

速度提供了这方面的功能吗?

  • 速度版本 - 1.7"
英文:

Can someone let me know how I can verify a velocity context against a template? Basically, I want to throw some error if all variables are not substituted in the velocity template.

For example, let's say I have a velocity template, card.vm

  card {
    type: CREDIT
    company: VISA
    name: "${firstName} ${lastName}"
  }

The substitution is done like below

VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.init();
   
Template t = velocityEngine.getTemplate("card.vm");
    
VelocityContext context = new VelocityContext();
context.put("firstName", "tuk");
StringWriter writer = new StringWriter();
t.merge( context, writer );

In this case, I want to throw some error specifying that lastName is not replaced in template.

Does velocity provide anything for this?

  • Velocity Version - 1.7

答案1

得分: 0

这在 velocity 邮件列表 中已经得到了解答。

> 不,但这很容易做到。不要使用基本的VelocityContext,而是将其封装在一个自定义的上下文中,类似于这样:

> public class WarningVelocityContext
> extends VelocityContext
> {
> public Object get(String key) {
> Object o = super.get(key);
>
> if(null == o) {
> throw new IllegalStateException("Key {" + key + "} has not
> been set");
> }
> }
> }

> 然后在代码中,在合并之前将上下文封装起来:

> t.merge(new WarningVelocityContext(context), writer);

> 请注意,这将对任何找不到的 任何 东西抛出异常,无论出于什么原因。如果你尝试这样做:

> $someObject.method()

> 并且 "someObject" 是 null,那么你会得到一个错误。可能很难区分你"关心的"事物和你不关心的事物之间的区别。如果你有一个简单的用例,那么这可能会起作用。

英文:

This has been answered in velocity mailing list

> No, but it would be easy to do. Instead of using the basic
> VelocityContext, wrap it in a custom one, something like this:
>
> public class WarningVelocityContext
> extends VelocityContext
> {
> public Object get(String key) {
> Object o = super.get(key);
>
> if(null == o) {
> throw new IllegalStateException("Key {" + key + "} has not
> been set");
> }
> }
> }
>
> Then in your code, wrap the context right before merging:
>
> t.merge(new WarningVelocityContext(context), writer);
>
> Note that this will throw exceptions for anything that's not found
> for any reason. If you try something like:
>
> $someObject.method()
>
> And "someObject" is null, then you'll get an error. It may be
> difficult to tell the difference between the things you "care about"
> and the things you do not. If you have a simple use-case, then
> probably this will work.

huangapple
  • 本文由 发表于 2023年6月13日 17:07:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定