天天看點

Java模版引擎velocity的使用前言定義一個layout模版定義一個變量魂環數組或者list條件判斷内置對象自定義标簽(指令)

前言

關于velocity與SpringMVC的配置請參考前一篇文章,此處不再介紹。velocity作為Java模版引擎的主要目的是為了允許任何人使用簡單而強大的模闆語言來引用定義在Java代碼中的對象。在velocity檔案中可以給該頁面指定模版布局,進而節省了大量的時間去寫通用的模版布局。可以定義變量,與Java方法進行互動。

定義一個layout模版

在上一篇文章中提到了配置預設模版,當然也可以不使用預設模版即在要用到的頁面的最上端寫上

?

1

#set($layout='layout/yourlayout.vm')

那麼如何定義一個layout,看下面的例子:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

<!

DOCTYPE

html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<

html

xmlns

=

"http://www.w3.org/1999/xhtml"

>

<

HEAD

>

<

TITLE

>$!page_title 測試layout頁面</

TITLE

>

<

meta

http-equiv

=

"Content-Type"

content

=

"text/html; charset=UTF-8"

/>

<

meta

http-equiv

=

"Content-Language"

content

=

"zh-CN"

/>

<

meta

http-equiv

=

"Content-Type"

content

=

"text/html; charset=UTF-8"

/>

<

meta

http-equiv

=

"Content-Language"

content

=

"zh-CN"

/>

<

link

href

=

"/resources/lib/font-awesome/css/font-awesome.min.css"

tppabs

=

"/resources/lib/font-awesome/css/font-awesome.min.css"

rel

=

"stylesheet"

type

=

"text/css"

/>

<

link

href

=

"/resources/lib/bootstrap/css/bootstrap.min.css"

tppabs

=

"/resources/lib/bootstrap/css/bootstrap.min.css"

rel

=

"stylesheet"

type

=

"text/css"

/>

</

HEAD

>

<

BODY

>

$screen_content

<

script

src

=

"/resources/lib/jquery-1.10.2.min.js"

tppabs

=

"/resources/lib/jquery-1.10.2.min.js"

type

=

"text/javascript"

></

script

>

<

script

src

=

"/resources/lib/jquery-migrate-1.2.1.min.js"

tppabs

=

"/resources/lib/jquery-migrate-1.2.1.min.js"

type

=

"text/javascript"

></

script

>

</

BODY

>

</

HTML

>

那麼$screen_content所在的位置即是你所打開頁面将要占用的位置。

定義一個變量

?

1 2 3

#set($var='xxxx')

##輸出上面的變量直接使用$var

魂環數組或者list

?

1 2 3

#foreach($ele in $list)

<

span

>output element:$ele</

span

>

#end

條件判斷

?

1 2 3 4 5 6 7

#if($var=='xxxx')

<

span

>it is right</

span

>

#elseif($var=='')

<

span

>sorry</

span

>

#else

<

span

>it is wrong</

span

>

#end

内置對象

類似于JSP,velocity也有自己的内置對象,如$request,$response,$session這樣以來我們就可以将Java對象request和response以及session裡的東西輕而易舉的展現在web頁面裡。

自定義标簽(指令)

velocity同時也支援自定義标簽,或者稱為指令。如果檢視velocity的源碼就會發現有個directive.properties

?

1 2 3 4 5 6 7 8 9

directive.

1

=org.apache.velocity.runtime.directive.Foreach

directive.

2

=org.apache.velocity.runtime.directive.Include

directive.

3

=org.apache.velocity.runtime.directive.Parse

directive.

4

=org.apache.velocity.runtime.directive.Macro

directive.

5

=org.apache.velocity.runtime.directive.Literal

directive.

6

=org.apache.velocity.runtime.directive.Evaluate

directive.

7

=org.apache.velocity.runtime.directive.Break

directive.

8

=org.apache.velocity.runtime.directive.Define

directive.

9

=org.apache.velocity.runtime.directive.Stop

這裡正是velocity目前所提供的9個标簽(指令),下面我們新添加一個指令say

?

1 2 3

<body>

#say(

"hello"

)

</body>

java代碼實作

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

import

java.io.IOException;

import

java.io.Serializable;

import

java.io.StringWriter;

import

java.io.Writer;

import

java.util.HashMap;

import

java.util.Map;

import

org.apache.velocity.VelocityContext;

import

org.apache.velocity.app.VelocityEngine;

import

org.apache.velocity.context.InternalContextAdapter;

import

org.apache.velocity.exception.MethodInvocationException;

import

org.apache.velocity.exception.ParseErrorException;

import

org.apache.velocity.exception.ResourceNotFoundException;

import

org.apache.velocity.runtime.directive.Directive;

import

org.apache.velocity.runtime.parser.node.Node;

import

org.apache.velocity.runtime.parser.node.SimpleNode;

import

org.springframework.beans.factory.annotation.Autowired;

import

com.alibaba.citrus.service.template.TemplateService;

import

com.alibaba.click.util.HostUtil;

public

class

Say 

extends

Directive{

@Autowired

TemplateService templateService;

private

static

final

VelocityEngine velocityEngine = 

new

VelocityEngine();

@Override

public

String getName() {

return

"say"

;

}

@Override

public

int

getType() {

return

LINE;

}

@Override

public

boolean

render(InternalContextAdapter context, Writer writer,

Node node) 

throws

IOException, ResourceNotFoundException,

ParseErrorException, MethodInvocationException {

SimpleNode sn = (SimpleNode) node.jjtGetChild(

);   

writer.write((String)sn.value(context));

return

true

;

}

}

接下來就可以在velocity.properties檔案裡添加上

?

1

say=cn.buglife.demo.msv.directive.Say

Java模版引擎velocity的使用前言定義一個layout模版定義一個變量魂環數組或者list條件判斷内置對象自定義标簽(指令)
擷取【下載下傳位址】