Question

I have a pretty basic tiles1 setup with header, menu, and content as so:

<definition name="Main" path="/WEB-INF/jsp/MainLayout.jsp">
    <put name="header" value="/WEB-INF/jsp/Header.jsp"/>
    <put name="menu" value="/WEB-INF/jsp/Menu.jsp"/>
    <put name="content" value=""/>
</definition>

All my subpages extend this definition and provide a jsp for the content section:

<definition name="SearchPage" extends="Main">
    <put name="content" value="/WEB-INF/jsp/Search.jsp"/>
</definition>

What I am looking for is a way for the SearchPage definition above to "put" an attribute so that it can be used in Search.jsp. My first attempt was:

<definition name="SearchPage" extends="Main">
    <put name="content" value="/WEB-INF/jsp/Search.jsp"/>
    <put name="showOptions" value="true" type="string"/>
</definition>

But in this case, the showOptions attribute would only be available to MainLayout.jsp and wouldn't be passed down to Search.jsp. I know there's some way to explicitly pass it down via useAttribute in the layout page, but I would very much like to avoid the layout page needing to know about every variable that I could possibly using in the subpages.

N.B. In tiles2 there seems to be a nifty cascade="true" attribute that does exactly what I'm looking for but unfortunately I think I'm stuck with tiles1.


Nice one-line fix thanks to GriffeyDog:

In MainLayout.jsp import all the tiles attributes into the request scope. They are then available to all the subpages (watch out for name collisions).

<tiles:importAttribute scope="request"/>

I also found an alternate method which uses a definition with the necessary attributes as the content instead of a jsp page: http://wiki.apache.org/struts/StrutsDocTiles (solution #1)

Was it helpful?

Solution

You can use tiles:importAttribute and specify a scope, such as request. Do this in your main layout and then you should be able to access it in your search page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top