Monday, 7 September 2015

User Defined Tags

User Defined tags:

·         All predefined tags are sufficient to remove java code from jsp. So java people released Tag class which is used to created user defined tags
·         Custom tags are collection of user defined tags
·         Procedure to develop User defined tags
o   Identify all tags
§  Add
§  Sub
§  Mul
§  Div
§  Mod
o   Identify tag library name
§  Arithmetic
o   Develop tag handler class
§  This is a class which can use the Tag interface or TagSupport implementation class.
§  We need to implement the doEndTag() to provide the functionality of a tag
§  It will return the integer value (EVIL_PAGE,SKIP_BODY)
§  EVIL_PAGE ----> JSP will execute the remaining part as well
§  SKIP_BODY-----> JSP will skip the remaining body of page
public class AddTH extends TagSupport {
      int one;
      int two;
      public void setOne(int one) {
            this.one = one;
            System.out.println(one);
      }
     
      public void setTwo(int two) {
            this.two = two;
            System.out.println(two);
      }

      public int doEndTag() throws JspException {
            try {
                  JspWriter out = pageContext.getOut();
                  out.print((one+two));
            }catch(IOException e) {
                  System.out.println(e.getMessage());
            }
            return EVAL_PAGE;
      }
}

o   Develop tld file
§  This is a configuration file to specify the tag name as well as java class
§  We need to place in WEB-INF folder
<taglib>
      <tlib-version>1.0</tlib-version>
      <jsp-version>1.2</jsp-version>
      <short-name>arth</short-name>
      <uri>http://www.arth.com/tags/</uri>
      <tag>
            <name>add</name>
            <tag-class>com.ibm.taghandlers.AddTH</tag-class>
            <attribute>
                  <name>one</name>
                  <required>true</required>
            </attribute>
            <attribute>
                  <name>two</name>
                  <required>true</required>
            </attribute>
      </tag>
</taglib>

o   Configure tld in web.xml
  <jsp-config>
    <taglib>
      <taglib-uri>http://www.arth.com/tags/</taglib-uri>
      <taglib-location>/WEB-INF/Arth.tld</taglib-location>
    </taglib>
  </jsp-config>

o   Jsp to use those tags
<%@ taglib uri="http://www.arth.com/tags/" prefix="arth"%>
<arth:add two="10" one="20"></tag:add>

·         Internal process for above class
o   When we use arth:add jsp compiler check the prefix of a tag(arth)
o   JSPC checks the tag lib is configured with this prefix
o   If not configured JSPC will treat it as a template text and display on browser
o   If configured JSPC check for the uri(http://www.arth.com/tags/) and looks into the web.xml file for this with <taglib-uri>
o   Then JSPC checks the tld file for specific uri and opens it check for the tag name(add)


o   JSPC execute the class and return the output to browser. Based on return value JSPC execute the remaining page

No comments:

Post a Comment