• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Welcome back!
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Bear Bibeault
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear. I got my vesions mixed up.
 
John M. Gabriele
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 67447
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Water proof donuts! Eat them while reading this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic