Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSP and the fly likes Add el support to your own taglib Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Add el support to your own taglib" Watch "Add el support to your own taglib" New topic
Author

Add el support to your own taglib

Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107
I am working on an older app server that does not have EL support. Is there a way to add EL support to my own taglib so I can do things like this?

<my:tag value="${param.myParam}"/>

I could parse the string ( ${param.myParam} ) and then get the value. Is there a class out there that I can use that already does this?


The squeaky wheel gets the grease. Well, that or replaced...
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
Certainly there is. I've never actually done it myself, but it should be simple enough (famous last words)

There are two approaches I can think of
1 - Jakarta Commons EL implementation. This is the implementation of the JSP2.0 package javax.servlet.jsp.el. You can then use those classes in your custom tag. Use the JSP2.0 api as a source for the base classes.

2 - Look at existing source code for the JSTL, and the struts-el implementations. You should be able to leverage off of the JSTL1.0 ExpressionEvaluatorManager class. It is in the package: org.apache.taglibs.standard.lang.support
The struts-el tags use that.

Good luck,
evnafets
Rusty Enisin
Ranch Hand

Joined: May 26, 2005
Posts: 107
I finally got around to implementing this. Three steps to personal el bliss.

Step 1: Import these two packages from Apache:


Step 2: Each taglib has different attributes that need to ba handled differently (null, not null are the two biggies). Create a method that, for each attribute for which you want to use el, calls ExpressionUtil.evalNotNull() like this.


Step 3: Then inside the doStartTag of your taglib call the method you created above before you do anything else in the method.


Pretty snappy and works like a charm.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60781
    
  65

Rusty, this is great information for those still stuck in the JSP 1.x world (kudos to Stefan for the pointer).

I have an idea... please contact me via email.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
 
subject: Add el support to your own taglib
 
Similar Threads
Compare between EL and TagLib
using EL in jsp include file
JSTL request.getAttribute(String attName)
EL with Java script inside a JSP
JSTL issue