여러 탭 의 끼 워 넣 기

실제 개발 에 서 는 하나의 임 무 를 수행 하기 위해 여러 개의 태그 가 필요 하 다. 이런 라벨 은 부자 관계 가 존재 한다.우 리 는 프로 세 스 제어 와 관련 된 탭 을 열 수 있 습 니 다. 예 를 들 어:

  <mt:switch value="test">
      <mt:case value="test1">
          my value is test1
      </mt:case>
      <mt:case value="test">
          my valie is test
      </mt:case>
  </mt:switch>

위의 탭 에서 < mt: switch > 는 부모 탭 이 고, < mt: case > 는 하위 탭 이 며, 부모 탭 은 여러 개의 하위 탭 과 HTML, Scriptlets 를 삽입 할 수 있 습 니 다.
구체 적 인 코드 는 다음 과 같다.

  package com.test.ch14
  import javax.servlet.jsp.*;
  import javax.servlet.jsp.tagext.*;
  import java.util.Hashtable;
  import java.io.Writer;
  import java.io.IOException;

public class IfTag extends BodyTagSupport
{
  private boolean value;
  
  public void setValue(boolean value){
      this.value = value;
   }
   /**
    *doStartTag  ,  value true,     TagBody  ,     
    */
   public int doStartTag() throws JspTagException{
     if(value)
     {
        System.out.println("value is true");
        return EVAL_BODY_INCLUDE;
      }else{
        System.out.println("value is false");
        return SKIP_BODY;
      }
    }
   
    //  doEndTag  
    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;
    }
}

Value 는 IfTag 의 속성 입 니 다.Value 가 true 일 때 IfTagBody 를 계산 합 니 다.Value 의 값 이 false 라면;IfTagBody 의 Tag 를 무시 합 니 다.
이 예 에서 IfTag 탭 에 하위 탭 이 삽입 되 어 있 습 니 다. 이 하위 탭 은 클 라 이언 트 에 정 보 를 출력 하 는 데 사 용 됩 니 다. 다음은 IfTag 에 포 함 된 하위 탭 을 보 겠 습 니 다. 예 를 들 어:

 package com.test.ch14;
 import javax.servlet.jsp.*;
 import javax.servlet.jsp.tagext.*;
 import java.util.Hashtable;
 import java.io.Writer;
 import java.io.IOException;

 public class OutTag extends TagSupport
 {
   private Object value;
  
  //  doStartTag  
  public void setValue(Object value){
    this.value = value;
   }

  public int doStartTag() throws JspTagException{
   return EVAL_BODY_INCLUDE;
  }
  //  doEndTag  
  public int doEndTag() throws JspTagException
  {
    try{
           System.out.println(value);
           pageContext.getOut().write(value.toString());
         }catch(IOException ex){
           throw new JspTagException("Fatal error: hello tag conld not write to JSP out");
          }
    return EVAL_PAGE;
    }
  }

OutTag 는 바디 가 없 는 간단 한 탭 으로 클 라 이언 트 에 정 보 를 출력 합 니 다.다음은 태그 설명 파일 을 업데이트 하고 다음 내용 을 추가 해 야 합 니 다.

 <tag>
     <name>if</name>
     <tag-class>com.test.ch14</tag-class>
     <body-content>jsp</body-content>
         <attribute>
           <name>value</name>
           <required>true</required>
           <rtexprvalue>true</rtexprvalue>
         </attribute>
 </tag>
 <tag>
    <name>out</name>
     <tag-class>com.test.ch14.OutTag</tag-class>
     <body-content>jsp</body-content>
         <attribute>
           <name>value</name>
           <required>true</required>
           <rtexprvalue>true</rtexprvalue>
         </attribute>
 </tag>

다음은 새 겨 진 탭 을 사용 하 는 JSP 를 만 듭 니 다.예컨대

 <%@ taglib uri="/demotag" prefix="mt"%>
 <html>
 <head>
 <title>vcorwork tag</title>
 <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
 </head>
 <body>
 <HR>
     <br>
 <%
   boolean test = true;
   String outValue = "HelloWorld"
  %>
  <mt:if value="<%=test%>">
   <mt:out value="<%= outValue%>">
      mt:out....>      。
   </mt:out>
  </mt:if>
 <HR>
 <mt:if value="fale">
  <mt:out value="<%=outValue%>">
               。
  </mt:out>
 </mt:if>
 </body>
 </html>

좋은 웹페이지 즐겨찾기