首先必須在web.xml中添加: <init-param> <param-name>org.apache.velocity.toolbox</param-name> <param-value>/WEB-INF/toolbox.xml</param-value>
</init-param> 才能夠使用toolbox.xml中定義的變量。css
舉例說明用法 好比我要使用日期,能夠以下定義: <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')html
若是咱們要使用本身的類,只須要把org.apache.velocity.tools.generic.DateTool這部分替換成本身定義的類,而後再初始化該類的實例,好比<key>class</key> 在.vm中這樣用$class.getXXX()java
toolbox.xml典型配置: Java代碼 收藏代碼web
<?xml version="1.0" encoding="UTF-8"?> apache
<!-- ============================================================= @(#) toolbox.xml Copyright (c) 2005, HOBOKEN Project, All Rights Reserved. ============================================================= --> session
<toolbox> app
<!-- [ 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> code