3

VelocityTools Tools Usage Summary和DateTool Javadoc似乎都表明了用于设置 TimeZone 的配置机制,但我根本无法发现那是什么。

DateTool 的时区设置器是protected,我不想重复将 TimeZone 传递给重载的format()方法。

(速度工具 2.0)

4

1 回答 1

7

要将配置值传递给标准工具,您必须在 WEB-INF/tools.xml 文件中明确列出它们:

<?xml version="1.0"?>
<tools>
  <toolbox scope="application">
    <tool key="date"
          class="org.apache.velocity.tools.generic.DateTool"
          timezone="GMT+7"/>
    ...
  </toolbox>
  ...
</tools>

请注意,您可以将 org.apache.velocity.tools.generic.ComparisonDateTool 用于相同目的,它添加了一些不错的日期时间比较功能。

如果您不依赖 VelocityView 工具加载机制,那么这意味着您将自己的工具置于 Velocity 上下文中。如果是这样,那么配置值将手动提供给工具 configure(Map)。例如:

Map<String,String> config = new HashMap<>();
config.put(DateTool.TIMEZONE_KEY,"GMT+7");

DateTool date = new DateTool();
date.configure(config);

VelocityContext context = new VelocityContext();
context.put("date", date);
于 2012-08-16T13:13:57.830 回答