This post originated from an RSS feed registered with Java Buzz
by Cal Holman.
Original Post: Building Bread Crumbs with Tiles and Struts
Feed Title: Cal Holman's Blog
Feed URL: http://www.calandva.com/holmansite/do/rss/CreateRssFile?type=blog
Feed Description: CalAndVA.com is built on many Java Open Source projects - this is a blog on the site progress
Let me preface this with the facts: i am not very technical so there are probably many out there that can do what i am demonstrating in a 'better' manner. I used the ideas from the Tiles creator himself - Cedric Dumoulin (thanks for a great tool) and his advanced manual to create my site. This is a small part of the site demonstrating the use of bread crumbs and Tiles. I love Tiles and probably tend to over use the framework. You will need to download the Crumb.war
There are two techniques used in this app for bread crumb generation. The first is to write a Tiles Action that accesses the Tiles definition and the second is a tag that reads the tiles definition. Both have advantages. Using the Tiles Action you can extend the functionality of the class to provide additional Tiles based services. Using the tag is super easy and reduces the overhead of an additional action class.
The three crumb bars on each page display - from the top - the Tiles Action technique, the default (almost - style class added) crumb tag and the third the crumb tag with a different style class and separator. You have probably already clicked about a figured out how they work - but let me describe the Tiles Action first.
The Tiles Action is triggered by adding a controllerUrl attribute to the definition - in this case i am using controllerUrl="/do/renderCrumb". renderCrumb extends Tiles Action and is called whenever the corresponding Tiles Definition is triggered - this includes any controllerUrl that is in a parent definition. Since i attached the controllerUrl to my base definition it will be called for each page (I only link to definitions). Anyway in the renderCrumb action the Tiles Component Context is read giving us access to the current definition - including all the inherited definitions. So the crumb attributes are accessed as well as the page name attribute. This depends on the crumb link and title attributes following a specific naming convention. Each level of the site must inherit it's definition from it's parent and add a set of crumb attributes incrementing the level number on the end of the attribute.
In the Crumbs Action i extended the ability of the Tiles Action to read and load another attribute (of course this is pretty easy to get directly in the jsp as well). This allows some menu styling based on the current Tiles definition. Some additional things you can do in a Tiles Action is play with the definition - change an attribute or even change the template. For an example of this go to my personal site settings page. Here i change the master template used and the stylesheet based on the user's selection.
Tiles Actions are pretty handy - if you select the portal view each of the little list boxes on the right side of the screen are individual jsps with their own action classes. This allows to each to be used on any jsp and the Action Class for the jsp does not need to know the other tile is there. The little tile can load it's own beans without depending on the page it is included in. Additionally i am using osCache to cache the portal tiles since executing 6 or 7 Action Classes per page is a bit much. The support projects page has all the support projects i am using.
The Tiles Action version displays the root (home) crumb but the Crumb Tag does not - you can modify both. The Crumb Tag needs no attributes if you name your crumb link and title crumblink and crumbtitle. If you are inclined you can change the style, separator character, and names of the crumb attributes in the Tiles definition.xml.
The only "bad" part of all this is - you need to use tiles for your site layout in a structured manner. This may make this technique not right for your site. You could get the crumb definitions from another source in the Tiles Action using a page attribute as a key to a database or xml file.
Again - none of this is original, Cedric provided the code for the Tiles Action and Steve Melzer wrote the tag which i extended.