日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java velocity 语法_Velocity语法

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java velocity 语法_Velocity语法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.變量

(1)變量的定義:

#set($name = "hello")說明:velocity中變量是弱類型的。

當使用#set指令時,括在雙引號中的字面字符串將解析和重新解釋,如下所示:

#set($directoryRoot = "www" )

#set($templateName = "index.vm" )

#set($template = "$directoryRoot/$templateName" )

$template

輸出將會是:www/index.vm

注:在velocity中使用$2.5這樣的貨幣標識是沒有問題得的,因為velocity中的變量總是以一個大寫或者小寫的字母開始的。

(2)變量規范的寫法

${name} ,也可以寫成:$name。提倡用前面的寫法。

例如:你希望通過一個變量$vice來動態的組織一個字符串。

Jack?is?a?$vicemaniac.

本來變量是$vice現在卻變成了$vicemaniac,這樣Veloctiy就不知道您到底要什么了。所以,應該使用規范的格式書寫?:?Jack?is?a?${vice}maniac

現在Velocity知道變量是$vice而不是$vicemaniac。

注意:當引用屬性的時候不能加{}

(3)變量的賦值:

$name="hello"

賦值的左邊必須是一個變量或者是屬性引用。右邊可以是下面六種類型之一:

變量引用,字面字符串,屬性引用,方法引用,字面數字,數組列表。

下面的例子演示了上述的每種類型:

#set( $monkey = $bill ) ## variable reference

#set( $monkey.Friend = "monica" ) ## string

#set( $monkey.Blame = $whitehouse.Leak ) ## property reference

#set( $monkey.Plan = $spindoctor.weave($web) ) ## method reference

#set( $monkey.Number = 123 ) ##number

#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList

注意:①如果上述例子中的右值是null, 則左值不會被賦值,也就是說會保留以前的值。

②velocity模板中未被定義的變量將被認為是一個字符串。例如:

#set($foo?=?"gibbous")

$moon?=?$foo

輸出結果為:

$moon?=?gibbous

③velocity模板中不會將reference解釋為對象的實例變量。例如:$foo.Name將被解釋為Foo對象的getName()方法,而不是Foo對象的Name實例變量。例如:

$foo.getBar()等同于$foo.Bar;

$data.getUser("jon")等同于$data.User("jon");

data.getRequest().getServerName()等同于

$data.Request.ServerName等同于${data.Request.ServerName}

2.循環

#foreach?($element?in?$list)

This?is?$element.

$velocityCount

#end

例子:

#set( $list = ["pine", "oak", "maple"])

#foreach?($element?in$list)

$velocityCount

This?is$element.

#end

輸出的結果為:

1 This is pine.

2 This is oak.

3 This is maple.

每次循環$list中的一個值都會賦給$element變量。$list可以是一個Vector、Hashtable或者Array。分配給$element的值是一個java對象,并且可以通過變量被引用。例如:如果$elementt是一個java的Product類,并且這個產品的名字可以通過調用他的getName()方法得到。

#foreach ( $key in $list.keySet())

Key: $key -> Value: $list.get($key)

#end

提示:velocity中大小寫敏感。

Velocity還特別提供了得到循環次數的方法,$velocityCount變量的名字是Velocity默認的名字。

例子:

First example:#foreach ( $foo in [1..5] )

$foo

#end

Second example:

#foreach ( $bar in [2..-2] )

$bar

#end

Third example:

#set ( $arr = [0..1] )

#foreach ( $i in $arr )

$i

#end上面三個例子的輸出結果為:First example:1 2 3 4 5

Second example:2 10 -1 -2Third example:0 1

3.條件語句

#if (condition)

#elseif (condition)

#else

#end

4.語句的嵌套

#foreach ($element in $list)

## inner foreach內循環

#foreach ($element in $list)

This is $element. $velocityCount
inner

#end

## inner foreach內循環結束

## outer foreach

This is $element.

$velocityCount
outer

#end

語句中也可以嵌套其他的語句,如#if…#else…#end等。

5.注釋(1)單行注釋:

##?This?is?a?single?line?comment.

(2)多行注釋:

#*

Thus?begins?a?multi-line?comment.?Online?visitors?won’t

see?this?text?because?the?Velocity?Templating?Engine?will

ignore?it.

*#

(3)文檔格式:

#**

This?is?a?VTL?comment?block?and

may?be?used?to?store?such?information

