• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reusing a "mostly" static chunk of HTML

 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I've got a header file, where most of it is static HTML, but which has a few parameterized elements:

<html>
<head>
<title><%= metaTitle %></title>
</head>
<body>
... lots of static code ...
<h1><%= pageTitle %></h1>
... lots of static code ...
<!-- ## begin content ## -->

If I wanted to do this in ColdFusion (that's my background), I'd simply make a custom tag, and I'd refer to it from my calling page as:

<cf_header metaTitle="Welcome to my site" pageTitle="Home"/>

What's the JSP equivalent? I have a vague understanding of JSP custom tags, but I guess I'm just trying to avoid a slew of those beastly "out.println()"s, "+"s, or ".append()"s. Seems like there should be a better way, when the bulk of the output is static, with only a couple of parameters.

Am I making any sense? :-)

Thanks,
Jamie
[ January 25, 2005: Message edited by: Jamie Jackson ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant, that does look like the answer!

Thanks!
Jamie
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit: Jamie snuck in his response, which seems to indicate that he is using JSP 2.0, while I was writing mine. I'm going to leave my response here through in case anyone has a similar issue but is not using JSP 2.0 yet.]

Tag files are indeed exactly what you are looking for assuming you are using JSP 2.0 (which you should be unless you have good reason not to be).

If you are stuck in the JSP 1.2 world, a custom tag is still the best way to go, it's just a little more work. From within the Java code of your tag handler you can include a JSP fragment file that contains markup (a concept I called 'taglets') and pass the dynamic stuff to it as scoped variables. That way, you don't have to build a bunch of static markup in string buffers (that way lies madness).
[ January 25, 2005: Message edited by: Bear Bibeault ]
 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just saw your second response after I posted a followup thread. Maybe I don't have JSP 2.0. I think JRun 4 is JSP 1.2.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If so, you won't be able to use tags files and you may need to resort to my "poor man's tag files" scheme. It's not that hard really: in your tag handler use the include() method of the pageContext to suck in the "taglet" file. Use scoped variables to communicate between the handler and the "taglet".
[ January 25, 2005: Message edited by: Bear Bibeault ]
 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I get the scheme you're describing. Happen to know if that's documented with a code sample anywhere? (It's kinda hard to Google.)

Otherwise, I'll just start hacking.

Thanks for your help today, Ben & Bear,
Jamie
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
If so, you won't be able to use tags files and you may need to resort to my "poor man's tag files" scheme. It's not that hard really: in your tag handler use the include() method of the pageContext to suck in the "taglet" file. Use scoped variables to communicate between the handler and the "taglet".


A code sample donated to Simple Stuff would be most welcome.
[ January 25, 2005: Message edited by: Ben Souther ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben, I haven't had a chance to check out your stuff yet. I'm looking forward to doing so when I get a chance.

Meanwhile (great minds thinking alike), I had written up a blog entry on this.

For anyone interested in a write up on the "Taglet" mechanism, please check out the link to the Bear Den below.
 
Dan Novik
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check out for example Copy taglib or Cache taglib in Coldtags suite:
http://www.servletsuite.com/jsp.htm
 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks, fellas.

Jamie
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!