5

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Rydermark
  • 217
  • 1
  • 4
  • 13
  • Hi, i do not understand, why you have to duplicate layout.jsp. and you do not have to define `result-types`, try this: `` – Jaiwo99 Oct 18 '12 at 10:00
  • Thx for your answer. No change adding tiles-default. I need to duplicate because he is waiting a layout.jsp in a folder with the same name as the namespace. For instance in this example, I rename the layout.jsp and I get a 404 error ( search/layout.jsp not found). – Rydermark Oct 18 '12 at 16:31
  • I finally understand your question now. I will give you the answer later.. – Jaiwo99 Oct 18 '12 at 16:34

1 Answers1

1

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.

Jaiwo99
  • 9,687
  • 3
  • 36
  • 53
  • Thx Jaiwo. I made some tests. The important point is to put a path for the layout. (Not put "layout.jsp" but "/layout.jsp") You open my eyes! – Rydermark Oct 19 '12 at 11:46