80 likes | 246 Views
Creating JSP Custom Tag Libraries. Celsina Bignoli bignolic@smccd.net. Custom Tag Libraries using Java. tag extension mechanism set of classes and interfaces to develop custom actions in JSP tag handler class that implements the behavior of a custom action
E N D
CreatingJSP Custom TagLibraries Celsina Bignoli bignolic@smccd.net
Custom Tag Libraries using Java • tag extension mechanism set of classes and interfaces to develop custom actions in JSP • tag handler class that implements the behavior of a custom action • simple tag handler simplified mechanism for developing custom actions based on a single interface (SimpleTag), introduced in JSP2.0 • does not allow scripting as part of the action body
Tag Library Components • tag handler class that implements the behavior of a custom action • Tag Library Descriptor(TLD)file XML file that maps action names to their tag handler class and describes all attributes supported by each custom action
Tag Handler Class • It is essentially a Bean • Implements SimpleTag interface • Most commonly by extending the SimpleTagSupport class • Methods • setJspContext(JspContext) • doTag() • setter methods for each attributes of the custom action • setJspBody(JspFragment)
Method Calls <prefix:actionName attr1=“value1” attr2=“value2” > body </prefix:actionName> setAttr1(“value1”) setAttr1(“value1”) doTag()
Tag Handler Lifecycle • A new tag handler instance is created each time by the container by calling the provided zero-args constructor. • simple tag handlers are never cached and reused by the JSP container. • The setJspContext() and setParent() methods are called by the container. The setParent() method is only called if the element is nested within another tag invocation. • The setters for each attribute defined for this tag are called by the container. • If a body exists, the setJspBody() method is called by the container to set the body of this tag, as a JspFragment. If the action element is empty in the page, this method is not called at all. • The doTag() method is called by the container. • All tag logic, iteration, body evaluations, etc. occur in this method.
JSP Fragment • the JSP container converts the body of a custom action into an object of type javax.servlet.jsp.tagext.JspFragment • this fragment is associated with the JspContext of the page where it is defined • The container calls the method invoke() on the JspFragment causing all dynamic elements in the fragment to be executed • their output is combined with static template code if any to form an evaluation result
Exceptions • catch exceptions and deal with them or rethrow them as JspException or JspTagException • public JspTagException() • public JspTagException(String message) • public JspTagException(String message, Throwable rootCause) • public JspTagException(Throwable rootCause) • can be caught and handled in a JSP page using JSTL <c:catch> action.