简体   繁体   中英

apache + tiles2 layout problem

I have the following tiles configuration file

<tiles-definitions>
    <definition name="base" template="/includes/layout.jsp">
        <put-attribute name="header" value="/includes/header.jsp" />
        <put-attribute name="menu" value="/includes/menu.jsp" />
        <put-attribute name="footer" value="/includes/footer.jsp" />
    </definition>
    <definition name="home" extends="base">
        <put-attribute name="contentBody" value="/home/view.jsp" />
    </definition>
</tiles-definitions>

and to display "view.jsp" page I have another page called "home.jsp" and it has the following code (only)

//home.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<tiles:insertDefinition name="home" />

and the problem here is, If i want to load "edit.jsp" page with same layout, I need to repeat this part in tiles.xml

<definition name="editbase" extends="base">
            <put-attribute name="contentBody" value="/home/edit.jsp" />
</definition>

and create a file called "editbase.jsp" and call it. (repeating the above code)

I'm just wondering is this the correct way or can I do something like

<tiles-definitions>
        <definition name="base" template="/includes/layout.jsp">
            <put-attribute name="header" value="/includes/header.jsp" />
            <put-attribute name="menu" value="/includes/menu.jsp" />
            <put-attribute name="footer" value="/includes/footer.jsp" />
        </definition>
        <definition name="home" extends="base">
            <put-attribute name="contentBody" value="/home/view.jsp" />
            <put-attribute name="contentBody" value="/home/edit.jsp" />  
        </definition>
</tiles-definitions> 

and load the page accordingly.. I think my question is clear

I'm using apache tiles 2.2.2

thanks in advance

cheers

sameera

Unless you have many different header/footer/menu, you can simplify /includes/layout.jsp have placing the header, footer and menu code directly in it. Then, use <tiles:insert> to insert the actual page content:-

/includes/layout.jsp

<html>
    <head>
        ...
    </head>
    <body>
        <!-- put your header code here -->
        ...

        <!-- put your menu code here -->
        ...

        <tiles:insert attribute="contentBody" />        

        <!-- put your footer code here -->
        ...
    </body>
</html>

tiles-defs.xml

Create the attribute called contentBody .

<tiles-definitions>
    <definition name="base" template="/includes/layout.jsp">
        <!-- Create a contentBody with no value -->
        <put name="contentBody" value="" />
    </definition>

    <definition name="view" extends="base">
        <!-- Override base's contentBody -->
        <put name="contentBody" value="/home/view.jsp" />
    </definition>

    <definition name="edit" extends="base">
        <!-- Override base's contentBody -->
        <put name="contentBody" value="/home/edit.jsp" />
    </definition>       
</tiles-definitions>

Your JSP viewer files will not have any tiles tags because putting these tags in the viewers make your layout very rigid and intrusive... this is the problem you are facing now. Instead, put only the actual content in the viewer files. They will be automatically included by <tiles:insert> from the layout file.

struts-config.xml

If you are using Struts, the definition name view and edit must match the path in your struts config, for example:-

    <action path="/bla" type="..." name="FrmReport" scope="request">
        <!-- path "view" must match tiles definition name -->
        <forward name="success" path="view"></forward>
    </action>

This way, Tiles will automatically decorate your page before the page gets displayed on the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM