JSP custom Label IfElse and traversal custom Label example

Source: Internet
Author: User

  This article mainly introduces the JSP custom label IfElse and traverses the custom label, needs the friend to be possible to refer to under

The first example:  a simple JSP custom label 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 gets the contents of the tag body this.getjspbody ();  returns the Jspfragment class, which invokes Invoke (This.getjspcontext (). Getout ());  This method can also be written in empty, the meaning of the expression is output to the browser;  code as follows: public class SimpleDmeo1 extends Simpletagsupport {  @Override   public void Dotag () throws Jspexception, IOException {  jspfragment js =this.getjspbody ();  js.invoke (null); nbsp } }    Then write TLD file Tag library description file, and JSP file, these are simpler   if you do not want to execute a certain content to throw an exception   throw new Skippageexception (); And the surface content will not be displayed   Next is a JSP custom tag file with attributes   code as follows: public class SimpleDmeo1 extends simpletagsupport {  private int Co unts;  public void setcounts (int counts) {  this.counts = counts; }  @Override   public void DoTa G () 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>    then;  the contents of the JSP file the   code is as follows: <hbsi: Demo counts= "3" >aaaaaaa<br/></hbsi:demo>//Output three series   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); } }&NBsp   This is converted to uppercase code, the others are consistent;  about if else code, too much, I put in the resources, 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.