分享

在velocity使用toolbox.xml

 真爱图书 2014-04-01
首先必须在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典型配置:
Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!--   
  3. =============================================================   
  4.   @(#) toolbox.xml   
  5.         
  6.   Copyright (c) 2005, HOBOKEN Project, All Rights Reserved.   
  7. =============================================================   
  8. -->   
  9.   
  10. <toolbox>   
  11.   
  12.     <!-- [ DateTool ]   
  13.          @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/generic/DateTool.html  (ja)   
  14.         @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/DateTool.html  (en)   
  15.         @since VelocityTools 1.0  
  16.     -->   
  17.     <tool>   
  18.         <key>date</key>   
  19.         <scope>application</scope>   
  20.         <class>org.apache.velocity.tools.generic.DateTool</class>   
  21.     </tool>   
  22.        
  23.     <!-- [ MathTool ]   
  24.         @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/generic/MathTool.html  (ja)   
  25.         @see http://velocity.apache.org/tools/devel/generic/MathTool.html  (en)   
  26.         @since VelocityTools 1.0  
  27.     -->   
  28.     <tool>   
  29.         <key>math</key>   
  30.         <scope>application</scope>   
  31.         <class>org.apache.velocity.tools.generic.MathTool</class>   
  32.     </tool>   
  33.        
  34.     <!-- [ NumberTool ]   
  35.         @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/NumberTool.html  (en)   
  36.         @since VelocityTools 1.2  
  37.     -->   
  38.     <tool>   
  39.         <key>number</key>   
  40.         <scope>application</scope>   
  41.         <class>org.apache.velocity.tools.generic.NumberTool</class>   
  42.     </tool>   
  43.        
  44.     <!-- [ RenderTool ]   
  45.         @see http://velocity.apache.org/tools/devel/generic/RenderTool.html  (en)   
  46.         @since VelocityTools 1.0  
  47.        
  48.     <tool>   
  49.         <key>render</key>   
  50.         <scope>application</scope>   
  51.         <class>org.apache.velocity.tools.generic.RenderTool</class>   
  52.     </tool>   
  53.     -->   
  54.        
  55.     <!-- [ EscapeTool ]   
  56.         @see http://velocity.apache.org/tools/devel/generic/EscapeTool.html  (en)   
  57.         @since VelocityTools 1.2  
  58.     -->   
  59.     <tool>   
  60.         <key>esc</key>   
  61.         <scope>application</scope>   
  62.         <class>org.apache.velocity.tools.generic.EscapeTool</class>   
  63.     </tool>   
  64.        
  65.     <!-- [ ResourceTool ]   
  66.                @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/ResourceTool.html  (en)   
  67.         @since Velocity 1.3  
  68.     <tool>   
  69.         <key>text</key>   
  70.         <class>org.apache.velocity.tools.generic.ResourceTool</class>   
  71.         <parameter name="bundles" value="resources,prj.hoboken.patrasche.resources.PatrascheResources" />   
  72.         <parameter name="locale" value="ja_JP" />   
  73.     </tool>   
  74.     -->   
  75.        
  76.     <!-- [ AlternatorTool ]   
  77.   
  78.         @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/AlternatorTool.html  (en)   
  79.         @since VelocityTools 1.2  
  80.     -->   
  81.     <tool>   
  82.         <key>alternator</key>   
  83.         <scope>application</scope>   
  84.         <class>org.apache.velocity.tools.generic.AlternatorTool</class>   
  85.     </tool>   
  86.        
  87.     <!-- [ ValueParser ]   
  88.   
  89.         @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/ValueParser.html  (en)   
  90.         @since VelocityTools 1.2  
  91.     -->   
  92.     <tool>   
  93.         <key>parser</key>   
  94.         <scope>application</scope>   
  95.         <class>org.apache.velocity.tools.generic.ValueParser</class>   
  96.     </tool>   
  97.        
  98.     <!-- [ ListTool ]   
  99.   
  100.         @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/ListTool.html  (en)   
  101.         @since VelocityTools 1.2  
  102.     -->   
  103.     <tool>   
  104.         <key>list</key>   
  105.         <scope>application</scope>   
  106.         <class>org.apache.velocity.tools.generic.ListTool</class>   
  107.     </tool>   
  108.        
  109.     <!-- [ SortTool ]   
  110.   
  111.         @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/SortTool.html  (en)   
  112.         @since VelocityTools 1.2  
  113.     -->   
  114.     <tool>   
  115.         <key>sorter</key>   
  116.         <scope>application</scope>   
  117.         <class>org.apache.velocity.tools.generic.SortTool</class>   
  118.     </tool>   
  119.        
  120.     <!-- [ IteratorTool ]   
  121.         @see http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/generic/IteratorTool.html  (en)   
  122.         @since VelocityTools 1.0  
  123.     -->   
  124.     <tool>   
  125.         <key>mill</key>   
  126.         <scope>request</scope>   
  127.         <class>org.apache.velocity.tools.generic.IteratorTool</class>   
  128.     </tool>   
  129.            
  130. <!--   
  131. ============================================================   
  132.   [ TOOL FOR STRUTS TAGLIB ]   
  133. ============================================================   
  134. -->   
  135.     <!-- [ ActionMessagesTool ]            
  136.         @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/ActionMessagesTool.html  (ja)   
  137.         @see http://velocity.apache.org/tools/devel/struts/ActionMessagesTool.html  (en)   
  138.         @since VelocityTools 1.1  
  139.     -->   
  140.     <tool>   
  141.         <key>messages</key>   
  142.         <scope>request</scope>   
  143.         <class>org.apache.velocity.tools.struts.ActionMessagesTool</class>   
  144.     </tool>   
  145.        
  146.     <!-- [ ErrorsTool ]   
  147.         @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/ErrorsTool.html  (ja)   
  148.         @see http://velocity.apache.org/tools/devel/struts/ErrorsTool.html  (en)   
  149.         @since VelocityTools 1.0  
  150.     -->   
  151.     <tool>   
  152.         <key>errors</key>   
  153.         <scope>request</scope>   
  154.         <class>org.apache.velocity.tools.struts.ErrorsTool</class>   
  155.     </tool>   
  156.        
  157.     <!-- [ FormTool ]   
  158.         @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/FormTool.html (ja)   
  159.         @see http://velocity.apache.org/tools/devel/struts/FormTool.html (en)   
  160.         @since VelocityTools 1.0  
  161.     -->   
  162.     <tool>   
  163.         <key>form</key>   
  164.         <scope>request</scope>   
  165.         <class>org.apache.velocity.tools.struts.FormTool</class>   
  166.     </tool>   
  167.        
  168.     <!-- [ MessageTool ]   
  169.          @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/MessageTool.html (ja)   
  170.         @see http://velocity.apache.org/tools/devel/struts/MessageTool.html  (en)   
  171.         @since VelocityTools 1.0  
  172.     -->   
  173.     <tool>   
  174.         <key>resource</key>   
  175.         <scope>request</scope>   
  176.         <class>org.apache.velocity.tools.struts.MessageTool</class>   
  177.     </tool>   
  178.        
  179.     <!-- [ StrutsLinkTool ]   
  180.         LinkTool            
  181.         @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/StrutsLinkTool.html  (ja)   
  182.         @see http://velocity.apache.org/tools/devel/struts/StrutsLinkTool.html  (en)   
  183.         @since VelocityTools 1.0  
  184.     -->   
  185.     <tool>   
  186.         <key>slink</key>   
  187.         <scope>request</scope>   
  188.         <class>org.apache.velocity.tools.struts.StrutsLinkTool</class>   
  189.     </tool>   
  190.        
  191.     <!-- [ SecureLinkTool ]   
  192.         LinkTool            
  193.         @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/SecureLinkTool.html  (ja)   
  194.         @see http://velocity.apache.org/tools/devel/struts/SecureLinkTool.html  (en)   
  195.         @since VelocityTools 1.1  
  196.     -->   
  197.     <tool>   
  198.         <key>sslink</key>   
  199.         <scope>request</scope>   
  200.         <class>org.apache.velocity.tools.struts.SecureLinkTool</class>   
  201.     </tool>   
  202.        
  203.     <!-- [ TilesTool ]   
  204.         Tiles            
  205.         @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/TilesTool.html  (ja)   
  206.         @see http://velocity.apache.org/tools/devel/struts/TilesTool.html  (en)   
  207.         @since VelocityTools 1.1  
  208.     -->   
  209.     <tool>   
  210.         <key>tiles</key>   
  211.         <scope>request</scope>   
  212.         <class>org.apache.velocity.tools.struts.TilesTool</class>   
  213.     </tool>   
  214.        
  215.     <!-- [ ValidatorTool ]   
  216.         Validator            
  217.         @see http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs-ja/struts/ValidatorTool.html  (ja)   
  218.         @see http://velocity.apache.org/tools/devel/struts/ValidatorTool.html  (en)   
  219.         @since VelocityTools 1.1  
  220.     -->   
  221.     <tool>   
  222.         <key>validator</key>   
  223.         <scope>request</scope>   
  224.         <class>org.apache.velocity.tools.struts.ValidatorTool</class>   
  225.     </tool>   
  226.   
  227.   
  228.     <data type="string">   
  229.         <scope>request</scope>   
  230.         <key>app_version</key><value>3.0</value>   
  231.     </data>   
  232.     <data type="string">   
  233.         <scope>request</scope>   
  234.         <key>app_name</key><value>Patrasche</value>   
  235.     </data>   
  236.        
  237.     <data type="string">   
  238.         <scope>request</scope>   
  239.         <key>next</key><value>/images/icon/next.jpg</value>   
  240.     </data>   
  241.     <data type="string">   
  242.         <scope>request</scope>   
  243.         <key>prev</key><value>/images/icon/prev.jpg</value>   
  244.     </data>   
  245.     <data type="string">   
  246.         <scope>request</scope>   
  247.         <key>top-page</key><value>/images/icon/top-page.jpg</value>   
  248.     </data>   
  249.     <data type="string">   
  250.         <scope>request</scope>   
  251.         <key>last-page</key><value>/images/icon/last-page.jpg</value>   
  252.     </data>   
  253.     <data type="string">   
  254.         <scope>request</scope>   
  255.         <key>pageIcon</key><value>/images/icon/page.png</value>   
  256.     </data>   
  257.     <data type="string">   
  258.         <scope>request</scope>   
  259.         <key>bookIcon</key><value>/images/icon/book.png</value>   
  260.     </data>   
  261.     <data type="string">   
  262.         <scope>request</scope>   
  263.         <key>defaultStyle</key><value>./web/default.css</value>   
  264.     </data>   
  265.     <data type="string">   
  266.         <scope>request</scope>   
  267.        <key>errorMetaHeader</key><value>/error/default_header.vm</value>   
  268.     </data>   
  269.     <data type="string">   
  270.         <scope>request</scope>   
  271.         <key>errorStyle</key><value>/error/error.css</value>   
  272.     </data>   
  273.     <data type="string">   
  274.         <scope>request</scope>   
  275.         <key>detailOfHTTP</key><value>./error/detail_HttpError.vm</value>   
  276.     </data>   
  277.     <data type="string">   
  278.         <scope>request</scope>   
  279.         <key>detailOfSERV</key><value>./error/detail_ServerError.vm</value>   
  280.     </data>   
  281.     <data type="string">   
  282.         <scope>request</scope>   
  283.         <key>multiPart</key><value>enctype='multipart/form-data'</value>   
  284.     </data>   
  285.     <data type="string">   
  286.         <scope>request</scope>   
  287.         <key>parentMark</key><value>..</value>   
  288.     </data>   
  289.     <data type="string">   
  290.         <scope>request</scope>   
  291.         <key>chartToolTips</key><value>toolTips</value>   
  292.     </data>   
  293.     <data type="string">   
  294.         <scope>request</scope>   
  295.         <key>chartClickable</key><value>isClickable</value>   
  296.     </data>   
  297.        
  298.     <!-- [ NullTool ]   
  299.     -->   
  300.     <tool>   
  301.         <key>null</key>   
  302.         <scope>application</scope>   
  303.         <class>com.company.util.velocity.NullTool</class>   
  304.     </tool>   
  305.   
  306.     <!-- [ StringTool ]   
  307.         文字操作           
  308.     -->   
  309.     <tool>   
  310.         <key>words</key>   
  311.         <scope>application</scope>   
  312.         <class>com.company.util.velocity.StringTool</class>   
  313.     </tool>   
  314.        
  315.     <!-- [ PageScrolllTool ]   
  316.                    
  317.     -->   
  318.     <tool>   
  319.         <key>pager</key>   
  320.         <scope>session</scope>   
  321.         <class>com.company.util.velocity.PageScrollTool</class>   
  322.     </tool>   
  323.   
  324.     <!-- [ LoginUserTool ]   
  325.     -->   
  326.     <tool>   
  327.         <key>loginuser</key>   
  328.         <scope>request</scope>   
  329.         <class>com.company.util.velocity.LoginUserTool</class>   
  330.     </tool>   
  331.   
  332.     <!-- [ MenuTool ]   
  333.     -->   
  334.     <tool>   
  335.         <key>menuviewer</key>   
  336.         <scope>request</scope>   
  337.         <class>com.company.util.velocity.MenuTool</class>   
  338.     </tool>   
  339.        
  340.     <!-- [ FileSystemTool ]   
  341.            
  342.     -->   
  343.     <tool>   
  344.         <key>filesys</key>   
  345.         <scope>request</scope>   
  346.        <class>com.company.util.velocity.contrib.FileSystemTool</class>   
  347.     </tool>   
  348.        
  349.     <!-- [ ExceptionTool ]           
  350.            
  351.     -->   
  352.     <tool>   
  353.         <key>exception</key>   
  354.         <scope>request</scope>   
  355.         <class>com.company.util.velocity.ExceptionTool</class>   
  356.     </tool>   
  357.        
  358.     <!-- [ POITool ]                 
  359.         使用自己写的操作Excel的类   
  360.     -->   
  361.     <tool>   
  362.         <key>poi</key>   
  363.         <scope>request</scope>   
  364.         <class>com.company.util.velocity.POITool</class>   
  365.     </tool>   
  366.   
  367.     <!-- [ ConstantTool ]   
  368.         使用自己定义的常量。   
  369.     -->   
  370.     <tool>   
  371.         <key>constant</key>   
  372.         <scope>application</scope>   
  373.         <class>com.company.Constant</class>   
  374.     </tool>   
  375.        
  376. </toolbox>  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多