First example:
Simple JSP custom tags to get content:
First create a JSP instance class and then inherit the Simpletagsupport class
Then implement the Dotag () method of the parent class
In this method get the contents of the tag body this.getjspbody ();
Returns the Jspfragment class, which invokes Invoke (This.getjspcontext (). Getout ()) based on this class object.
This method can also write empty, the meaning of the expression is output to the browser;
Copy Code code as follows:
public class SimpleDmeo1 extends Simpletagsupport {
@Override
public void Dotag () throws Jspexception, IOException {
Jspfragment JS =this.getjspbody ();
Js.invoke (NULL);
}
}
Then write the TLD file tag library description file, and JSP file, these are simpler
Throw an exception if you don't want to do something
throw new Skippageexception (), and the surface content is not displayed
Next is a JSP custom label file with attributes
Copy Code code as follows:
public class SimpleDmeo1 extends Simpletagsupport {
private int counts;
public void setcounts (int counts) {
This.counts = counts;
}
@Override
public void Dotag () throws Jspexception, IOException {
Jspfragment JS =this.getjspbody ();
for (int i=0;i<counts;i++) {//Loop fetch
Js.invoke (NULL);
}
}
}
<description>a Tag Library exercising Simpletag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>c</short-name> prefix name
<uri>http://www.csdn.com</uri>
<tag>
<name>demo</name>
<tag-class>com.csdn.simple.SimpleDmeo1</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>counts</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
And then write the content in the JSP file;
Copy Code code as follows:
Jspfragment js = This.getjspbody ();
StringWriter JW = new StringWriter ();
Js.invoke (JW);
String s = jw.tostring (). toUpperCase ();
JspWriter out =this.getjspcontext (). Getout ();
for (int i=0;i<counts2;i++) {
Out.print (s);
}
}
This is converted to uppercase code, the others are consistent;
About if else's code, too much, I put it in the resource, if necessary, can download, for reference only.