I'd like to implement a menu using Struts Tiles which highlights the currently selected menu item somehow. How should I pass the information about the currently selected page to the tile responsible for rendering the menu?
Here's an example:
In other words: 1) The layout tile named ".mainLayout" specifies that every page includes the navigation.jsp for rendering a navigation menu 2) I want both "frontPage.page" and "anotherPage.page" to tell "navigation.jsp" that it should highlight that particular link to indicate that that's what the user has currently selected and is viewing.
Anyone? I've never done this kind of stuff with Tiles so even the smallest bit of advice would be much appreciated.
You can add a value to each definition to specify which page is selected. <definition name="frontPage.page" extends=".mainLayout"> <put name="content" value="/pages/FrontPage.jsp" /> <put name="currentPage" value="front" /> </definition>
Your menu jsp would then compare the value passed against a hard-coded value to determine which section of the menu to highlight. If you do this in a scriptlet you may access the tiles variable with: (String)request.getAttribute("currentPage")
A good workman is known by his tools.
Joined: Jan 23, 2002
Thanks for the tip, Marc, but I'm getting null from the getAttribute() call.
I also tried to use <tiles:useAttribute name="currentPage"/> in the navigation.jsp to fetch the attribute I set in my tiles-def.xml with <put name="currentPage" value="foo"/> but that didn't work either. I'm getting the following exception: