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>
分享到:
相关推荐
对于更复杂的缓冲运动,可以使用JavaScript编写自定义函数。比如,`easeInQuad`, `easeOutQuad`, `easeInOutQuad`等是二次贝塞尔曲线缓动函数,它们分别代表加速开始、减速结束、先加速后减速的效果。这些函数通过...
1. **模板引擎**:如FreeMarker或Velocity,它们允许开发者定义模板文件,将数据模型与代码结构结合,生成相应的代码。模板文件中可以包含变量、条件判断、循环等逻辑,使得生成的代码更加灵活。 2. **元数据配置**...
- 使用Velocity或Freemarker语法编写模板文件。例如,创建一个自定义的实体类模板,用于生成带注释、特定构造函数和getter/setter的Java类。 3. **配置Hibernate Tools**: - 在Hibernate Tools的配置文件中...
2. **模板集成**:通过整合VelocityTools,JSF Tools允许开发者利用Velocity模板语言编写更复杂的视图,使得页面布局和样式更加灵活。 3. **数据绑定增强**:JSF Tools提供了更强大的数据绑定机制,使得模型数据与...
6. **Veloeclipse**:这是一款用于MyEclipse的Velocity模板语言编辑器,提供了语法高亮、代码提示、自动完成等功能,帮助开发者更方便地编写和调试基于Velocity模板引擎的Web应用。 这些插件的集成使得MyEclipse不...
项目部署方面,使用了Maven 3.0作为项目管理工具,结合MySQL 5.5数据库、JDK 1.7版本以及Spring Tool Suite(STS)或Eclipse IDE进行开发。部署时需要创建数据库、初始化数据、更新配置文件并导入依赖包,最终启动...
这可以通过编写Freemarker或Velocity模板文件实现,然后在配置文件中指定模板路径。 5. **最佳实践** - 使用一致的编码标准,避免因编码不一致导致的乱码问题。 - 为每个数据库表定义清晰的注释,以便在生成实体...
Scalate Generator,通常以"scalate-generator_2.9.2-0.5.0.zip"的形式发布,是专门为Scala构建的工具,它依赖于Scala Build Tool (SBT) 的扩展功能。这个版本的Scalate Generator与Scala 2.9.2兼容,并且处于0.5.0...
通过使用Velocity模板,开发者可以编写出可重用的代码片段,这些片段可以在模型转换过程中根据需要动态生成代码。这在模型驱动开发(MDD)中非常有用,因为它允许开发者将业务逻辑与表示层分离,从而更方便地维护和...
3. **Annotation Processing Tool (APT)**:Java的APT允许开发者编写注解处理器,这些处理器可以在编译时分析源代码并生成额外的源代码或资源。例如,Lombok库利用APT生成getter、setter和其他常用方法,减少了样板...
它允许开发者通过定义脚本(scripts)来创建RESTful API,这些脚本可以是基于FreeMarker、Velocity或JavaScript的模板语言。这样的设计使得Web服务的创建更加灵活和模块化。 Spring Webscripts的架构由以下几个关键...
2. **代码生成工具**:例如,Apache Velocity Tools和FreeMarker等模板引擎可以用于生成Java代码。这些工具允许开发者定义模板,然后填充变量以生成定制的源代码。 3. ** Annotation Processing Tool (APT)**:Java...
该工具利用了Hibernate、Velocity和Webwork等开源技术,为开发者提供了一套灵活的报表解决方案。 2. **JFreeChart**:JFreeChart是一个强大的Java库,专门用于创建各种类型的图表,如饼图、柱状图、线图、区域图等...
例如,研究 DispatcherServlet 如何初始化配置、HandlerMapping 如何工作以及 Model-View-Controller 之间的交互,都能帮助我们编写更高效和灵活的代码。 **8. 工具支持** Spring Tool Suite(STS)、IntelliJ IDEA...
- **纯Java报表工具**:这类工具完全使用Java语言编写,包括报表引擎、内核、设计器界面等所有组件,代表性工具如StyleReport、JasperReport、BIRT、FineReport、iReport、杰表等。 #### 报表工具功能对比 - **...
“Velocity Search UI”部分介绍了Velocity模板在搜索用户界面上的应用,Velocity是一种模板引擎,能够生成动态内容。 在“Relevance”部分讨论了相关性评估和提升搜索结果的相关性。 “Query Syntax and Parsing...
**简介**:DataVision 是一款用 Java 编写的报表工具,类似于 Crystal Reports。它支持多种数据源和输出格式,具备友好的用户界面,使报表设计变得简单快捷。 **主要特性**: - **多数据源支持**:支持 JDBC、文本...
4. **传播速度**(Propagation Velocity):这是信号在传输线中传播的速度,通常小于光速,取决于介质的介电常数。 5. **衰减**(Attenuation):信号在传输线中每单位长度的功率损耗,受导体电阻、介电损耗和辐射...
2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的支持 2.4. 中间层 2.4.1. 在XML里更为简单的声明性事务配置 2.4.2. JPA 2.4.3. 异步的JMS 2.4.4. ...