编写velocity tool

本文详细介绍了如何在工具开发中进行命名、作用域划分、安全配置,并提供了错误处理、灵活性、选择作用域与避免线程安全问题的建议。通过示例代码和配置文件,展示了如何实现一个完全可配置且符合Velocity Tools标准的自定义工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Those of you who write your own custom tools may want to make a few changes to upgrade your custom tools to do things the "Tools 2 way". Here's a few quick starts for that, though this doesn't cover everything. More details can be found in the instructions for creating tools.

 

Naming: The recommended practice is to give a tool to be used as $foo the name FooTool . This is not required but is a convention that keeps things easy follow and learn. If you have to name a tool FooBarUtility but want it to be $foo in templates, the second best thing is to provide a @DefaultKey("foo") annotation on the class, though this introduces a dependency on VelocityTools. As a last resort, if you are providing tools for a framework or otherwise can influence or control the configuration, you might consider providing a default configuration--perhaps even one automatically discoverable by ConfigurationUtils --to set the tool's key for your users.

Scoping: If your tool is only meant to be used in a particular scope, it's recommended that you give the class a @ValidScope(Scope.REQUEST) annotation as well. If you only want to ban a particular scope and allow all others, you could provide a @InvalidScope(Scope.APPLICATION) annotation on the class. The Scope class provides constants for REQUEST, SESSION, and APPLICATION. Other scopes are now theoretically possible, but only a little work and no testing has been done there at this point.

Configuration: If you have a configurable tool whose configuration should not changed by the templates which use it, then consider having your tool extend the SafeConfig class (or FormatConfig or LocaleConfig ). These safely standardize configuration of these common configuration properties. Also take note that the configure(Map) and init(Object) methods have been changed into just the configure(Map) and individual setter methods (e.g. setRequest, setSession, etc). Basically, when it's time to instantiate a tool, the tool manager will gather all the "configuration properties" for that tool, its toolbox, etc and combine it into a single map with the "init data" (context, request, session, etc). The manager searches for relevant setter methods that accept that data and also for a configure(Map) method. The setters get what they're asking for (if available) and the configure() method accepts the whole combined Map. The upshot of this approach is that tools no longer need to conform to any interfaces or patterns. In fact, it's possible to write a FooTool that doesn't know anything about any VelocityTools classes whatsoever and yet be fully instantiable and configurable by VelocityTools. Your tools don't need to know about anything except what they need to know about.

 

开发时需要注意

1 Tool Keys/Names

2 scope

3 SafeConfig

4 Be robust. Catch exceptions and return null on errors.

5 Be flexible. Have methods accept Object when possible.

6 Be careful. Choose scope carefully and be aware of thread safety issues.

7 Be fluent. Subtools or get(key) methods can make a clear and flexible API.

 

example

java代码:

import org.apache.velocity.tools.config.DefaultKey;

@DefaultKey("myTool")
public class VelocityTools {
    public VelocityTools() {
    }
    public String getName(){
        return "hello world";
    }
}

 配置文件

<tools>
    <toolbox scope="request">
        <tool class="com.lufax.reporter.web.VelocityTools"/>
    </toolbox>
</tools>

 

velocity VM文件调用

<DIV>
$myTool.getName()
</DIV>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值