Social Icons

Dynamic menu with Sitemesh

It's always a matter of concern for web developers to keep the page design constant across all of the application. Using frames is a dead idea, so what's left including your header, navigation and footer jsp on each and every page ?

Well there is something better than that, Sitemesh. It allows you to keep your page design consistent or even maintaining different UI styles for different parts of the site.

But a major problem which Sitemesh users often come across is to implement a dynamic menu / navigation, like in a situation where menus in your application changes based on the 'role' of the logged in user. The solution for the same is here implemented in a simple JSP / Servlet code.

Dynamic menu with Sitemesh

To keep a dynamic approach, I have kept Menu texts and links in a properties file.

sitemesh-2.4.2.jar -

The basic idea is once the user logs into the application, the user's role is stored into the application session. The sitemesh reads the user role and applies appropriate navigation menus based on the role. Secondly to highlight the menu item which is currently selected a meta tag is placed on each page just to denote under when menu item it belongs. The sitemesh decorator reads the same meta tag and applies a different css to it's menu item.

web.xml - Configure the sitemesh filter to intercept request and send for decoration

decorators.xml - sitemesh config file for defining decorators

main.jsp - decorator, the place where you design the layout of your design and implement the dynamic logic. The code below is pretty self-explainatory with the logic stated above.


Download the MyEclipse / Eclipse project

Hope these tips helped you, don't forget to leave your comments here.