-
Dec 18th, 2010, 04:35 AM
#1
How do I show the currently selected tab on the main menu?
Please tell me there's an easy way to do this, because I'd like to add this feature to my site, but I think I really have to go about doing a whole slew of things in order to enable this.
For example, don't I need to know the whole sitemap in order to calculate which of the main tabs is currently selected? For example, if they are 3 pages deep, I still want to keep the main section's tab highlighted, so I need to take the current page and go up to the top level menu item, and then compare that to each of the menu items to make it selected, right?
And such a sitemap can get really messy, because it needs to account for which users have access to view those pages, so each user's sitemap is going to be totally different.
This lends itself to a solution where I populate some kind of sitemap structure when my application starts up along with the roles required for each page in the sitemap... and then when each page renders, I run through that sitemap structure and reduce it (make a copy) to only include the pages that user can see.
Furthermore, putting such a thing in the database is going to make development just awful, so I'd probably have to keep this structure in memory and feed it to my freemarker template for each page request (whenever I need to render the site layout... not for ajax requests).
But of course, if javascript should move to another section on the site, I also have to account for it the problem there too. Argh!
Please tell me there's a very easy to accomplish this without having to do 4-6 hours of work. It would be great to just annotate the controllers what the sitemap should be, so I can just derive it from the controllers without having to duplicate all of this role/path information in a totally separate part of the site.
-
Dec 19th, 2010, 01:48 AM
#2
What you are describing would require Spring to be aware of all the navigational possibilities of your site. This isn't the purpose of Spring. For all its power and convenience, it simply can't derive all of the navigation options of your site on its own. You will have to spend the time creating logical handlers to maintain the desired state of your UI based on a user's actions.
An analogy to what you're trying to achieve is a "bread crumb," a text and link history, placed at the top of the page which provides for the user some context of where he is in the site and how he arrived at this place. In one of our sites, each controller method calls a method that creates the bread crumb and adds it to the model. You will need to implement something similar to decide which tab is currently displayed.
To create our bread crumb handler, we analyzed every possible outcome of a user's interaction with our site. Then, at the beginning of each controller method, we pass a form object that represents the inputs bound to the submitted form. With this information, we build a String of links and separators that is displayed in the resulting view.
The point of all this is to say there isn't an easy way to do this. However, the task is not as daunting as you may think. But you certainly should budget at least 4-6 hours for this, depending upon the complexity of your web site.
-
Dec 19th, 2010, 02:56 AM
#3
Thanks for the response.
Well, while the breadcrumbs is a neat idea, I don't really want to litter all of my controllers with this aspect-oriented problem. I don't really need breadcrumbs on this particular site, at least not yet.
I think the best solution for me is to do what I indicated then... to have a SiteMap object that contains a nested graph for the entire site.
Basically, each entry would have a name, a url, whether it's visible on the menu or not, and a collection of roles that are allowed to view the page (with some special options for any user that is authenticated)
Then when a user wants a menu, they just ask SiteMap, "Hey, give me my menu", and it takes their roles and builds a custom graph of menu items and adds that to the model.
I can do this all in an interceptor, so no controller ever has to worry about this.
If I can build the sitemap by putting a method on each @Controller and just inspect them all when the application starts up, all the better. Otherwise, I guess I have to manually populate it in some bootstrap bean.
That's pretty much the only nice way I can think of to solve this problem.
-
Dec 19th, 2010, 04:20 AM
#4
Hi,thanks for sharing such useful information.Really a nice information is presented here.Thanks a lot again for sharing the information...
Enneagram Coaching
Last edited by innermastery; Dec 19th, 2010 at 04:21 AM.
Reason: signature is not visible
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules