在velocity使用toolbox.xml

前端之家收集整理的这篇文章主要介绍了在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>
原文链接:https://www.f2er.com/xml/294280.html

猜你在找的XML相关文章