JSP custom Label IfElse and traversal Custom Label Example _JSP programming

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.