ݱ 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