Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add el support to your own taglib

 
Rusty Enisin
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stefan Evans
Bartender
Posts: 1777
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic