aspose file tools*
The moose likes Struts and the fly likes Interesting tiles/struts making a menu question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Interesting tiles/struts making a menu question" Watch "Interesting tiles/struts making a menu question" New topic
Author

Interesting tiles/struts making a menu question

Claude Forkner
Greenhorn

Joined: Feb 19, 2006
Posts: 24
Hi all. I have a groovy question for you. I'm looking for some guidance.

I have a web app with 15 jsp pages. It's struts and uses tiles for the headers and footers. Each page has the same navigation menu on the left side, however there are some exceptions. The navigation menu has about 10 top level items. There are then two of these top level items that have subitems. The subitems are only displayed in the menu when a parent is clicked.
The menu item that corresponds to the page you are currently on, is highlighted white (the menu items are normally blue), using some css objects.
I currently have the menu hard coded in every jsp page. The appropriate menu item for each page is highlighted white in this code, and also any "trees" (parent with sub items) are displayed depending on what page we are on.

Make sense so far? Nothing too crazy going on, but I may stink at explaining it.

So, I have this yearning to put all the menu code in ONE place and not hard coded in each JSP page. Here is my plan.

Create a jsp page that is a tile, called MenuTile.jsp. This page will be called from one of the regular jsp pages using code like this:

Page1.jsp:

...

<tiles:insert page="../tiles/MenuTile.jsp" >
<tiles ut name="pageVariable" value="page1" />
</tiles:insert>

...

In the MenuTile.jsp I will grab this attribute with this code:
<tiles:useAttribute name="pageVariable" />

I will have a whole bunch of if statements in the MenuTile.jsp. There will be an if statment for every menu item. If that menu item's name is passed in (such as "page1" here), then that menu item is changed to be white, otherwise it stays blue (this is all done in the if statment), and the menu item is then dispalyed.

It gets a little harder with the menu items that have sub items. I will need to do an initial if stmt to see if the passed in pageVariable is one of the sub items of a parent menu item. If it is, then I need to display that whole tree, otherwise I keep the whole tree hidden.

Does this sound like an appropriate plan to make a menu that is only stored in one place, and can accomodate the fact that different css code is used for a link when you are actually on that links page...and the whole menu item with sub items? Is this common practice? I'm sure people have delt with this sort of situation before. I'd love to have some help, and I'd imagine this sort of discussion might help others in the future. Thanks!
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
You are going to have each page include a menu?

Why not just specify a layout, and have the menu as a .do instead of jsp. Then you could dynamically generate the .jsp page.

Maybe I misunderstood. I've done what you are trying to do many times.
Claude Forkner
Greenhorn

Joined: Feb 19, 2006
Posts: 24
Can you explain that in more detail for me? I'm an intermediary level struts guy, but could use things explained to me at a newb level.

Any other people have comments/help also? Thanks!
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
Lets say you want an application with a left hand navigation and to the right of that will be the body. Well to make it more realistic we'll throw a header and a footer in the mix.

siteLayout.jsp




Now for the tile definitions. Note that the header and navigation are generated dynamically, so they will reference a .do, the footer is static content, so it will reference the JSP.

tiles-defs.xml:



link1 and link2 are dynamically generated, so the action will point to a reference in the struts-config.xml and that will in turn point to the tile-defs.xml to reference its JSP.

struts-config.xml snippet:



-Chris
[ January 04, 2007: Message edited by: Chris Boldon ]
Claude Forkner
Greenhorn

Joined: Feb 19, 2006
Posts: 24
Chris, I got some code coming your way. I'll post it in a second. It's hard to tell, but I think I need to describe what I'm trying to do a little better. I'm not sure if your example can handle the different permutations the menu has to handle. Here comes some test code I have put together - and THANKS so far for your help.
Claude Forkner
Greenhorn

Joined: Feb 19, 2006
Posts: 24
So. Every jsp page will look sort of like this:




So, each jsp page will basically pass the tile (I don't really use tiles correctly, It's more of a jsp page in my case) a variable with it's own name. That way the menu tile can figure out which link to highlight, etc...

Now let's look at what I have going on in my test tile jsp page:

Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
Your right, that really isn't the proper use of tiles:-).

I have an idea.

If you are using Spring you could easily create a bean that is persistant through all classes. When you interact with the menu you hit a dispatch method within the MenuAction. In the display action you call the bean through Spring, set a variable called "location" to the link's selection and then forward to the correct JSP for the body.

When the menu reloads it would use the "location" from the bean to dynamically change the way it looks.

This is how I would do it if I were writting the app. I don't like to have scriptlets in my JSPs either. If you absolutly cannot get around using scriptlets create your own custom Tag. Maybe even create a custom tag that pulls from the resource properties file so it's easily maintainable. Then in your menu jsp file you'd only have something like <mytag:mytag location="${location}" />.

But I think I just made something simple entirely more complicated.

But seriously I'd look into using Spring to create a persistant bean throughout your classes. This way you can share common information without opening up your scope. You could then get back to using a layout.jsp and make your app much more maintainable.

Interesting topic:-).
Claude Forkner
Greenhorn

Joined: Feb 19, 2006
Posts: 24
Interesting. I'm such a fish out of water with this web stuff. Man it's frustrating. I've had a lot more experience doing standalone apps.

What do you do to avoid using scriptlets? JSTL?

I'm not using Spring at the moment, and probably can't handle adding in another framework.

Anything you think I can do without using Spring? You wouldn't think doing a menu would be very hard....that's what surprises me. I'm trying to slowly move this app to be more maintainable by taking small steps at a time. That's why I'm hesitant to dare take any large steps, since this is a production app.

Is the idea I've come up with totally crazy, or just not the "best" idea? Is it a move in the right direction (over having the menu hard coded in every page - the menu is pretty big with 15 links, and is hogging a lot of space in the code of the jsp pages) that I can take to centralize the code, while slowly learning more and better ways to code this? Would it be much slower having about 15 if stmts evaulate, compared to the way I have it now with the menu hardcoded right in the JSP page?

Thanks for your thoughts.
Chris Boldon
Ranch Hand

Joined: Aug 10, 2006
Posts: 190
Ah, I know how it can be moving from standalone apps to web. Yeah use JSTL or Struts tags to get rid of Scriptlets.

Do you have gTalk? If you do send me a private message and I'll get ahold of you sometime after lunch. I'd be happy to help you get on your feet, pointed in the direction you're looking to go. I'm sure we can come up with a good solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interesting tiles/struts making a menu question