• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Passing information between tiles

 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try adding this to the layout definition:
<put name="currentPage"value="${currentPage}"/>
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic