Search code examples
javajspjstltilesapache-tiles

Using core library set variable in Apache Tiles


I have one template JSP page:

<html>
<head></head>
<body>
    <c:set var="temp" scope="request" value="" />
    <tile:insertAttribute name="header"/>
    <tile:insertAttribute name="body"/>
    <tile:insertAttribute name="footer"/>
</body>
</html>

My template XML is below:

<tiles-definitions>

    <definition name="home" extends="template">
        <put-attribute name="temp" value="home" />
        <put-attribute name="body" value="/WEB-INF/pages/home.jsp" />  
    </definition>

    <definition name="template" template="/WEB-INF/templates/template.jsp">
        <put-attribute name="temp" />
        <put-attribute name="header" value="/WEB-INF/pages/includes/header.jsp" />
        <put-attribute name="body"  />
        <put-attribute name="footer" value="/WEB-INF/pages/includes/footer.jsp" />
    </definition>
<tiles-definitions>

My question:

When I have set "home" value in "temp" name in home definition, I want to get this "home" value in header page and footer page so I am do ${temp} but I haven't found "home" value.

It's possible? If yes then how?


Solution

  • You can use tiles to print attribute to JSP and set to the request scope variable or create a request scope variable with tile:useAttribute. For example in the first case you can do

    <html>
    <head></head>
    <body>
        <c:set var="temp" scope="request"><tile:getAsString name="temp"/></c:set>
        <tile:insertAttribute name="header"/>
        <tile:insertAttribute name="body"/>
        <tile:insertAttribute name="footer"/>
    </body>
    </html>