wood burning stoves 2.0*
The moose likes JSP and the fly likes Including JSP templates in Taglibs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Including JSP templates in Taglibs" Watch "Including JSP templates in Taglibs" New topic
Author

Including JSP templates in Taglibs

Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107
I would like to write a taglib that essentualy wraps the tag body with a template. Something like this:



I can do this. But the template is not small. It will result in a bunch of out.println() statememts (or building a bunch of string buffer appends - you get the idea). That is not quite optimal. Tag Files would seem to be the obvious answer here but our app server does not support that yet...

So is there an easy way to keep the templates in JSP or HTML files and then use them in the taglib by including them? Is this a stupid thing to do?

Thanks in advance...


The squeaky wheel gets the grease. Well, that or replaced...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

Sorry, but I am not understanding what your issue is.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107
Sorry. Let me be more clear. I would like the taglib to look something like this when I use it:



The output from this taglib would look like this:



If I were using a tag file instead of a taglib it would look like this:


This would be really cool. But I do not have the option to do this. The app server I am using does not support tag files. So I have to fall back on taglibs. These I do not know as well.

If I understand correctly, this is the way I would have to write the taglib:


Wow. Now we are back to the problem with Servlets and outputting text. Tons of println statements all over the place. When this template gets more dynamic, things really get complex. Simply put, this would really bite if the template gets bigger than this.

What I would like to do is break out the html template text into separate files. Then I would include them. I could do this with static html files getting it as a resource. But what about JSP files that need to be dynamic? As in, part of the header is the title. If I had a header.jsp include file, I would need to pass the title to it so that part of the template can populate it in the header.

The best option appears to be to find a templating library and use it. I have used Tiles before. I liked it. We have not been using it here. Maybe this is time to convince the powers that be to start using Tiles or some other templating system. I think performance has kept us away from them. In the mean time, I need to see if there is a way we can do this without Tiles.

Another aproach would be to toss out the taglib idea and use <jsp:include> with <jsp aram>. That, too, would work, but there is a disconnect between the header and footer.



So I am stuck. I need templates. I don't want to create a taglib that has a ton of prinln statements. Tag files are not an option. I would like to use my own taglib if I can. But I don't know how to include dynamic JSP files in my taglib.

Any suggestions?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

A few things.

Firstly, I would not try to do everything in doStartTag. Your tag naturally prefixes and suffixes its content, so emit the prefix in doStartTag and the suffix in doEndTag.

Secondly, I can understand your reluctance to build markup in Java. It's a bear (no pun) to maintain.

You might be interested in this blog entry that I wrote a while back.
Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107
That is exactly what I was looking for! You are the man!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Including JSP templates in Taglibs