2012-10-18 24 views
5

我想在我的struts2配置中添加命名空間,並使用瓦片。對瓦片使用多個Struts2命名空間

A爲例包我的struts.xml的:

<package name="search" namespace="/search" extends="struts-default"> 
<result-types> 
    <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" /> 
</result-types> 
<action name="SearchActionInit" class="web.action.SearchAction" method="initSearch"> 
    <result name="input" type="tiles">search</result> 
    <result name="success" type="tiles">search</result> 
</action> 
</package> 

而相應區塊配置:

<definition name="baseLayout" template="layout.jsp"> 
    <put-attribute name="titre"    value="titre.default" /> 
    <put-attribute name="header"   value="/common/header.jsp" /> 
    <put-attribute name="menu"   value="/common/menu.jsp" /> 
    <put-attribute name="leftcontent"   value="/common/leftcontent.jsp" /> 
    <put-attribute name="rightcontent"   value="/common/rightcontent.jsp" /> 
    <put-attribute name="detail"   value="/common/detail.jsp" /> 
    <put-attribute name="footer"    value="/common/footer.jsp" /> 
</definition> 

<definition name="search" extends="baseLayout"> 
    <put-attribute name="titre"    value="titre.search" /> 
    <put-attribute name="rightcontent"   value="/pages/search/Search.jsp" /> 
</definition> 

我的問題是,我需要複製在該layout.jsp搜索文件夾以進行名稱空間搜索(對於其他名稱空間等)。這不是在瓷磚邏輯中,並會帶來更多的努力來維護。

有沒有人對這個問題的關鍵,以避免重複?

+0

嗨,我不明白,爲什麼你必須重複layout.jsp。你不必定義'result-types',試試這個:'' – Jaiwo99

+0

Thx for your answer。添加tiles-default沒有變化。我需要複製它,因爲他正在與名稱空間名稱相同的文件夾中等待layout.jsp。例如在這個例子中,我重命名了layout.jsp,並得到了一個404錯誤(找不到search/layout.jsp)。 – Rydermark

+0

我終於明白你的問題了。我稍後會給你答案.. – Jaiwo99

回答

1

試試這個:

模板:

<!-- meta template --> 
<definition name="global" template="/WEB-INF/template/layout.jsp"> 
    <put-attribute name="attr1" value="/WEB-INF/template/attr1.jsp"/> 
    <put-attribute name="attr2" value="/WEB-INF/template/attr2.jsp"/> 
    <!-- more --> 
</definition> 

然後將數據:

<!-- instance --> 
<definition name="myApp.search" extends="global"> 
    <put-attribute name="attr2" value="/jsp/search/search.jsp"/> 
</definition> 
<!-- instance --> 
<definition name="myApp.page2" extends="global"> 
    <put-attribute name="attrN" value="/jsp/namespaceN/whatever.jsp"/> 
</definition> 

你只需要改寫頁layout.jsp,您希望加載的一部分。

這裏是我的項目結構:

Root 
| 
--jsp 
    | 
    namespace1 
     | 
     --*.jsp 
    | 
    --*.jsp 
| 
--WEB-INF 
    | 
    --template 
     | 
     --layout.jsp 
     | 
     --attr1.jsp 

我希望這能解決你的問題。

+0

Thx Jaiwo。我做了一些測試。重要的一點是爲佈局設置一條路徑。 (不要把「layout.jsp」,而是「/layout.jsp」)你睜開眼睛! – Rydermark