Áö±Ý±îÁö´Â tag Çϳª¸¸ ¾²ÀÏ °æ¿ì¿¡ ´ëÇؼ­¸¸ »ìÆ캸¾Ò½À´Ï´Ù.

ÇÏÁö¸¸, Á»´õ µ¿ÀûÀΠű׸¦ ¾µ °æ¿ì¿¡´Â µÎ°³ ÀÌ»óÀÇ Å±׸¦ ½á¾ß ÇÕ´Ï´Ù.

´ç¿¬ÇÑ ¸»ÀÌÁö¸¸ µÎ°³ ÀÌ»óÀÇ Å±װ¡ ¾²ÀÏ °æ¿ì¿¡´Â ÇϳªÀÇ Å±×(child)´Â ´Ù¸¥ Çϳª(parent)ÀÇ Å±׸¦ ÂüÁ¶ÇØ¾ß ÇÕ´Ï´Ù.

 

ÀÌ·¸°Ô ´Ù¸¥ ű׸¦ ÂüÁ¶Çϱâ À§Çؼ­´Â Áö³­¹ø °­Á¿¡¼­µµ ¾ð±ÞÇßµíÀÌ getParent() ¸Þ¼­µå¸¦ ¾µ¼öµµ ÀÖÁö¸¸, TagSupport Ŭ·¡½ºÀÇ findAncestorWithClass() ¸Þ¼­µå¸¦ »ç¿ë ÇÒ °ÍÀÔ´Ï´Ù.

 

À̹ø °­Á¿¡¼­´Â ÀÌ·¸°Ô Çù·Â°ü°è¿¡ Àִ ű×Áß switch/case ¸¦ ±¸ÇöÇØ º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.

 

tag.CaseTag

 

+CaseTag()

+doStartTag():int

 

tag.SwitchTag

Value:String

+SwitchTag()

+setValue(value:String):void

+getValue():String

+doStartTag():int

 

 

 

 

 

 

 


Javax.servlet.jsp.tagext.TagSupport

 

 

TagSupport Ŭ·¡½ºÀÇ static ¸Þ¼­µåÀÎ findAncestorWithClass() ¸Þ¼­µå´Â getParent()¸Þ¼­µå¸¦ »ç¿ëÇÏ¿©

JSP PageÀÇ Ã³À½±îÁö °Å½½·¯ ¿Ã¶ó°¡¸ç ƯÁ¤ÇÑ Å±×ÀÇ ÀνºÅϽº¸¦ ã´Â´Ù.

ã±â¿¡ ½ÇÆÐÇϸé nullÀ» ¸®ÅÏÇÑ´Ù.

 

I.                    Switch Tag ÀÇ ÀÛ¼º

Tag.SwitchTag

Value:String

+SwitchTag()

+setValue(value:String):void

+getValue():String

+doStartTag():int

 

javax.servlet.jsp.tagext.TagSupport

 

 

 

<SwitchTag.java>

package tag;

import javax.servlet.jsp.tagext.*;

public class SwitchTag extends Tagsupport{

             String value;

             public SwitchTag(){super();}

             public void setValue(String value){

                           this.value = value;

             }

             public String getValue(){

                           return value;

             }

             // ¸öü¸¦ ó¸® ÇÒ ¼ö ÀÖµµ·Ï ÇÑ´Ù.

             public int doStartTag(){

                           return EVAL_BODY_INCLUDE;

             }

}

 

II.                  Case Tag ÀÇ ÀÛ¼º

Tag.CaseTag

 

+CaseTag()

+doStartTag():int

 

Tag.SwitchTag

Value:String

+SwitchTag()

+setValue(value:String):void

+getValue():String

+doStartTag():int

 

 

 

 

 

 

 

 

 

 

<CaseTag.java>

 

package tag;

 

import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

 

public class CaseTag extends SwitchTag{

             public SwitchTag(){super();}

             public int doStartTag() throws JspTagException{

                           SwitchTag parent = (SwitchTag)findAncestorWithClass(this,SwitchTag.class);

                           If(parent == null){

                                        throw new JspTagException(¡°Case Tag without Switch Tag¡±);

                           }

                           try{

                                        if(parent.getValue().equals(getValue()){

                                                     return EVAL_BODY_INCLUDE;

                                        }else{

                                                     return SKIP_BODY;

                                        }

                           }catch(NullPointerException e){

                                        if(parent.getValue() == null && getValue == null){

                                                     return EVAL_BODY_INCLUDE;

                                        }else{

                                                     return SKIP_BODY;

                                        }

                           }                        

}

}

 

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

<mytag.tld>

-- Switch ű×

<tag>

<name>switch</name>
<tagclass> tag.SwitchTag</tagclass>
<bodycontent>JSP</bodycontent>
<info>A simple switch tag</info>
<attribute>

<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

-- Case ű×

<tag>

<name>case</name>
<tagclass> tag.CaseTag</tagclass>
<bodycontent>JSP</bodycontent>
<info>A simple case Tag</info>
<attribute>

<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>

</attribute>

</tag>

 

 

IV.               JSPÆÄÀÏ ÀÛ¼º

<switch.jsp>

<%@ page contentType=¡±text/html;charset=EUC-KR¡± %>

<%@ taglib uri=¡±taglibExample¡± prefix=¡±mt¡± %>

<HTML>

<HEAD>

<TITLE>Switch Example</TITLE>

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

<P>This example will show how to use collaborating Tags.</P>
<mt:switch value="dark">

<mt:case value="light"><P>This is light.</P></mt:case>
<mt:case value="Dark"><P>This is Dark.</P></mt:case>
<mt:case value="dark"><P>This is dark.</P></mt:case>

</mt:switch>

</BODY>

</HTML>

 

<mt:switch value=¡±dark¡±> ÀÇ ºÎºÐÀ»

<mt:switch value=¡¯<%=request.getParameter(¡°color¡±)%> À¸·Î ¹Ù²ã¼­ Å×½ºÆ®µµ Çغ¸±â¹Ù¶ø´Ï´Ù.

=>http://localhost/tag_tu/switch.jsp?color=light