What's Taglib?
Taglib is a JSP extension feature to allow developer to define complex UI interactivation in java code. It can simplify the code and logical in JSP page and we can control the JSP page display in java code.
How to implement Taglb?
Any java code that wants control JSP page display through Taglib should extend class: javax.servlet.jsp.tagext.TagSupport provided by JSP container.
javax.servlet.jsp.tagext.TagSupport
There are two methods in TagSupport:
public int doStartTag() ;
When JSP parsing element "<", "doStartTag()" method will be invoked. Normally this method is used for business logical execution.
public int doEndTag();
When JSP parsing element ">", "doEndTag()" method will be invoked. Normally this method is used to output UI display.
SetXXX: Transmit parameters to TagSupport
Any attibute in elements' definition equivalent to a "setXXX" method in java code that implement "TagSupport".
This "setXXX" method is used to transmit parameters from JSP page to Java code.
How to use Taglib in JSP Page?
Like any other JSP technologies, JSP container will manage the JSP display according to your Taglib implementation. So how to notice JSP container your Taglib implementation?
- Step 1: define your implementation in TLD (tag libraries descriptor)
- Step 2: import your TLD in JSP file.
- Step 3: Using taglib as a normal html tag: <myTag:doSomething attribute1="*"/>