码迷,mamicode.com
首页 > 其他好文 > 详细

velcoity使用说明:foreach指令

时间:2014-11-06 16:58:27      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   使用   java   

http://blog.csdn.net/madding/article/details/6641020当在velocity中需要显示一个列表信息,我们会用foreach循环输出,

要求:

假如现在需要在页面中输出单数的内容背景为红,双数的内容为黑,构造方式如下:

  1. package org.apache.velocity.test.issues;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.apache.velocity.test.BaseTestCase;  
  7. /** 
  8.  * 测试foreach 
  9.  * @author madding.lip at 2011.07.28 
  10.  */  
  11. public class MaddingTestForeach extends BaseTestCase {  
  12.   
  13.     public MaddingTestForeach(String name) {  
  14.         super(name);  
  15.     }  
  16.   
  17.     public void test() {  
  18.         List<String> list = new ArrayList<String>();  
  19.           
  20.         for(int i = 1; i <= 100; i++) {  
  21.             list.add(String.valueOf(i));  
  22.         }  
  23.         context.put("features", list);  
  24.           
  25.         String template =   
  26.             "#foreach ($feature in $features)" +  
  27.                 "#if($velocityCount%2 == 1)" +  
  28.                     "<font color=\"red\">$feature</font>" +  
  29.                 "#elseif($velocityCount%2 == 0)" +  
  30.                     "<font color=\"black\">$feature</font>" +  
  31.                 "#end" +  
  32.                 "#if($velocityHasNext)" +  
  33.                 "|" +  
  34.                 "#end" +  
  35.             "#end";  
  36.           
  37.         System.out.println(evaluate(template));  
  38.           
  39.     }  
  40. }  

 

BaseTestCase是Velocity源代码中的测试类

 

说明:

1.#foreach是velocity指令,

2.velcotiyCount上, velocity foreach定义的一个变量,该变量主要用来记录当前的循环次数

3.velocityHasNext, velocity foreach定义的一个变量 ,表明该循环当前是否到尾部了

 

velocity.properties:

 

  1. # ----------------------------------------------------------------------------  
  2. # F O R E A C H  P R O P E R T I E S  
  3. # ----------------------------------------------------------------------------  
  4. # These properties control how the counter is accessed in the #foreach  
  5. # directive. By default the reference $velocityCount and $velocityHasNext  
  6. # will be available in the body of the #foreach directive.  
  7. # The default starting value for $velocityCount is 1.  
  8. # ----------------------------------------------------------------------------  
  9.   
  10. directive.foreach.counter.name = velocityCount  
  11. directive.foreach.counter.initial.value = 1  
  12. directive.foreach.maxloops = -1  
  13.   
  14. directive.foreach.iterator.name = velocityHasNext  



 

实战记录

有一种情况就是在一个foreach下遍历两个list,那么肯定要是for(int i=0;i<...)这种数字计数形式。java中很好实现,velocity说是有个$velocityCount,但我发现不好用。它的值是从1开始的,而取list的第一个对象list.get(0),所以我用计算方式($velocityCount-1)不行,好些无效,配置$velocityCount初始值为0,配置方式也无效
msgMap.put("directive.foreach.counter.initial.value", 0);
msgMap.put("insuranceCountList", insuranceCountList);
 
没办法了,我就再做一个数字数组或List,里面存储我需要的数字,专用来做迭代,比如手动做的list为insuranceCountList
          <AA_INSURANCELIST>
             ## AA_INSURANCE信息
             #foreach ($count in $insuranceCountList)
           <AA_INSURANCE>
             <NAME> $!{app.getComponent($insured).get($count).getCInsuredNme()} </NAME>
             <AA_PLYNO> $!{app.getComponent($base).get($count).getCPlyNo()}</AA_PLYNO >
           </AA_INSURANCE>
           #end
         </AA_INSURANCELIST>
 
注意的$velocityCount可以不用注入,velocity foreach中直接可使用,最后给一个实战配置模版例子
REQUEST_09.xml
bubuko.com,布布扣
<?xml version="1.0" encoding="GBK"?>
<PACKET type="REQUEST" version="1.0">

    #set($base="PlyMain")
    #set($insured="Insured")
    #set($contactInfo="ContactInfo")
    
    <HEAD>
        <REQUEST_TYPE>$!{REQUEST_TYPE}</REQUEST_TYPE>
        <USER>$!{USER}</USER>
        <PASSWORD>$!{PASSWORD}</PASSWORD>
    </HEAD>
     <BODY>
       <VOUCHER_FLAG>$!{app.getComponent($contactInfo).get(0).getCVoucherFlag()}</VOUCHER_FLAG>
       <AA_INSURANCELIST>
            ## AA_INSURANCE信息
         #foreach ($count in $AA_INSURANCECountList)
         <AA_INSURANCE>
           <NAME>$!{app.getComponent($insured).get($count).getCInsuredNme()}</NAME>
           <DD_PLYNO>$!{app.getComponent($base).get($count).getCDD_PLYNO()}</DD_PLYNO>
         </AA_INSURANCE>
         #end
       </AA_INSURANCELIST>
     </BODY>
</PACKET>
View Code

 

bubuko.com,布布扣

velcoity使用说明:foreach指令

标签:style   blog   http   io   color   ar   os   使用   java   

原文地址:http://www.cnblogs.com/svennee/p/4078974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!