Question

I am creating a custom tag library and want to use it in one of my components. I have created a bundle which includes tag hello class which is extending TagSupport class and i created tags.tld file under my resource folder

In my pom.xml, I have used resource tag to include my .tld file in the generated jar file.

Here is my java class and tld file

TAG CLASS:-

package com.cb;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
/**
* Simple tag example to show how content is added to the
* output stream when a tag is encountered in a JSP page.
*/
public class Hello extends TagSupport {
private String name=null;
  /**
   * Getter/Setter for the attribute name
     as defined in the tld file
   * for this tag*/
public void setName(String value){
    name = value;
}
public String getName(){
      return(name);
}
/**
* doStartTag is called by the JSP container
  when the tag is encountered */
public int doStartTag() {
  try {
    JspWriter out = pageContext.getOut();
    out.println("<table border=\"1\">");
     if (name != null)
      out.println("<tr><td> Welcome <b>" + name +
        "</b> </td></tr>");
    else
      out.println("<tr><td> Hello World </td></tr></table>");
  } catch (Exception ex) {
    throw new Error("All is not well in the world.");
  }
  // Must return SKIP_BODY because we are not
       //supporting a body for this
  // tag.
  return SKIP_BODY;
}
/**
* doEndTag is called by the
  JSP container when the tag is closed */
public int doEndTag(){
   return EVAL_PAGE;
}
}

I also successfully installed the bundle in my felix console without having any error. Then i written custom tag in my jsp as below

JSP:-

<%@include file="/libs/foundation/global.jsp"%>
<%@ page import="com.testcb.TestCustomTag"%>
<%@ taglib prefix="mytest" uri="http://cs.test.com/bundles/cq/1.8"%>
<mytest:hello name="sachin"></mytest:hello>

I am getting the like "org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.jsp.jasper.JasperException: /apps/test/components/content/test/test.jsp(4,0) Unable to load tag handler class "com.cb.Hello" for tag "mytest:hello".

The same code is working fine in my apache tomcat server without having any issue. I am getting the error when i incorporate it in CQ.

What am i doing here? Is there any config i need to do in OSGI console to make it available?

UPDATE:

There was some problem with package name. Now Sling can read my tag handler class after i renamed the package name.

The error "Unable to load tag handler class" also has gone.

Now i am getting error as "org.apache.sling.api.scripting.ScriptEvaluationException: javax.servlet.ServletException: javax.servlet.jsp.JspException: com.testcb.TestCustomTag cannot be cast to javax.servlet.jsp.tagext.Tag"

I have the following dependency in pom.xml

<dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
</dependency>

And Here is my tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> 
    <description>My tag library123</description>
    <tlib-version>1.0</tlib-version>
    <short-name>TagLib-Test</short-name>
    <uri>http://cs.test.com/bundles/cq/1.0</uri>
    <jspversion>2.1</jspversion>
    <tag>
      <name>testcustomtag</name>
      <tagclass>com.testcb.TestCustomTag</tagclass>
      <bodycontent>empty</bodycontent>
      <info>This is a simple hello tag</info>
    <attribute>
      <name>name</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    </tag>
</taglib>

Is there any problem with jsp version?

Please guide me to resolve.

Thanks

Was it helpful?

Solution 2

Yes, It is working as expected after i removed tag in pom.xml. This was the cause issues. :)

Thanks !

OTHER TIPS

The tags.tld file needs to be under the META-INF folder. If you don't have it already you can create one under your resources source folder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top