aspose file tools*
The moose likes JSP and the fly likes How can I consolidate some duplicate JSTL code in my JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How can I consolidate some duplicate JSTL code in my JSP?" Watch "How can I consolidate some duplicate JSTL code in my JSP?" New topic
Author

How can I consolidate some duplicate JSTL code in my JSP?

John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
Hi. Wow. I haven't been here in ages. It's good to be using Java again (last job was almost all C for a few years) and it's good to be on JavaRanch again.

Anyhow, I've just started using JSP's/Struts, and have a JSP page with some duplicate JSTL code in it. How can I encapsulate that code to avoid duplication? I want to consolidate it somehow. It looks like:

<c:if test="${}">
... some html code (less than 10 lines), possibly containing some EL expressions...
</c:if>

I don't think it's enough code to make it into its own .tag file. Is there a simple way to gather up the code into its own little variable, then later use it like ${code} when I need it?

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

I use tag files for even single-line elements that get used over and over again.

I'd highly recommend using such a mechanism over some arcane mechanism that will make the page confusing.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

P.S. Welcome back!
Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107
Are you using JSP 1.2? If so a .tag file is the only way you will get it to work like that ${code}. But really it would look like this:

In your JSP...



======================

include.tag file:



======================

If you are using an older version of JSP (1.1-), which it does not look like you are, then I think your only option is to use an include.



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

Joined: Jan 10, 2002
Posts: 61010
    
  65

Are you using JSP 1.2? If so a .tag file is the only way you will get it to work


Tag files were introduced with JSP 2.0 and are not available for JSP 1.2.
Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107
Thanks Bear. I got my vesions mixed up.
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
Thanks for the quick replies.

Right. I'm using the newer JSP 2.0 implementation where I can just whip up a .tag file rather than having to make a java class. That is, I've got that:

<%@taglib prefix="foo" tagdir="/WEB-INF/tags" %>

directive at the top of my .jsp file already for another .tag file that's being used.

Rusty, regarding your example .tag file, shouldn't

<%@ attribute name="myTest" required="true" rtexprvalue="true" %>
be
<%@ attribute name="theTest" required="true" rtexprvalue="true" %>

and shouldn't your example tag file be named myTagFiles.tag instead
of include.tag?

The way it is on my system, in your JSP page, you just mention the prefix and
then the WEB-INF/tags directory in the directive at the top of the JSP file. After
that, any tag you use, like <foo:za>, gets implemented in a WEB-INF/tags/za.tag file.

Also, what does rtexprvalue mean?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Also, what does rtexprvalue mean?


It allows run-time expression values to be used for the attribute.

And yes, you can associate a folder with a prefix so that any tags in that folder are mapped to the prefix, or you can be explicit and define the tag file in a TLD (which is the only way you can mix tags implemented via tag files and tags implemented via tag handlers to share the same prefix).
[ June 10, 2005: Message edited by: Bear Bibeault ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I consolidate some duplicate JSTL code in my JSP?