从 Apache Velocity 模板访问常量值? [英] Accessing constant values from an Apache Velocity template?

查看:22
本文介绍了从 Apache Velocity 模板访问常量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从 Velocity 模板访问常量值(即 Java 类中定义的公共静态最终变量)?

Is it possible to access a constant value (i.e. a public static final variable defined in a Java class) from a Velocity template?

我希望能够写出这样的东西:

I would like to be able to write something like this:

#if ($a lt Long.MAX_VALUE)

但这显然不是正确的语法.

but this is apparently not the right syntax.

推荐答案

有多种方法.

1) 您可以将值直接放在上下文中.

1) You can put the values directly in the context.

2) 您可以使用 FieldMethodizer 使类中的所有公共静态字段都可用.

2) You can use the FieldMethodizer to make all public static fields in a class available.

3) 您可以使用自定义的 Uberspect 实现,该实现在查找顺序中包含公共静态字段.

3) You can use a custom Uberspect implementation that includes public static fields in the lookup order.

4) 您可以使用 来自 VelocityTools 的 FieldTool.

我推荐 1 代表少数值,2 代表少数类,3 代表大量类和值,4 如果您已经在使用 VelocityTools,否则会使用 1 或 2.

I recommend 1 for a few values, 2 for a few classes, 3 for lots of classes and values, and 4 if you are already using VelocityTools and would otherwise use 1 or 2.

这篇关于从 Apache Velocity 模板访问常量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