posts - 431,  comments - 344,  trackbacks - 0

          package com.rain.tag;

          import Java.io.IOException;

          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.JspTagException;
          import javax.servlet.jsp.tagext.BodyTagSupport;

          /*
           * if tag
           * usage:<tag:if value=true>
           *       ...
           *       </tag:if>
           */
          public class IfTag extends BodyTagSupport {

           private boolean value;
           
           public void setValue(boolean value){
            this.value=value;
           }

           @Override
           public int doEndTag() throws JspException {
            // TODO Auto-generated method stub
            try{
             if(bodyContent!=null){
              bodyContent.writeOut(bodyContent.getEnclosingWriter());
             }
            }catch(IOException e){
             throw new JspTagException("IO Error:"+e.getMessage());
            }
            return EVAL_PAGE;
           }

           @Override
           public int doStartTag() throws JspException {
            // TODO Auto-generated method stub
            if(value){
             System.out.println("value is true");
             return EVAL_BODY_INCLUDE;
            }else{
             System.out.println("value is false");
             return SKIP_BODY;
            }
           }
           
           
          }



          package com.rain.tag;

          import Java.io.IOException;

          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.JspTagException;
          import javax.servlet.jsp.tagext.TagSupport;

          public class OutTag extends TagSupport {

           private Object value;
           
           public void setValue(Object value){
            this.value=value;
           }

           @Override
           public int doEndTag() throws JspException {
            // TODO Auto-generated method stub
            return EVAL_BODY_INCLUDE;
           }

           @Override
           public int doStartTag() throws JspException {
            // TODO Auto-generated method stub
            try{
             System.out.println(value);
             pageContext.getOut().write(value.toString());
            }catch(IOException e){
             throw new JspTagException("IO Error:"+e.getMessage());
            }
            return EVAL_PAGE;
           }
           
           
          }



          <tag>
                    <name>if</name>
                    <tag-class>com.rain.tag.IfTag</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.rain.tag.OutTag</tag-class>
                    <body-content>jsp</body-content>
                    <attribute>
                      <name>value</name>
                      <required>true</required>
                      <rtexprvalue>true</rtexprvalue>
                    </attribute>
                  </tag>


          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "<web-app>
           <taglib>
             <taglib-uri>/demotag</taglib-uri>
             <taglib-location>/WEB-INF/mytag.tld</taglib-location>
           </taglib>
          </web-app>


          <%@ page language="
          Java" contentType="text/html; charset=UTF-8"%>
          <%@ taglib uri="/demotag" prefix="mt" %>
          <html>
          <head>
          <title>Insert title here</title>
          </head>
          <body>
          <%
            boolean flag=true;
            String outValue="Hello World!";
          %>
          <mt:if value="<%=flag%>">
            <mt:out value="<%=outValue%>">
              這是mt:out...>打印的內容。
            </mt:out>
          </mt:if>
          <br>
          <mt:if value="false">
            <mt:out value="<%=outValue%>">
              這些內容不會顯示在客戶端。
            </mt:out>
          </mt:if>
          </body>
          </html>


          結果顯示:
          控制臺
          value is true
          Hello World!
          value is false
          頁面
          Hello World! 這是mt:out...>打印的內容。
          posted on 2007-01-22 15:27 周銳 閱讀(322) 評論(0)  編輯  收藏 所屬分類: Jsp
          主站蜘蛛池模板: 潢川县| 尉氏县| 沈丘县| 松潘县| 怀安县| 平武县| 康保县| 青龙| 乌兰县| 鹤庆县| 富裕县| 上高县| 文水县| 商城县| 策勒县| 德惠市| 桦川县| 兰考县| 黎川县| 富顺县| 仁化县| 昆明市| 嘉祥县| 渭南市| 会同县| 临沂市| 当涂县| 时尚| 同德县| 宜宾县| 衡阳市| 西青区| 丁青县| 汶上县| 城固县| 连云港市| 西吉县| 广昌县| 石渠县| 河津市| 仙居县|