as?the?document?author?and?versioning

information:

@version?1.1

@authorxiao

*#

Velocity 也具有邏輯AND, OR 和 NOT 操作符。

## example for AND

#if($foo && $bar)

This AND that

#end

例子中#if()指令僅在$foo 和$bar 斗為真的時候才為真。如果$foo 為假,則表達式也為假;并且 $bar 將不被求值。如果 $foo 為真,Velocity 模板引擎將繼續檢查$bar的值,如果 $bar 為真,則整個表達式為真。并且輸出This AND that 。如果 $bar 為假,將沒有輸出因為整個表達式為假。

Velocity中的宏我們可以理解為函數。

①宏的定義

#macro(宏的名稱 $參數1 $參數2 …)

語句體(即函數體)

#end

②宏的調用

#宏的名稱($參數1 $參數2 …)

說明:參數之間用空格隔開。

8.#stop

停止執行模板引擎并返回,把它應用于debug是很有幫助的。

9.#include與#parse

#include和#parse的作用都是引入本地文件, 為了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目錄下。

區別:

(1)與#include不同的是,#parse只能指定單個對象。而#include可以有多個

如果您需要引入多個文件,可以用逗號分隔就行:#include ("one.gif","two.txt","three.htm")

在括號內可以是文件名,但是更多的時候是使用變量的:

#include ( “greetings.txt”, $seasonalstock )

(2)#include被引入文件的內容將不會通過模板引擎解析;

而#parse引入的文件內容Velocity將解析其中的velocity語法并移交給模板,意思就是說相當與把引入的文件copy到文件中。

#parse是可以遞歸調用的,例如:如果dofoo.vm包含如下行:

Count down.

#set ($count = 8)

#parse ("parsefoo.vm")


All done with dofoo.vm!

那么在parsefoo.vm模板中,你可以包含如下VTL:

$count

#set($count = $count - 1)

#if ( $count > 0 )

#parse( "parsefoo.vm" )

#else


All done with parsefoo.vm!

#end的顯示結果為:

Count down.

8

7

6

5

4

3

2

1

0

All done with parsefoo.vm!

All done with dofoo.vm!

注意:在vm中使用#parse來嵌套另外一個vm時的變量共享問題。如:

->a.vm 里嵌套 b.vm;

->a.vm 里定義了變量 $param;

->b.vm 里可以直接使用$param,無任何限制。

但需要特別注意的是,如果b.vm里同時定義有變量$param,則b.vm里將使用b.vm里定義的值。

10.轉義字符'\'的使用

如果reference被定義,兩個’\’意味著輸出一個’\’,如果未被定義,剛按原樣輸出。如:

#set($email = "foo" )

$email

\$email

\\$email

\\\$email

輸出:

foo

$email

\foo

\$email

如果$email未定義

$email

\$email

\\$email

\\\$email

輸出:

$email

\$email

\\$email

\\$email

11.內置對象Velocity內置了一些對象,在vm模版里可以直接調用,列舉如下:

$request、$response、$session,另外,模板內還可以使用 $msg內的消息工具訪問 Struts 的國際化資源,達到簡便實現國際化的方法。

12.數組訪問

對數組的訪問在Velocity中存在問題,因為Velocity只能訪問對象的方法,而數組又是一個特殊的Array,所以雖然數組可以進行循環列舉,但卻不能定位訪問特定位置的元素,如 strs[2],數組對固定位置元素的訪問調用了Array的反射方法get(Object array, int index),而Velocity沒能提供這樣的訪問,所以數組要么改成List等其他類容器的方式來包裝,要么就通過公用Util類的方式來提供,傳入數組對象和要訪問的位置參數,從而達到返回所需值的目的。

示例部分

1.Hello world的示例代碼:

(1)Velocity模板(hello.html)

New Document

hello,$name!(注意:這里的name與VelocityTest.java中的名稱要一致)

(2)將velocity模板的內容轉換的類(VelocityTest.java)

import java.io.File;

import java.io.FileOutputStream;

import java.io.PrintWriter;

import java.io.Writer;

import org.apache.velocity.Template;

import org.apache.velocity.VelocityContext;

import org.apache.velocity.app.Velocity;

import org.apache.velocity.app.VelocityEngine;

/**

* Velocity轉換

* @author

*/

public class VelocityTest

