【问题标题】:Velocity template formatting issue速度模板格式问题
【发布时间】:2015-03-31 10:03:59
【问题描述】:

marge 模板后,我的速度模板中有几个 #foreach #if 组合我面临以下格式问题:

模板:

#if ($rq.allowanceType == "TAXI")
#foreach($i in [1..50])
#set($test = "$rq.subType$i")
#if($render.eval($ctx, "$rq.subType$i") != "" && $test != $render.eval($ctx, "$rq.subType$i"))      
    <aps:lineItem>
    <aps:details>
    <aps:type>$render.eval($ctx, "$rq.type$i")</aps:type>
    <aps:billNumber>$render.eval($ctx, "$rq.billNumber$i")</aps:billNumber>
    <aps:isReceived>$render.eval($ctx, "$rq.valid$i")</aps:isReceived>
    #if($render.eval($ctx, "$rq.valid$i") == "YES")
    <aps:FromDate>$render.eval($ctx, "$rq.FromDate$i")</aps:FromDate>
    <aps:ToDate>$render.eval($ctx, "$rq.ToDate$i")</aps:ToDate>#end#if($render.eval($ctx, "$rq.Amount$i") != "")<aps:Amount>$render.eval($ctx, "$rq.Amount$i")</aps:Amount>#end#if($render.eval($ctx, "$rq.VatAmount$i") != "")<aps:VatAmount>$render.eval($ctx, "$rq.VatAmount$i")</aps:VatAmount>
    #end        
    <aps:GrossAmount>$render.eval($ctx, "$rq.GrossAmount$i")</aps:GrossAmount>
    </aps:details>
    </aps:lineItem>
#end
#end 
#end

格式化为

    <aps:lineItem>
    <aps:details>
    <aps:type>FRAMES</aps:type>
    <aps:billNumber>695</aps:billNumber>
    <aps:isReceived>YES</aps:isReceived>
    <aps:FromDate>01/02/1993</aps:FromDate>
    <aps:ToDate>01/02/1994</aps:ToDate> <aps:GrossAmount>3000</aps:GrossAmount>
    </aps:details>
    </aps:lineItem>
            <aps:lineItem>
    <aps:details>
    <aps:type>TEST</aps:type>
    <aps:billNumber>695</aps:billNumber>
    <aps:isReceived>NA</aps:isReceived>
        <aps:GrossAmount>3000</aps:GrossAmount>
    </aps:details>
    </aps:lineItem>

在速度模板中添加条件和循环时我们应该注意什么,以免造成格式问题?

【问题讨论】:

    标签: velocity


    【解决方案1】:

    不幸的是,速度并不容易让输出的格式保持漂亮和干净,同时拥有漂亮的模板。

    一个简单的解决方法是不关心速度的输出。在您的情况下,由于您正在生成 xml,因此在速度创建文件后应用 xml-beautifier,例如您可以使用 xmlbeans 中的一个,它可以很容易地以编程方式使用:

       import org.apache.xmlbeans.XmlObject;
       import org.apache.xmlbeans.XmlOptions;
    
       public static void beautify(File xmlFile) {
          XmlOptions options = new XmlOptions();
          options.setLoadLineNumbers();
          XmlObject doc = XmlObject.Factory.parse(xmlFile, options);
          options = new XmlOptions();
          options.setSavePrettyPrint();
          options.setSavePrettyPrintIndent(4);
          doc.save(xmlFile, options);
       }
    

    【讨论】:

      【解决方案2】:

      您可以尝试使用快速模板来获得 XML/JSON 模板功能

      提供比 apache velocity 更好的功能

      IEngine templateEngine=EngineFactory.getInstance().getEngine(EngineType.TEMPLATES);
      
      templateEngine.initialize("/com/tester/template-rules-config.xml");
      
      long startTime=System.currentTimeMillis();
      
      EngineResponse response=templateEngine.execute(context);
      

      Quick-Templates Framework

      【讨论】:

        猜你喜欢
        • 2023-03-03
        • 1970-01-01
        • 2010-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多