JSP: Créer un tag avec des attributs*

Author:
       
///
  


  /java2s
  /WEB-INF/java2s.tld


// create Fichier:java2s.tld in the /WEB-INF/


    

  1.0
  1.2
  Java2s Simple Tags

  
  
    emptyTagWithAttrs
    com.java2s.EmptyTagWithAttrs
    empty
    
      howMany
    
  

//compile the following code into WEB-INFclassescomjava2s

package com.java2s;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/* This tag handler generates the random numbers. The "howMany" attribute
   specifies how many numbers to generate and display.
 */

public class EmptyTagWithAttrs extends TagSupport
{
  // Code to implement the "howMany" attribute

  private int howMany;
  public int getHowMany()
  {
    return howMany;
  }
  public void setHowMany(int i)
  {
    howMany = i;
  }
  
  public int doStartTag() throws JspException
  {
    try 
    {
      JspWriter out = pageContext.getOut();

      for ( int i=0; i" + nextNumber + "");
      } // end of for ()
    }
    catch (IOException e) 
    {
      System.out.println("Error in EmptyTagWithAttrs.doStartTag()");
      e.printStackTrace();
      throw new JspException(e); // throw the error to the error page (if set)
    } // end of try-catch

    return SKIP_BODY;
  }
}


// start comcat and load the following jsp page in browser

<%@ taglib uri="/java2s" prefix="java2s" %>


  
    A custom tag: empty with attributes
  
  
    This page uses a custom tag that has no body content, but takes
    an attribute called "howMany" that specifies how many random
    numbers to generate. Here is its output:
    
               
                      

A Voir sur le même Sujet:

  • java class exemple
  • créer une collection jsp

Leave a Reply

Your email address will not be published. Required fields are marked *