作者:mobiledu2502861323 | 来源:互联网 | 2024-10-19 07:38
文章目录1.注释2.定义变量3.条件语句4.循环标签5.Velocity中的宏6.debug语法标签7.不存在的变量显示为空白8.本地文件引入9.调用类方法1.注释单行注释:#
文章目录 1. 注释 2. 定义变量 3. 条件语句 4. 循环标签 5. Velocity 中的宏 6. debug语法标签 7. 不存在的变量显示为空白 8. 本地文件引入 9. 调用类方法
1. 注释 单行注释 :
## 内容
多行注释:
#* 内容 * #
文档注释:
#* * 内容* #
2. 定义变量 可以使用#set
来定义变量 ,举个例子:
① 模板内容:
#set ( $word= "zhuoqianmingyue" ) ${ word} #$word #set ( $surname= "Lee" ) #set ( $name= "junkui" ) #set ( $fullname= "$surname $name" ) ${ fullname}
② 单元测试方法:
public void set ( ) { String templatePath = "cn/lijunkui/api/set.vm" ; String htmlPath = "D:\\set.html" ; test ( templatePath, htmlPath) ; }
③ 测试类
private void test ( String templatePath, String htmlPath) { VelocityEngine ve= new VelocityEngine ( ) ; ve. setProperty ( Velocity . RESOURCE_LOADER, "class" ) ; ve. setProperty ( "class.resource.loader.class" , "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" ) ; try { ve. init ( ) ; Template t= ve. getTemplate ( templatePath, "utf-8" ) ; VelocityContext context = new VelocityContext ( ) ; PrintWriter writer = new PrintWriter ( htmlPath) ; t. merge ( context, writer) ; writer. close ( ) ; } catch ( Exception e) { e. printStackTrace ( ) ; } }
3. 条件语句 语法:
#if #elseif #else #end
下面为模板内容示例:
#set ( $number = 1 ) #if ( $number == 1 ) 这个数是 1 #elseif ( $number == 2 ) 这个数是 2 #else 这个数是3 #end
4. 循环标签 循环标签可以使用#foreach
举例:
#foreach ( $item in [ 1. .5 ] ) $item #end-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - #set ( $arr = [ 0. .1 ] ) #foreach ( $x in $arr ) $x #end-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - #set ( $arr2 = [ 0. .1 ] ) #set ( $k = 1 ) #foreach ( $x in $arr2 ) x: $x, k: $k #set ( $k = $k+ 1 ) #end-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
5. Velocity 中的宏 举例:
#macro ( html) < h2> 这是一个宏< / h2> #end #macro ( htmlContent $content) < h2> $content< / h2> #end #html ( ) #htmlContent ( "有参数的宏" )
6. debug语法标签 debug
语法标签可以使用#stop
&#xff0c;举例&#xff1a;
stop前的内容 #stop stop后的内容
7. 不存在的变量显示为空白 为了把不存在的变量或变量值为null
的对象$msg
显示为空白&#xff0c;则只需要在变量名前加一个“!”
号即可&#xff0c;举例&#xff1a;
#set ( $word&#61; "zhuoqianmingyue" ) 存在的内容&#xff1a;${ word} # 不存在的内容&#xff1a;${ word1} 不存在的内容不显示&#xff1a;$! { word1} < br> -- -- -- -- -- -- -- -- -- -- -- -- -- - < br> #if ( $! { word1} ) 存在#else 不存在 #end
8. 本地文件引入 #include
: 可以引入多个文件&#xff0c;被引入文件的内容将不会通过模板引擎解析 #parse
&#xff1a; 只能引入单个文件&#xff0c;引入的文件内容Velocity
将解析其中的velocity
语法并移交给模板 举例&#xff1a;
#include ( "abc.txt" , "cde.txt" )
#parse ( "parent.vm" )
9. 调用类方法 举例&#xff1a;
① 定义一个String
工具类 将小写内容转换为大写&#xff1a;
package cn. lijunkui. api ; public class StringUtils { public static String toUpper ( String word) { return word. toUpperCase ( ) ; } }
② 编写模板引擎初始化并将工具类的实例对象加载进模板的上下文中&#xff1a;
public void initWihtFun ( String templatePath, String htmlPath) { VelocityEngine ve&#61; new VelocityEngine ( ) ; ve. setProperty ( Velocity . RESOURCE_LOADER, "class" ) ; ve. setProperty ( "class.resource.loader.class" , "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" ) ; ve. setProperty ( Velocity . FILE_RESOURCE_LOADER_PATH, "D:\\workspace-sts-3.9.5.RELEASE\\velocitylearn\\target\\velocitylearn\\WEB-INF\\classes\\cn\\lijunkui\\api" ) ; try { ve. init ( ) ; Template t&#61; ve. getTemplate ( templatePath, "utf-8" ) ; VelocityContext context &#61; new VelocityContext ( ) ; StringUtils stringUtils &#61; new StringUtils ( ) ; context. put ( "stringUtils" , stringUtils) ; PrintWriter writer &#61; new PrintWriter ( htmlPath) ; t. merge ( context, writer) ; writer. close ( ) ; } catch ( Exception e) { e. printStackTrace ( ) ; } }
③ 编写模板内容&#xff1a;
#set ( $word&#61; "zhuoqianmingyue" ) $stringUtils. toUpper ( "abc" ) $stringUtils. toUpper ( ${ word} )
④ 测试用例&#xff1a;
public void useFun ( ) { String templatePath &#61; "cn/lijunkui/api/useFun.vm" ; String htmlPath &#61; "D:\\useFun.html" ; initWihtFun ( templatePath, htmlPath) ; }