Tuesday, April 23, 2013

Custom JSP tags

1) Make sure that the library for tags is in your classpath. servlet-api-2.3.jar supports it.
2) Create the tag class
   Ex.
   public class MyTag extends TagSupport
   public class HelloTag extends SimpleTagSupport
3) Create the tag libray descripto file and put anywhere inside WEB-INF directory
   Ex. WEB-INF/tld/myApp.tld
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>Example TLD</short-name>
  <tag>
    <name>Hello</name>
    <tag-class>com.tutorialspoint.HelloTag</tag-class>
    <body-content>empty</body-content>
  </tag>
</taglib>
4) Use the tag. You can also set uri in tld and use it as your uri in JSP page.
<%@ taglib prefix="ex" uri="WEB-INF/tld/myApp.tld"%>
<html>
  <head>
    <title>A sample custom tag</title>
  </head>
  <body>
    <ex:Hello message="This is custom tag" />
  </body>
</html>

No comments:

Post a Comment