{

/**

* 主函數

* @param args

*/

public static void main(String[] args)

{

//獲取模板引擎

VelocityEngine ve = new VelocityEngine();

//模板文件所在的路徑

String path = "D:/java/jproject/regedit/webroot";

//設置參數

ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);

//處理中文問題

ve.setProperty(Velocity.INPUT_ENCODING,"GBK");

ve.setProperty(Velocity.OUTPUT_ENCODING,"GBK");

try

{

//初始化模板

ve.init();

//獲取模板(hello.html)

Velocity模板的名稱Template template = ve.getTemplate("hello.html");

//獲取上下文

VelocityContext root = new VelocityContext();

//把數據填入上下文

root.put("name","world");(注意:與上面的對應)

//輸出路徑

Strint outpath = "e:/helloworld.html";

//輸出

Writer mywriter = new PrintWriter(new FileOutputStream(

new File(outpath)));

template.merge(root, mywriter);

mywriter.flush();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

(3)環境的搭建

在lib目錄內分別copy進:velocity-1.4.jar,velocity-dept.jar;

下載地址:http://jakarta.apache.org/velocity/

(4)運行后的結果如下:

New Document

hello,world!

2.Servlet和Velocity結合示例

(1)example.html

Velocity

Welcom to Velocity!

Here's the list of people

Names:

#foreach ($name in $theList)

$name

#end

(2)servlet

package com.koal.velocity;

import java.io.IOException;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Properties;

import java.util.Vector;

import javax.servlet.ServletConfig;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;

import org.apache.velocity.context.Context;

import org.apache.velocity.servlet.VelocityServlet;

import org.apache.velocity.app.Velocity;

import org.apache.velocity.exception.ResourceNotFoundException;

import org.apache.velocity.exception.ParseErrorException;

public class SampleServlet extends VelocityServlet

{

/**

*由VelocityServlet.init()調用,

*在此找出模版的路徑

*/

protected Properties loadConfiguration(ServletConfig config )

throws IOException, FileNotFoundException {

Properties p = new Properties();

//取得路徑

String path = config.getServletContext().getRealPath("/");

if (path == null)

{

System.out.println(" SampleServlet.loadConfiguration() : unable to "

+ "get the current webapp root.Using '/'. Please fix.");

path = "/";

}

//設置路徑

p.setProperty( Velocity.FILE_RESOURCE_LOADER_PATH, path);

return p;

}

/**

*Velocity主要的商業邏輯處理方法,由VelocityServlet自動調用

*@param ctx 模板上下文

*@return Template 模板信息

*/

public Template handleRequest( HttpServletRequest request,

HttpServletResponse response, Context ctx )

{

//主要在此設置演示用的數據,開發中在此調用相應的業務處理流程,

//并設置返回到頁面的數據

//待展示的列表數據

String p1 = "第一位:LiuDong";

String p2 = "第二位:Liang.xf";

Vector personList = new Vector();

//中文需要轉換

try {

personList.addElement(new String(p1.getBytes(), "ISO-8859-1") );

personList.addElement(new String(p2.getBytes(), "ISO-8859-1") );

} catch (Exception e) {

System.out.println("數據轉換異常:"+e);

}

//設置數據,供頁面模版替換成顯示的數據

ctx.put("theList", personList );

//定義模板

Template outty = null;

try

{

//取模板

outty =getTemplate("example.html");

}

catch( ParseErrorException pee )

{

System.out.println("SampleServlet: parse error for template " + pee);

}

catch( ResourceNotFoundException rnfe )

{

System.out.println("SampleServlet: template not found " + rnfe);

}

catch( Exception e )

{

System.out.println("Error " + e);

}

return outty;

}

(3)在web.xml中的配置:

SampleServlet

com.koal.velocity.SampleServlet

SampleServlet

/SampleServlet

(4)環境的搭建

在lib目錄內分別copy進:commons-collections.jar,velocity-1.4.jar,velocity-dept.jar;

Tomcat運行環境正常。

啟動Tomcat,在IE上輸入:http://localhost:8080/example,頁面顯示數據列表:

Here's the list of people

Names:

第一位:LiuDong

第二位:Liang.xf

Let life be beautiful like summer flowers and death like autumn leaves.

posted on 2008-06-07 10:18 Alexwan 閱讀(47239) 評論(10) ?編輯 ?收藏 所屬分類: J2EE 、小筆記

總結

以上是生活随笔為你收集整理的java velocity 语法_Velocity语法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。