`

在velocity使用toolbox.xml

阅读更多
首先必须在web.xml中添加:
<init-param>
<param-name>org.apache.velocity.toolbox</param-name>
<param-value>/WEB-INF/toolbox.xml</param-value>     
</init-param>
才可以使用toolbox.xml中定义的变量。

举例说明用法
比如我要使用日期,可以如下定义:
<toolbox>
<tool>
   <key>date</key>
   <scope>application</scope>
   <class>org.apache.velocity.tools.generic.DateTool</class>
   <parameter name="format" value="yyyy-M-d"/>
</tool>
</toolbox>
这里DateTool是velocity为我们预定义的工具类,还有很多其他工具类(比如:MathTool,ListTool,SortTool...)
date是这个类的实例,这样我们就可以在.vm中使用$date来引用DateTool中的方法了,如  $date.get('yyyy-M-d H:m:s')

如果我们要使用自己的类,只需要把org.apache.velocity.tools.generic.DateTool这部分替换成自己定义的类,然后再初始化该类的实例,比如<key>class</key>
在.vm中这样用$class.getXXX()

toolbox.xml典型配置:
<?xml version="1.0" encoding="UTF-8"?>
<!--
=============================================================
  @(#) toolbox.xml
     
  Copyright (c) 2005, HOBOKEN Project, All Rights Reserved.
=============================================================
-->

<toolbox>

    <!-- [ DateTool ]
         @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/generic/DateTool.html  (ja)
        @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/DateTool.html  (en)
        @since VelocityTools 1.0
    -->
    <tool>
        <key>date</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.DateTool</class>
    </tool>
    
    <!-- [ MathTool ]
        @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/generic/MathTool.html  (ja)
        @see http://velocity.apache.org/tools/devel/generic/MathTool.html  (en)
        @since VelocityTools 1.0
    -->
    <tool>
        <key>math</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.MathTool</class>
    </tool>
    
    <!-- [ NumberTool ]
        @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/NumberTool.html  (en)
        @since VelocityTools 1.2
    -->
    <tool>
        <key>number</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.NumberTool</class>
    </tool>
    
    <!-- [ RenderTool ]
        @see http://velocity.apache.org/tools/devel/generic/RenderTool.html  (en)
        @since VelocityTools 1.0
    
    <tool>
        <key>render</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.RenderTool</class>
    </tool>
    -->
    
    <!-- [ EscapeTool ]
        @see http://velocity.apache.org/tools/devel/generic/EscapeTool.html  (en)
        @since VelocityTools 1.2
    -->
    <tool>
        <key>esc</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.EscapeTool</class>
    </tool>
    
    <!-- [ ResourceTool ]
               @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/ResourceTool.html  (en)
        @since Velocity 1.3
    <tool>
        <key>text</key>
        <class>org.apache.velocity.tools.generic.ResourceTool</class>
        <parameter name="bundles" value="resources,prj.hoboken.patrasche.resources.PatrascheResources" />
        <parameter name="locale" value="ja_JP" />
    </tool>
    -->
    
    <!-- [ AlternatorTool ]

        @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/AlternatorTool.html  (en)
        @since VelocityTools 1.2
    -->
    <tool>
        <key>alternator</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.AlternatorTool</class>
    </tool>
    
    <!-- [ ValueParser ]

        @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/ValueParser.html  (en)
        @since VelocityTools 1.2
    -->
    <tool>
        <key>parser</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.ValueParser</class>
    </tool>
    
    <!-- [ ListTool ]

        @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/ListTool.html  (en)
        @since VelocityTools 1.2
    -->
    <tool>
        <key>list</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.ListTool</class>
    </tool>
    
    <!-- [ SortTool ]

        @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/SortTool.html  (en)
        @since VelocityTools 1.2
    -->
    <tool>
        <key>sorter</key>
        <scope>application</scope>
        <class>org.apache.velocity.tools.generic.SortTool</class>
    </tool>
    
    <!-- [ IteratorTool ]
        @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/IteratorTool.html  (en)
        @since VelocityTools 1.0
    -->
    <tool>
        <key>mill</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.generic.IteratorTool</class>
    </tool>
        
<!--
============================================================
  [ TOOL FOR STRUTS TAGLIB ]
============================================================
-->
    <!-- [ ActionMessagesTool ]         
        @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/ActionMessagesTool.html  (ja)
        @see http://velocity.apache.org/tools/devel/struts/ActionMessagesTool.html  (en)
        @since VelocityTools 1.1
    -->
    <tool>
        <key>messages</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.struts.ActionMessagesTool</class>
    </tool>
    
    <!-- [ ErrorsTool ]
        @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/ErrorsTool.html  (ja)
        @see http://velocity.apache.org/tools/devel/struts/ErrorsTool.html  (en)
        @since VelocityTools 1.0
    -->
    <tool>
        <key>errors</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.struts.ErrorsTool</class>
    </tool>
    
    <!-- [ FormTool ]
        @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/FormTool.html (ja)
        @see http://velocity.apache.org/tools/devel/struts/FormTool.html (en)
        @since VelocityTools 1.0
    -->
    <tool>
        <key>form</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.struts.FormTool</class>
    </tool>
    
    <!-- [ MessageTool ]
         @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/MessageTool.html (ja)
        @see http://velocity.apache.org/tools/devel/struts/MessageTool.html  (en)
        @since VelocityTools 1.0
    -->
    <tool>
        <key>resource</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.struts.MessageTool</class>
    </tool>
    
    <!-- [ StrutsLinkTool ]
        LinkTool         
        @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/StrutsLinkTool.html  (ja)
        @see http://velocity.apache.org/tools/devel/struts/StrutsLinkTool.html  (en)
        @since VelocityTools 1.0
    -->
    <tool>
        <key>slink</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.struts.StrutsLinkTool</class>
    </tool>
    
    <!-- [ SecureLinkTool ]
        LinkTool         
        @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/SecureLinkTool.html  (ja)
        @see http://velocity.apache.org/tools/devel/struts/SecureLinkTool.html  (en)
        @since VelocityTools 1.1
    -->
    <tool>
        <key>sslink</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.struts.SecureLinkTool</class>
    </tool>
    
    <!-- [ TilesTool ]
        Tiles         
        @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/TilesTool.html  (ja)
        @see http://velocity.apache.org/tools/devel/struts/TilesTool.html  (en)
        @since VelocityTools 1.1
    -->
    <tool>
        <key>tiles</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.struts.TilesTool</class>
    </tool>
    
    <!-- [ ValidatorTool ]
        Validator         
        @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/ValidatorTool.html  (ja)
        @see http://velocity.apache.org/tools/devel/struts/ValidatorTool.html  (en)
        @since VelocityTools 1.1
    -->
    <tool>
        <key>validator</key>
        <scope>request</scope>
        <class>org.apache.velocity.tools.struts.ValidatorTool</class>
    </tool>


    <data type="string">
        <scope>request</scope>
        <key>app_version</key><value>3.0</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>app_name</key><value>Patrasche</value>
    </data>
    
    <data type="string">
        <scope>request</scope>
        <key>next</key><value>/images/icon/next.jpg</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>prev</key><value>/images/icon/prev.jpg</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>top-page</key><value>/images/icon/top-page.jpg</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>last-page</key><value>/images/icon/last-page.jpg</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>pageIcon</key><value>/images/icon/page.png</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>bookIcon</key><value>/images/icon/book.png</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>defaultStyle</key><value>./web/default.css</value>
    </data>
    <data type="string">
        <scope>request</scope>
       <key>errorMetaHeader</key><value>/error/default_header.vm</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>errorStyle</key><value>/error/error.css</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>detailOfHTTP</key><value>./error/detail_HttpError.vm</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>detailOfSERV</key><value>./error/detail_ServerError.vm</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>multiPart</key><value>enctype='multipart/form-data'</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>parentMark</key><value>..</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>chartToolTips</key><value>toolTips</value>
    </data>
    <data type="string">
        <scope>request</scope>
        <key>chartClickable</key><value>isClickable</value>
    </data>
    
    <!-- [ NullTool ]
    -->
    <tool>
        <key>null</key>
        <scope>application</scope>
        <class>com.company.util.velocity.NullTool</class>
    </tool>

    <!-- [ StringTool ]
        文字操作        
    -->
    <tool>
        <key>words</key>
        <scope>application</scope>
        <class>com.company.util.velocity.StringTool</class>
    </tool>
    
    <!-- [ PageScrolllTool ]
                
    -->
    <tool>
        <key>pager</key>
        <scope>session</scope>
        <class>com.company.util.velocity.PageScrollTool</class>
    </tool>

    <!-- [ LoginUserTool ]
    -->
    <tool>
        <key>loginuser</key>
        <scope>request</scope>
        <class>com.company.util.velocity.LoginUserTool</class>
    </tool>

    <!-- [ MenuTool ]
    -->
    <tool>
        <key>menuviewer</key>
        <scope>request</scope>
        <class>com.company.util.velocity.MenuTool</class>
    </tool>
    
    <!-- [ FileSystemTool ]
        
    -->
    <tool>
        <key>filesys</key>
        <scope>request</scope>
       <class>com.company.util.velocity.contrib.FileSystemTool</class>
    </tool>
    
    <!-- [ ExceptionTool ]        
        
    -->
    <tool>
        <key>exception</key>
        <scope>request</scope>
        <class>com.company.util.velocity.ExceptionTool</class>
    </tool>
    
    <!-- [ POITool ]              
        使用自己写的操作Excel的类
    -->
    <tool>
        <key>poi</key>
        <scope>request</scope>
        <class>com.company.util.velocity.POITool</class>
    </tool>

    <!-- [ ConstantTool ]
        使用自己定义的常量。
    -->
    <tool>
        <key>constant</key>
        <scope>application</scope>
        <class>com.company.Constant</class>
    </tool>
    
</toolbox>
分享到:
评论

相关推荐

    vilocity简单例子

    在这个模板中,`${page.title}`、`${user.name}`和`${lastLogin}`是来自数据模型的变量,而`${date.format}`是使用了我们在`toolbox.xml`中配置的DateTool。 总结起来,这个"vilocity简单例子"涵盖了Vilocity的基本...

    velocity入门使用教程

    在velocity.properties文件中,可以指定toolbox.xml文件,该文件用于定义工具箱,工具箱中可以包含自定义的Velocity工具(Macro)和函数(Function)。这些工具和函数可以在模板中直接调用,简化模板编写。 通过...

    如何解决SpringBoot2.x版本对Velocity模板不支持的方案

    spring.velocity.toolbox-config-location=/WEB-INF/toolbox.xml ``` 最后,我们需要在 spring.xml 文件中添加以下视图解析配置: ```xml &lt;!-- 设置视图解析工具 --&gt; &lt;bean id="velocityViewResolver" class="org....

    Velocity语法以及整合struts2总结

    1. **配置Struts2**:在`struts.xml`配置文件中,需要指定`struts.velocity.toolboxlocation`常量,指向Velocity的工具箱配置文件,如`/WEB-INF/toolbox.xml`。 2. **配置Action结果**:在Action的配置中,设置`...

    Struts课堂笔记.rar--struts2的struts.properties配置文件详解

    struts.configuration.xml.reload Whether to reload the XML configuration or not 是否加载xml配置(true,false) struts.continuations.package The package containing actions that use Rife continuations ...

    velocity例子

    1. **模板语言**:Velocity使用自己的模板语言(VTL,Velocity Template Language),这是一种简单但强大的语法,允许在HTML或XML文档中嵌入动态内容。例如,`#set($name = "World")` 和 `Hello, ${name}!` 分别用于...

    Struts2属性文件详解

    #### struts.configuration.xml.reload 该属性设定了当`struts.xml`文件发生改变后,系统是否自动重新加载该文件,默认值为`false`。 #### struts.velocity.configfile 指定了Velocity框架所需的`velocity....

    Freemarker使用指南

    在实际应用中,由于Velocity需要自定义toolbox类和编写通用模板代码,而Freemarker则相对更易于使用。 Freemarker的工作原理可概括为:将页面样式设计放在FreeMarker模板文件中,然后将动态数据以键值对的形式放入...

    Turbine Tutorial

    作者 Henning Schmiedehausen(henning@apache.org)自 2001 年起就开始使用 Turbine,并在 2002 年成为该项目的提交者,同时也是 Apache 软件基金会成员。通过本教程的学习,读者将能够掌握 Turbine 的基础知识,...

Global site tag (gtag.js) - Google Analytics