File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Building a custom taglib without a JAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Building a custom taglib without a JAR" Watch "Building a custom taglib without a JAR" New topic
Author

Building a custom taglib without a JAR

Stefan Mohr
Greenhorn

Joined: Mar 11, 2008
Posts: 14
Hi guys,

I'm having trouble getting my custom tags to show up in MyEclipse.. I suspect I've got something wrong with the way the implementation is declared.

I did a test using one of the Jakarta taglibs and MyEclipse picked it up (in the visual JSP editor's typeahead).

I followed the same setup steps for my own, except I didn't bundle my taglib implementation into a JAR.

web.xml:


Home.jspx:


I then created a myTagLib.tld and placed it in /WEB-INF
myTagLib.tld:


Then in HomeBean.java, I implemented inside the HomeBean class:



The Jakarta sample taglib I used worked fine because there was also a JAR that I threw into /WEB-INF/lib

For my own code I don't really want to split it off from my backing bean (I'd like all of my business logic to live in one place since it's only related to the Home page and will never grow to an unmanageable size).


So what's the missing link? On checking and double checking, my package names and URIs seem consistent. I am left thinking that I need some other tag in one of the descriptors to prevent it from just looking into the WEB-INF/lib folder.

(Edit for world class spelling)

[ March 20, 2008: Message edited by: Stefan Mohr ]
[ March 20, 2008: Message edited by: Stefan Mohr ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Two things:

1) Why are you using jspx format? That's not well-suited to hand coding.

2) Your function signature in the TLD must specify fully qualified class names. E.g. java.lang.String.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Mohr
Greenhorn

Joined: Mar 11, 2008
Posts: 14
Thanks!

I've been using ICEfaces and virtually all of the tutorials I'd followed use a jspx file extension (though the actual differences between JSP and JSPX aren't known to me and I have a feeling my code really is just JSP). It's been working so lower on my list of things to sort out.

I fully qualified the String return type and didn't see any difference. Could my problem be something more fundamental like it doesn't like me pointing to a method inside my backing bean as opposed to a dedicated tag class?

Some older taglib samples I've been looking over all implement a function that implements javax.servlet.jsp.tagext.Tag and have the code inside a doStartTag() method. I'm assuming that the way I'm trying to do it is a JSP 2.0 style that is legal.

All of the tags I intend to use are just Booleans - at this stage I only need them to perform some business logic so my <c:if> can tell me if I should render a control for particular user types.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Originally posted by Stefan Mohr:
and I have a feeling my code really is just JSP). It's been working so lower on my list of things to sort out.
Nope. The <jsp:root> element is part of JSP Document syntax, not JSP. I highly suggest using JSP syntax. The Document syntax is very rigorous and before long you'll start running into spending more time fighting markup syntax than programming.

I fully qualified the String return type and didn't see any difference.
The fact that you weren't getting an exception before indicates that something is wrong at a higher level (but sooner or later you;d find out about the fully-qualified requirement). Are there no errors in the logs?

Could my problem be something more fundamental like it doesn't like me pointing to a method inside my backing bean as opposed to a dedicated tag class?
Because the method is static, it can be in any class in the classpath.

Some older taglib samples I've been looking over all implement a function that implements javax.servlet.jsp.tagext.Tag
That's for implementing custom tags. You are implementing an EL function.
Stefan Mohr
Greenhorn

Joined: Mar 11, 2008
Posts: 14
Nope. The <jsp:root> element is part of JSP Document syntax, not JSP. I highly suggest using JSP syntax. The Document syntax is very rigorous and before long you'll start running into spending more time fighting markup syntax than programming.


Thanks for the early warning - I'll go down that road, then. My initial reasoning for avoiding the "old" JSP-style syntax was that I had the feeling that JSP Document was the new "proper" way of doing things.

The fact that you weren't getting an exception before indicates that something is wrong at a higher level (but sooner or later you;d find out about the fully-qualified requirement). Are there no errors in the logs?


Nothing in the logs, which is really odd. There are only a few lines reporting the page was loaded and which stylesheet was loaded.

Because the method is static, it can be in any class in the classpath.


That's for implementing custom tags. You are implementing an EL function.


That brought about my "duh" moment - I started to blur the distinction between tags and EL.

I never posted my invocation in the jspx - having flipped through Java Enterprise in a Nutshell for a bit (in the EL area, not the tag area!), I think I'm just calling it wrong.



My book indicates I should be calling an instance of an class member in request/session/application scope. Since I'm calling a static function, am I allowed to just make a call like this?

Thanks a bunch for your help, Bear.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Originally posted by Stefan Mohr:
My initial reasoning for avoiding the "old" JSP-style syntax was that I had the feeling that JSP Document was the new "proper" way of doing things.
No. The JSP Document format is intended for automated tools and container internals where an XML DOM is easier to manage programatically. It is not intended for human generation or consumption.

am I allowed to just make a call like this?
Once an EL function is declared in a namespace (let's say xyz), calling it would use syntax:

[ March 20, 2008: Message edited by: Bear Bibeault ]
Stefan Mohr
Greenhorn

Joined: Mar 11, 2008
Posts: 14
Ah! Well my invocation syntax was certainly the problem!
I resolved that and found another problem in my TLD:

I previously had my function-class in my TLD set to com.mypackage.web.beans.HomeBean.goJim - but goJim is the function name.
Changed it to com.mypackage.web.beans.HomeBean.

Following the syntax of another forum post, I feel as though this lines up perfectly with what another person was doing (which they reported working).

Since my function has no parameters, I am invoking it with:



I'm now, happily (!!!) seeing some exceptions, but nothing that's giving me a strong clue:



My function is returning a hard-coded java.lang.String, so I don't think it's the return type that's causing the NullPointerException.

It seems like such a trivial example to be giving me problems... Can this be a simple syntax problem or could I have something misconfigured with my project itself?
[ March 22, 2008: Message edited by: Stefan Mohr ]
Stefan Mohr
Greenhorn

Joined: Mar 11, 2008
Posts: 14
(stupid) issue resolved - it was an initial misunderstanding of what the TLD is actually saying.

I had:


Note that I named my function Jim, yet the function signature (and as it was declared in code) was goJim. NOT the same name, genius!

Probably crept in when I was banging in short form names and descriptions in other places and didn't make the connection.

Thanks again very much for giving it your attention, Bear - you did point out several other things I was doing not-so-hot that I've learned from.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Building a custom taglib without a JAR