Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple Struts2 namespaces with tiles

I want to add namespaces in my struts2 configuration and I use tiles.

A package of my struts.xml for example :

<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>

And the corresponding tiles configuration :

<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>

The problem I have is that I need to duplicate the layout.jsp in a search folder for the namespace search (and so on for the other namespaces). It's not in the tiles logic and will bring more effort to maintain.

Has anybody a key to this problem to avoid duplication?

like image 283
Rydermark Avatar asked Nov 13 '22 20:11

Rydermark


1 Answers

try this:

template:

<!-- 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>

then the data:

<!-- 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>

You need only overwrite the part of the page layout.jsp, which you'd like to load.

here is my project structure:

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

I hope this solves your problem.

like image 154
Jaiwo99 Avatar answered Jan 04 '23 02:01

Jaiwo99