• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing information between tiles

 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Thanks.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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")
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try adding this to the layout definition:
<put name="currentPage"value="${currentPage}"/>
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I got it figured out. Here's what I've got:

tiles-def.xml
<put name="currentPage" value="FrontPage" />

Layout.jsp
<tiles:useAttribute name="currentPage" scope="request" />

Navigation.jsp
<jsp:useBean id="currentPage" type="java.lang.String" scope="request"/>
Current page is <%= currentPage %>

...and it works. I noticed that I could do <tiles:useAttribute name="currentPage"/> on the layout page, but not in the navigation page, so I figured it had something to do with scope -- and it did.

Thanks for the help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic