JSP: Custom Tags #37

  • Custom Tags = alternatives to java beans for JSP = reduces some limitations of JavaBeans (in JSP) like to few methods to access the bean, you may still require to write much java code in JSP [that we want to avoid]
  • Overview:
  • Custom tag with no body should extend TagSupport class that implements Tag interface
  • Tags extending TagSupport should extend only one method – doStartTag(), and in this case the doStartTag() method should return SKIP_BODY
  • A simple tag code is shown below:
  • Next you need to define a tld file – a descriptor file for the tag – XML file.
  • In the tld files, the most important tag is the tag
  • The sub-elements of the tag element are: name, tag-class, description, body-content.
  • Sample:
      SimpleTag  tags.SimpleTag  Sample TLD File  empty
  • To use the custom tag in a JSP file, You need to provide a directive like:
     
  • Example use:
  • How to add attributes to tags: In the class, declare private variable for each attribute you want, define a setXXX method for each of the attribute
  • In the tld – file, add an attribute tag for each attribute you want. This attribute tag can have following sub-elements: name, required, rtexprvalue = can the value be an expression, type=class to which the value should typecast to, description
  • Defining tag body: doStartTag() should return EVAL_VALUE_INCLUDE
  • A new method named doEndTag() should be defined that will define what will be the effect on the body text.
  • The doEndTag() method may return EVAL_PAGE = continue to process rest of the JSP page, SKIP_PAGE = stop processing rest of the page
  • To support body inside the custom tags – body-content tag in the tld file needs to be adjusted: example
      SimpleTag  tags.SimpleTag  Sample TLD File  JSP
  • Example use: Body Text

From: http://sitestree.com/?p=4964
Categories:37
Tags:
Post Data:2012-12-20 05:06:38

    Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
    (Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a> 
    In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
    <a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
    8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
    Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
    Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>