By reading the three articles before, already can handle your own custom tag libraries, but if not familiar with the operation principle of custom tag, if you encounter an error may be confused, this article will introduce the operation principle of custom tag.
Achieved the previous instance TagSupport class, and there is no direct use JspTag interface, but in fact tag handling class must be achieved JspTag interfaces. JspTag interface definition in javax.servlet.jsp.tagext package, it is just a marking interface does not define any methods, all tag handling class must implement this interface.
javax.servlet.jsptagext package also includes IterationTag interfaces, BodyTag interfaces, SimpleTag interfaces, TagSupport class, BodyTagSupport class and simpleTagSupport class, inheritance relation between them is as follows:
----------- Tag Interface
| ---------- IterationTag Interface
| --------------------- TagSupport class
| | |
| ---------------- BodyTag Interface |
| | |
| --------- BodyTagSupport class
----------- SimpleTag Interface
-------- SimpleTagSupport class
This interface defines the write tag address classes to achieve the basic methods, including what methods: doStartTag (), doEndTag (), getParent (), release () and setPageContext () method, which doStartTag () and doEndTag () method is the most commonly used method. doStartTag () method in the implementation of the custom tag is called when the start tag, doEndTag () method in the implementation of the custom tag's end tag is called.
Tag in the actual preparation of the class when dealing with the general without directly implement this interface, usually inherit this interface has been achieved or BodyTagSupport TagSupport class.
This interface inherits from the Tag interface and adds a new doAfterBody () method, this method can generally achieve the contents of custom tag that contains the cycle of operation.
doAfterBody () method only if the doStartTag () method returns a value of EVAL_BODY_INCLUDE only when called. Call doAfterBody () method, if the return value is EVAL_BODY_AGAIN, it will again run the custom tag that contains the content and once again call doAfterBody () method until the doAfterBody () method returns a value of SKIP_BODY, so that to realize the custom tag contains operational content.
This class is the most commonly used preparation of custom tag class, in fact it is a helper class that can help us to quickly write tag handler class, he realized IterationTag interfaces. We write tag handler class can inherit the class time, rewrite the way they need, without the need to directly interface to achieve IterationTag or Tag. The design model used here the default adapter mode.
An inherited class that TagSupport tag handling custom tag this tag can display the content contained, but can not edit the contents contained in and transform. If you need to edit the contents contained in the need to inherit BodyTagSupport class
TagSupport class call process is as follows: