aspose file tools*
The moose likes Struts and the fly likes Passing information between tiles Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Passing information between tiles" Watch "Passing information between tiles" New topic
Author

Passing information between tiles

Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Feb 05, 2003
Posts: 4727

try adding this to the layout definition:
<put name="currentPage"value="${currentPage}"/>
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Passing information between tiles