JSP 2.x custom tag and jsp2.x custom tag
JSP 1.x labels are flexible but complex. JSP 2.x provides a set of simplified tag syntax.
SimpleTagSupportIs a subclass of the SimpleTag interface. It also supports parameters and tag bodies. doTage is the core method.
Public AddTag extends SimpleTagSupport {private int num1; private int num2; public void doTag () throws JspException, IOException {this. getJspContext. getOut (). write ("the sum of two numbers equals to:" + (num1 + num2);} // The setter and getter methods are omitted}
The same function is used to calculate the sum of two numbers. The configuration is the same as that of JSP1.x.
The tag body of JSP 1.x is injected to BodyTag through setBodyContent and obtained through getBodyContent. SimpleTagJspFragmentObject instance implementation
// This label is used to convert the characters into public void doTag () throws JspException, IOException {StringWriter writer = newStringWriter (); JspFragment jspFragment = this. getJspBody (); // output to the specified writer through invoke. If the parameter is null, It is output to the default writer, // getJspContext (). getOut () jspFragment. invoke (writer); String content = writer. getBuffer (). toString (); this. getJspContext (). getOut (). print (content. toUpperCase ());}
The configuration is as follows:
<tag> <name>tagname</name> <tagclass>com.chen.tags.TagTest</tagclass> <bodycontent>tagdependent</bodycontent > <info>tag information</info></tag>
Here, <bodycontent> can only be tagdependent, not JSP or bodycontent
And JSP 1. x is different, JSP 2. the x tag can have multiple tag bodies, and the tag bodies can be called in different order and in different times. The <JSP: attribute>, in addition, the tld must be configured in combination with the fragment type.
<attribute> <name>body1</name> <required>false</ required> <fragment>true</fragment ></attribute>