欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【原创】velocity实用语法详解

程序员文章站 2022-07-13 23:42:40
...



    注:  语法中相关基础语法请查阅百度获取,此文章仅针对应用性讲解。

 

   一、注释,在页面中不做任何显示,包括查看源代码

          1.单行注解

              eg: ## 这是一个单行注解

          2.多行注解

              eg:
                   #* 
                      这是一个多行注解 
                   *#

 

  二、#set,当前页面赋值不做演示,仅演示后台传值

 前提:1.Controller传值 modelAndView.addObject("query", testBean);-----定义参数名query

               的testBean对象传于前端。

                   2.testBean结构(详细代码省略)

                      private Map<String,String> criteria;   ----- 属性

                   3. 赋值情况(详细代码省略)

                      criteria.put("name","xiaoming");   --- name赋值xiaoming
                      criteria.put("address","北京市");  --- address赋值北京市

        eg:

         -------代码-------

           #set( $result = $query.criteria.get("name") )       --- 获取name值,存在
           The result of the first query is $!result                  

           <br>
           #set( $result = $query.criteria.get("address") )   --- 获取address值,存在
           The result of the second query is $!result
           <br>
           #set( $result = $query.criteria.get("nothing") )      --- 获取nothing值,不存在
           The result of the third query is $!{result} ==$!result  

           <br>

           #set( $resultOther = $query.criteria.get("nothing") )  --- 无叹号演示
           The result of the fourth query is $resultOther
           <br>
-------页面展示结果-------:

         The result of the first query is xiaoming 
           The result of the second query is 北京市 
           The result of the third query is 北京市         ---未获取到nothing值,赋值失败,调用原来值

           The result of the fourth query is $resultOther

 

        注:   1.$!result ---- $result---- 叹号使用,若值不存在的返回结果""  ---- "$result";

                  2.$!{result} ==$!result  语句一样

                  3.引用参数查找顺序

                      $customer.address 引用时,查找顺序是:
                      1. getaddress()
                      2. getAddress()
                      3. get(“address”)
                      4. isAddress()
                   对于VTL中大写的属性名Address 引用,将是:
                      1. getAddress()
                      2. getaddress()
                      3. get(“Address”)
                      4. isAddress()

 

 

  三、#literal ,直接输出文字 不执行内部代码

        eg:

         -------代码-------

 

 #literal()
     #foreach ($woogie in $boogie)<br>
           &nbsp;&nbsp;nothing will happen to $woogie<br>
     #end

#end

        -------页面展示结果-------:

 #foreach ($woogie in $boogie)
          &nbsp;&nbsp;nothing will happen to $woogie
 #end

 

  四、循环(包含#foreach,#if,数组等知识点用法 )

 

前提:1. Controller传值 modelAndView.addObject("boogie", listTestBean);定义参数名boogie

               的listTestBean集合传于前端。

                  2. testBean结构----- 属性(详细代码省略)

                      private Map<String,String> criteria;
                      private String woogie;
                      private String[] shuzu_s;                    

                  3. 赋值情况(详细代码省略)

                     List<TestBean> listTestBean=new ArrayList<TestBean>();
                     for (int i = 0; i < 5; i++) {
                           TestBean testBean =new TestBean();
                            testBean.setWoogie("循环第"+i+"个");
                            String[] shuzu ={ i+"数组_1",i+"数组_2"};
                            testBean.setShuzu_s(shuzu);
                            listTestBean.add(testBean);
                    } 

        eg:

         -------代码-------

                    #foreach ($woogie in $boogie)           ##------ 循环1 ------
                           #if($velocityCount!=2)          ---- if演示 $velocityCount:获取当前索引
                                $velocityCount/$boogie.size()&nbsp;&nbsp;nothing will happen

                                to $!{woogie.woogie}==$!woogie.woogie
                                <br>
                                &nbsp;&nbsp;其中当前对象的数组为:
                                #foreach ($shuzu in $woogie.shuzu_s)
                                      ${velocityCount}、$shuzu
                                       #if($velocityCount!=$woogie.shuzu_s.size())        

                                        ,        ##------ 如果不是最后一个,添加逗号 ------

                                       #end
                                #end
                                <br>
                            #end
                      #end

       ------页面展示结果-------:
                     1/5  nothing will happen to 循环第0个==循环第0个
                        其中当前对象的数组为: 1、0数组_1 , 2、0数组_2
                     3/5  nothing will happen to 循环第2个==循环第2个
                        其中当前对象的数组为: 1、2数组_1 , 2、2数组_2
                     4/5  nothing will happen to 循环第3个==循环第3个 
                        其中当前对象的数组为: 1、3数组_1 , 2、3数组_2
                     5/5  nothing will happen to 循环第4个==循环第4个
                        其中当前对象的数组为: 1、4数组_1 , 2、4数组_2 

-------代码------- 

#foreach( $foo in [1..5] )                ##------ 循环2 ------
 $     foo<br>
#end

 -    ------页面展示结果-------:

1
2
3
4
5

  五、#macro ---宏定义(网上查阅,建议尽量少用)

1、定义:
      #macro (<宏的名称> [$参数1] [参数2] ...)
          ##statement segment
      #end
2、调用:
#<宏的名称>([<参数1>] [<参数2>] ...)

 

-------代码------- 

#macro (tableRow $color $tdCotent)
    <tr  style='background-color: $color;'><td>$tdCotent</td></tr>
#end
<table>
    #tableRow("red","第一个")
    #tableRow("green","第二个")
</table>

 

 ------页面展示结果-------:

 

【原创】velocity实用语法详解
            
    
    博客分类: 前端java velocity实用语法前端 
 

  • 【原创】velocity实用语法详解
            
    
    博客分类: 前端java velocity实用语法前端 
  • 大小: 602 Bytes