wood burning stoves 2.0*
The moose likes JSP and the fly likes import directives for taglib classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "import directives for taglib classes?" Watch "import directives for taglib classes?" New topic
Author

import directives for taglib classes?

Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
I've been working with a few taglib examples, and have come upon a point of confusion.
Whenever I run the examples, as given, my jsp page will not compile unless I import the classes that import the tags I use on the page. This is not exactly a surprise since the generated servlet code won't magically import those classes.
However, all the example code fails to do this. I am wondering if I am missing something here.
The taglib is declared in web.xml, its tld is located in WEB-INF and supporting classes are in WEB-INF/classes. The webapp has access to the classes (when I put the <%@ page import="SomeTagClass"> directives in the jsp page, it works).
Could it be that the examples just failed to mention that the imports are required in the .jsp pages?
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
Originally posted by Ken Pelletier:
I've been working with a few taglib examples, and have come upon a point of confusion.
Whenever I run the examples, as given, my jsp page will not compile unless I import the classes that import the tags I use on the page. This is not exactly a surprise since the generated servlet code won't magically import those classes.
However, all the example code fails to do this. I am wondering if I am missing something here.
The taglib is declared in web.xml, its tld is located in WEB-INF and supporting classes are in WEB-INF/classes. The webapp has access to the classes (when I put the <%@ page import="SomeTagClass"> directives in the jsp page, it works).
Could it be that the examples just failed to mention that the imports are required in the .jsp pages?

The <%@ page import %> directives are not required on a properly functioning JSP container. You simply need to import the tag library with a <%@ taglib %> directive.
If it fails for you, please respond with (a) the name and version of the container you're using; (b) a sample JSP page that fails; and (c) the error message it gives you when it fails. That'd help us help you track down the problem.
Best,


Shawn Bayern<br />"JSTL in Action" <a href="http://www.jstlbook.com" target="_blank" rel="nofollow">http://www.jstlbook.com</a>
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
Ah, so the container *should* locate the classes from the .tld and make the proper imports in the gen'd servlet for the .jsp page.
I'm using Tomcat 4.0.1, same result on RH7 Linux as on Mac OS X 10.1.5.
Here's the error message:
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 10 in the jsp file: /javascript.jsp
Generated servlet error:
/usr/local/downloads/jakarta/jakarta-tomcat-4.0.1/work/localhost/TaglibTest/javascript$jsp.java:63: Class org.apache.jsp.JavaScriptExampleTag not found.
JavaScriptExampleTag _jspx_th_js_message_0 = new JavaScriptExampleTag();
^
This is based on a simple example from Wrox's Pro JSP book.
The JavaScriptExampleTag.class is indeed inside WEB-INF/classes, and the .tld is in WEB-INF.
Without an import directive, the above error results. With an import directive, the error is avoided, tag works as expected.
The jsp code is (import commented out here):
<%-- <%@ page import="JavaScriptExampleTag" %> --%>
<%@ taglib uri="http://www.mydomain.com/jspkit/javascript" prefix="js" %>
<html>
<head>
</head>
<body>
<p>This is a simple javascript tag test page</p>
<js:message>
This is the body of the js:message tag
</js:message>
</body>
</html>
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
Ah. If you add packages to the classes (both actually and in the TLD), it'll work fine.
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
Thanks for the quick reply.
I'd somehow never written a tag support class that wasn't in a package before, but admit I never knew it was a requirement that they be packaged.
I did notice that the container was trying to instantiate org.apache.jsp.MyTagClass, which puzzled me a bit.
I presume the container uses root level (unpackaged) tag support classes as a special case and considers them to be part of the org.apache.jsp package (?)
Any enlightening explanation would be appreciated.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
It appears that when classes not in packages are encountered, the servlet JVM inevitably looks in the wrong place. Just bite the bullet and always use packages when working with servlets.
Bill
Ken Pelletier
Ranch Hand

Joined: Aug 01, 2002
Posts: 54
Thanks, William.
I normally would organize tag support classes into packages, but just stumbled on this when I coded a quick-and-dirty test w/o packages.
Thanks for the replies. Sorted.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: import directives for taglib classes?
 
Similar Threads
servlet action is not available
jsp
Not able to use Struts Taglibs
Taglib problem
Struts with Resin