2012-04-11 155 views
1

我正在使用spring-mvc和spring-webflow開發我自己的項目。閱讀了一些關於spring webflow和ajax的文章後,我明白了更好的選擇是使用Apache Tiles來渲染視圖。從Sitemesh遷移到Apache Tiles

在Sitemesh中,我使用了一個標籤調用頭()。該模板中使用的標記允許呈現在生成的HTML上呈現的頁面的整個頭部屬性。

有什麼辦法可以在Apache Tiles中實現這個功能嗎?從我的閱讀我認爲我必須做到以下幾點:

兩個jps,一個與頁面的主體和另一個與頭部定義。以下是模板,頁面和圖塊定義的示例,以便更好地理解。

瓷磚定義

<tiles-definitions> 
    <definition name="base" template="/WEB-INF/view/templates/tileslayout.jsp"> 
    <put-attribute name="title" value="Held - main page"/> 
    <put-attribute name="body" value=""/> 
    <put-attribute name="head" value=""/> 
    </definition> 

    <definition name="company.edit" extends="base"> 
    <put-attribute name="head" value="/WEB-INF/view/company/editHeader.jsp"></put-attribute> 
    <put-attribute name="body" value="/WEB-INF/view/company/edit.jsp"></put-attribute> 
    </definition> 

</tiles-definitions> 

模板:

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %> 
<html> 
<head> 
-- css and scripts -- 
<tiles:insertAttribute name="head" ignore="true"/> 
<!-- <decorator:head /> --> 
</head> 

<body> 

--- menu definition --- 

<div class="container-fluid"> 
    <tiles:insertAttribute name="body"/> 
<!-- <decorator:body/> --> 
</div> 
<hr/>  
<footer> 
    ----- 
</footer> 

</body> 
</html> 

公司網頁

<div class="container"> 
-- the page html code 
</div> 

頭公司網頁

<meta name="menu" content="company" /> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<style> 
.error { 
    color: red; 
} 
</style> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#name').focus(); 
    }); 
</script> 

有時頭可能更復雜。

生成的html是好的。但我不喜歡爲應該很簡單的東西定義兩個jps。

我做錯了什麼?

有沒有更好的方法來做到這一點?

回答

2

你有什麼是正確的。 Sitemesh使用的裝飾模式可以解決這個問題,而Tiles使用的組合不能。不過,我不會說這很令人擔憂。

組成是在概念上更簡單的策略,它是較少的資源密集型。這是好的,你正在學習瓷磚這兩個系統不是排他性的,可以是免費的。

瓷磚是關於把你的網頁分解成瓷磚。你所擁有的和瓷磚目前所允許的一樣好。你發現在頭腦中有劇本重新發生,所以已經制作了一片。雖然它不像執行你所提議的那麼簡單,但如果你把所有的網站功能放到一個單獨的腳本文件中,它將需要很短的時間來加載,但是這樣做之後不會因爲緩存而導致開銷。

如果安全性是一個問題(例如,您在應用程序中具有不同的安全級別,並且不想泄露高級用戶甚至可以通過暴露他們的JS的能力),那麼您可以讓tile屬性使用EL定義要引入哪些圖塊。爲此,請使用tiles 2.2.2版並使用tiles listener「org.apache.tiles.extras.complete.CompleteAutoloadTilesListener」,這將允許使用通配符,EL,OGNL和MVEL瓷磚定義。以下內容將說明wldcard如何大大減少定義的大小:

您的第二個定義是「公司」。編輯:」如果你有多個企業,有多個動詞,那麼你可以寫,而不是定義如下:

<definition name="*.*" extends="base"> 
    <put-attribute name="head" value="/WEB-INF/view/{1}/{2}Header.jsp"></put-attribute> 
    <put-attribute name="body" value="/WEB-INF/view/{1}/{2}.jsp"></put-attribute> 
</definition> 

現在,你可以簡單地公司後創建公司在/ WEB-INF /查看/以editHeader,viewHeader等沿。

如果升級到2.2.2,並使用「org.apache.tiles.extras.complete.CompleteAutoloadTilesListener」,因爲解釋表達的多種方式,你需要寫:

<definition name="WILDCARD:*.*" extends="base"> 
    <put-attribute name="head" value="/WEB-INF/view/{1}/{2}Header.jsp"></put-attribute> 
    <put-attribute name="body" value="/WEB-INF/view/{1}/{2}.jsp"></put-attribute> 
</definition> 

如果你決定決定把所有的東西ËJS爲特定的安全級別爲瓷磚您可能說得到相應的瓷磚:

<put-attribute name="head" value="/WEB-INF/view/{1}/OGNL:'session.securityLevel'+'.jsp'"></put-attribute> 

OGNL是默認的Struts2的表達式語言,你的情況,你會使用適當的EL或MVEL。

+0

非常感謝。有用的信息。 – 2012-04-19 13:49:25