Thursday, April 28, 2011

Quick Recap: Chapters 44 & 45

Now, let us quickly review what we have learnt about Tag Libraries and Custom Tags in the previous two chapters.

Tag Libraries:

• A tag library is similar to JavaBeans, which means that you can reference a class from JSP using XML notation.
• A tag library maps XML tags to underlying class methods in a Tag Library Descriptor (.tld) file
• JavaBeans allow you to separate your applications into presentation code (contained in the JSP) and business logic (contained in the beans). JavaBeans is a good start, but doesn't take us too far. If we want to do stuff, beyond getting and setting values, we need to use custom tags. Because this need made its way into Sun's Java wish list, we now have custom tags
• The main reasons as to why Tag Libraries are used so commonly are: Simple, Portable, Flexible, Expressive and Reusable
• Steps to create a tag library are:
      o Write and compile a simple tag handler that services the custom tag in your JSP (JSP custom tag attribute invokes a setter method in tag handler). Place the tag handler Java class files for your tags in the WEB-INF/classes directory of your Web application.
      o Write the tag library descriptor (TLD), which defines the tag library including the name of the tag handler class and attributes. Place it in its directory, which you declare in the deployment descriptor (CATALINA_HOME/webapps/examples/WEB-INF/xyz.tld).
      o Declare the Tag Library Descriptor in the Web application deployment descriptor (CATALINA_HOME/webapps/examples/WEB-INF/web.xml)
      o Declare the tag library in your JSP source using the JSP directive.
      o Use the custom tag in your JSP page.
      o Restart your container and test your tag library.

Custom Tags:

• The custom tag is a convention that allows you to have the container call the tag library life cycle event methods (for example, doStartTag), including the attribute get/set calls automatically invoked when attributes are declared in the custom tag. doStartTag and doEndTag methods are called when the JSP page request is processed
• A Custom tag has 3 attributes:
      o Name
      o Required and
      o Rtexprval
• A Custom Tag body can be:
      o Empty
      o Custom Tag that surrounds JSP code
      o Custom tag that surrounds contents that is used only by the tag handler.
• The various Events of a Custom Tag are:
      o EVAL_BODY_INCLUDE – Evaluate body into existing output stream
      o EVAL_BODY_AGAIN – Evaluate body again
      o EVAL_PAGE – Continue Evaluating the Page
      o SKIP_BODY – Skip body Evaluation. Stop processing the JSP after the current custom tag
      o SKIP_PAGE – Stop processing the JSP after the current custom tag.

The Key Terms we learnt in these chapters are:

• Custom tags
• Tag libraries
• Tag Library Descriptor
• Web Deployment Descriptor
• Nested tags
• Tag attributes and bodies
• Taglib directive
• doStartTag()
• doEndTag()

Previous Chapter: Chapter 45 - Custom Tags

Next chapter: Self Test - Chapters 44 & 45

No comments:

Post a Comment

© 2013 by All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.