II.      ¼øȯÇÏ´Â ÅÂ±× ÀÛ¼º

 

<IterateTag.java>
package tag;

 

import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

import java.util.*;

 

public class IterateTag extends BodyTagSupport{

   private String name;

   private Iterator iterator;

   private String type;

  

   public IterateTag(){super();}

  

   //BodyTag¿¡ ³Ñ°ÜÁø Collection ÀÌ ¿ø¼Ò¸¦ °¡Áö°í ÀÖ´ÂÁö È®ÀÎÇÑ´Ù.

   public void setCollection(Collection collection){

               if(collection.size()>0){

                           iterator = collection.iterator();

               }

   }

  

   public void setName(String name){

               this.name = name;

   }

  

   public void setType(String type){

               this.type = type;

   }

   // Iterator°¡ ÀÖ´ÂÁö È®ÀÎ ÇÏ°í ¾øÀ¸¸é ¸öü󸮸¦ ÇÏÁö¾Ê´Â´Ù.

   // Iterator°¡ ÀÖ´Ù¸é PageContext¿¡ ´ÙÀ½ ¿ø¼Ò¸¦ Ãß°¡ÇÑ´Ù.        

   public int doStartTag() throws JspTagException{

               if(iterator == null){

                           return SKIP_BODY;

               }         

               return addNext(iterator);

   }

  

   public int doAfterBody() throws JspTagException{

               return addNext(iterator);          

   }

   //JSP ÆäÀÌÁö¿¡ BodyContent¸¦ ¸®ÅÏÇÏ°í JSP ÆäÀÌÁö¸¦ °è¼ÓÇÏ¿© ó¸®Çϵµ·Ï ÇÑ´Ù.

   public int doEndTag() throws JspTagException{

               try{

                           if(bodyContent != null){

                                       bodyContent.writeOut(bodyContent.getEnclosingWriter());

                           }

               }catch(java.io.IOException e){

                           throw new JspTagException("IO Error: " + e.getMessage());                           

               }

               return EVAL_PAGE;

   }

  

  

   protected int addNext(Iterator iterator) throws JspTagException{

               if(iterator.hasNext()){

                           pageContext.setAttribute(name,iterator.next(),PageContext.PAGE_SCOPE);

                           return EVAL_BODY_TAG;

               }else{

                           return SKIP_BODY;

               }

   }         

}

 

          III.      TEI Ŭ·¡½ºÀÇ ÀÛ¼º

 

<IterateTEI.class>

 

package tag;

import javax.servlet.jsp.tagext.*;

public class IterateTEI extends TagExtraInfo

{

public IterateTEI()
{

super();

}

public VariableInfo[] getVariableInfo(TagData data)
{

return new VariableInfo[]
{

new VariableInfo(

data.getAttributeString("name"),
data.getAttributeString("type"),
true,
VariableInfo.NESTED

),

};

}

}

 

           

         IV.      ű׶óÀ̺귯¸®¿¡ ¼³¸íÀÚ µî·Ï

 

<tag>

<name>iterate</name>
<tagclass>com.acme.tag.IterateTag</tagclass>
<teiclass>com.acme.tag.IterateTEI</teiclass>
<bodycontent>JSP</bodycontent>
<info>A simple Iterator</info>
<attribute>

<name>collection</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>

</attribute>
<attribute>

<name>name</name>
<required>true</required>

</attribute>
<attribute>

<name>type</name>
<required>true</required>

</attribute>

</tag>

 

           V.      ºóÁî ÀÛ¼º

<Company.java>

               package bean;
 
               import java.util.*;
               import java.io.*;
 
               public class Company implements Serializable {
                   private String name=null;
                   private Collection contacts=new HashSet();
 
                   private Contact lnkContact;
 
                   public Company(){}
 
                   public String getName()
                   {
                     return name;
                   }
 
                   public void setName(String name) {
                     this.name = name;
                   }
 
                   public Collection getContacts()
                   {
                     return contacts;
                   }
 
                   public void addContact(Contact contact)
                   {
                               contacts.add(contact);
                   }
               }

 

<Contact.java>

               package com.acme.bean;
 
import java.util.*;
import java.io.*;
 
public class Contact implements Serializable {
  private String name=null;
  private String phone=null;
  private String email=null;
  private String comment=null;
 
  public Contact(){}
 
  public String getName() {
    return name;
  }
 
   public void setName(String name) {
     this.name = name;
   }
 
   public String getPhone() {
    return phone;
   }
 
   public void setPhone(String phone) {
     this.phone = phone;
   }
 
   public String getEmail() {
    return email;
   }
 
  public void setEmail(String email) {
    this.email = email;
  }
 
   public String getComment() {
    return comment;
   }
  public void setComment(String comment) {
    this.comment = comment;
  }
}

 

 

         VI.      JSP ÆÄÀÏ ÀÛ¼º

 

<%@ taglib uri="taglibExample" prefix="mt" %>
<%

bean.Company comp=new bean.Company();
comp.setName("Acme");
bean.Contact contact1=new bean.Contact();
contact1.setName("Bar, Foo");
contact1.setEmail("foo.bar@acme.com");
contact1.setPhone("+46 8 12 34 56");
contact1.setComment("Excellent with HTML");
bean.Contact contact2=new bean.Contact();
contact2.setName("Doe, Joe");
contact2.setEmail("joe.doe@acme.com");
contact2.setPhone("+46 8 65 43 21");
contact2.setComment("Very good at XML");
bean.Contact contact3=new bean.Contact();
contact3.setName("Doe, Jane");
contact3.setEmail("jane.doe@acme.com");
contact3.setPhone("+46 8 65 43 21");
contact3.setComment("JSP pro");
comp.addContact(contact1);
comp.addContact(contact2);
comp.addContact(contact3);
request.setAttribute("company",comp);

%>

<HTML>

<HEAD>

<TITLE>My Connections</TITLE>

</HEAD>
<BODY BGCOLOR="#FFFFFF">

<jsp:useBean id="company" scope="request" type="com.acme.bean.Company"/>
<FONT SIZE=+2>My connections at
<B><jsp:getProperty name="company" property="name"/></B></FONT>
<HR>
<TABLE>

<TR>

<TD>Name</TD>
<TD>Phone</TD>
<TD>Email</TD>
<TD>Comment</TD>

</TR>

<mt:iterate name="contact" collection="<%= company.getContacts()%>" type="com.acme.bean.Contact">
<TR>

<TD><jsp:getProperty name="contact" property="name"/></TD>
<TD><jsp:getProperty name="contact" property="phone"/></TD>
<TD><jsp:getProperty name="contact" property="email"/></TD>
<TD><jsp:getProperty name="contact" property="comment"/></TD>

</TR>
</mt:iterate>

</TABLE>

<HR>

</BODY>

</HTML>

 

2003-07-27