Velocity布局(layout)功能介紹

2021-09-02 04:08:06 字數 3797 閱讀 4173

對於乙個web應用來說,頁面的基本結構往往是固定的,頁面中的很多部分,例如頁面統一的頭部、尾部和選單,我們一般是不太需要變化的,各個頁面基本一致,變化的往往是頁面的具體內容部分,這樣,布局(layout)功能的使用,就能大大減化前端頁面的複雜性了。這裡簡單介紹一下如何使用velocity框架來實現頁面的布局。

我們知道,要在spring框架中配置velocity,一般需要配置兩個bean,乙個是velocityconfig, 別乙個則是viewresolver。先來看velocityconfig的配置,基本配置如下所示,這裡和不使用布局的配置方式沒有任何差別。

xml**  

<

bean

id= "velocityconfig"

class= "org.springframework.web.servlet.view.velocity.velocityconfigurer"

>

<

property

name= "resourceloaderpath"

>

<

value

>web-inf/view

value

>

property

>

<

property

name= "velocityproperties"

>

<

props

>

<

prop

key= "input.encoding"

>utf-8

prop

>

<

prop

key= "output.encoding"

>utf-8

prop

>

props

>

property

>

bean

>

接下來看看viewresolver的配置,在使用布局時,一般會配置如下:

xml**  

<

bean

id= "viewresolver"

class= "org.springframework.web.servlet.view.velocity.velocitylayoutviewresolver"

>

<

property

name= "cache"

value= "true"

/>

<

property

name= "prefix"

value= "/"

/>

<

property

name= "suffix"

value= ".vm"

/>

<

property

name= "contenttype"

>

<

value

>text/html;charset=utf-8

value

>

property

>

<

property

name="toolboxconfiglocation"

>

<

value

>/web-inf/toolbox.xml

value

>

property

>

<

property

name="layouturl"

value="layout/layout.vm"

/>

<

property

name="allowsessionoverride"

value="true"

/>

<

property

name="allowrequestoverride"

value="true"

/>

<

property

name="exposesessionattributes"

value="true"

/>

<

property

name="requestcontextattribute"

value="rc"

/>

<

property

name="exposerequestattributes"

value="true"

/>

bean

>

如果你配置過velocity,你會發現與你之前的配置主要有兩個地方的差別:

1.  一般情況下,當我們不使用velocity的布局功能時,我們一般會把viewresolver的class配置為:

org.springframework.web.servlet.view.velocity.velocityviewresolver,當需要使用布局功能的時候,viewresolver的class需要配置為:

org.springframework.web.servlet.view.velocity.velocitylayoutviewresolver,顧名思義,從命名中我們就能看出來這個viewresolver是支援layout功能的。

看看一般情況下,我們的layout頁面大概長什麼樣子吧:

html**  

<

body

>

<

divclass="content"

>

#parse("/common/header.vm")  

<

divclass="content-info"

>

#parse("/common/leftmenu.vm")  

<

divclass="content-main"

>

$screen_content  

div>

div>

#parse("common/footer.vm")  

div>

body

>

解釋一下:如果你在處理某個請求時返回了a.vm模板,那麼最終返回的頁面是:將a.vm模板的內容填充layout頁面的$screen_content佔位符的內容,然後再將整個填充後的頁面返回給使用者。如果有興趣你可以看一下大致的實現原理,其實很簡單,就是先呼叫velocity引擎渲染請求返回的模板,將該渲染結果(乙個string型別的字串)作為一引數傳遞給layout頁面,引數名稱就是screen_content,然後再次呼叫引擎渲染layout頁面,這樣就會把該screen_content引數對應的值作為頁面的一部分渲染出來了,從而實現整個頁面的布局功能。

這樣,我們就簡單介紹了如何簡單配置和使用velocity的layout功能了,這裡你可以會有疑問了,如果我某個頁面需要使用不同的layout頁面,甚至我有些頁面不需要使用layout功能,該怎麼辦?其實很簡單,我們前面viewresolver配置中,配置的只是預設的layout頁面的位址,如果你要使用不同的layout頁面,只需在你要返回的頁面中如下簡單設定一下即可:

html**  

#set($layout = "/layout/layout2.vm")  

這樣,我們就強制我們的這個頁面使用的是layout2.vm這個頁面布局。按照這個思路,如果我在某個特殊的頁面,不想使用layout功能了,我們可以再指定另外乙個layout頁面,只不過這個layout頁面內容比較特殊,它沒有一般布局頁面中有的那些頁面統一的頭部、尾部和選單,只有如下所示的一行**就可以了:

html**  

$screen_content  

關於設定android布局檔案Layout

一般情況,當xml檔案中未設定寬和高度時,此時可以new 乙個新的物件,接著,利用 relativeview.setlayoutparams new relativelayout.layoutparams 100,200 可以實現,但是如果是這種形式 relativeview relativelay...

Velocity初學筆記

velocity頁面 velocity 1.變數 屬性 方法 set foo velocity hello foo world 或者hello world customer.address purchase.gettotal ps set foo gibbous moon foo 輸出 moon g...

velocity 快速入門

基本語法 1 變數定義 name 注意 a 名字和 配合一起用 b 更規範的寫法是 2 賦值 set name 威少 3 條件判斷 if code elseif code else code end 注意 結尾 end不可缺 4.迴圈 foreach element in list element ...