posts - 495,  comments - 11,  trackbacks - 0

          JSP2.0中為了簡化標簽的復雜性,增加了制作Simple Tag的標簽類SimpleTagSupport類。
          SimpleTagSupport類是實現SimpleTag接口的。它只需要實現一個doTag()方法即可,而不需要一堆回傳值。

          舉例說明:
          例1:HelloSimpleTag標簽
          第一步:制作標簽處理類
          HelloSimpleTag.java

          package com.newould.taglib;

          import java.io.*;
          import javax.servlet.jsp.*;
          import javax.servlet.jsp.tagext.*;

          public class HelloSimpleTag extends SimpleTagSupport {

          public void doTag() throws JspException, IOException {
          ??
          ????? JspWriter out = getJspContext().getOut();
          ????? out.println("Hello Simple Tag");
          }
          }

          第二步:編寫標簽性質文件
          MyTaglib.tld

          <?xml version="1.0" encoding="UTF-8" ?>

          <taglib xmlns=" ?????? xmlns:xsi=" ?????? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
          ?????? version="2.0">
          ????
          ?????? <description>My Taglib by JavaWorld.com.tw</description>
          ?????? <tlib-version>1.0</tlib-version>
          ?????? <jsp-version>2.0</jsp-version>
          ?????? <short-name>Mytaglib</short-name>
          ?????? <uri></uri>
          ......

          <tag>
          ???????? <description>Hello Simple Tag</description>
          ???????? <name>HelloSimpleTag</name>
          ???????? <tag-class>com.newould.taglib.HelloSimpleTag</tag-class>
          ???????? <body-content>empty</body-content>
          ?????? </tag>
          </taglib>

          第三步:編寫Jsp網頁
          HelloSimpleTag.jsp

          <%@ page contentType="text/html;charset=GB2312" %>
          <%@ taglib uri="/WEB-INF/tlds/MyTaglib.tld" prefix="mytag" %>

          <html>
          <head>
          <title>HelloSimpleTag.jsp</title>
          </head>
          <body>

          <h2>Simple Tag 標簽</h2>

          <h1><mytag:HelloSimpleTag /></h1>

          </body>
          </html>

          =================================================================

          例2:AddSimpleTag標簽
          第一步:制作標簽處理類
          AddSimpleTag.java

          package com.newould.taglib;

          import java.io.*;
          import javax.servlet.jsp.*;
          import javax.servlet.jsp.tagext.*;

          public class AddSimpleTag extends SimpleTagSupport {

          private int num1 = 0;
          private int num2 = 0;

          public void setNum1(int num1) {
          ????? this.num1 = num1;
          }

          public void setNum2(int num2) {
          ????? this.num2 = num2;
          }

          public void doTag() throws JspException, IOException {

          ????? JspContext ctx = getJspContext();
          ????? JspWriter out = ctx.getOut();
          ???
          ????? int sum = num1 + num2;
          ????? ctx.setAttribute("sum", Integer.toString(sum));
          ??
          ????? out.println(num1 + " + " + num2 + " = " + sum);
          }
          }

          第二步:編寫標簽性質文件
          MyTaglib.tld

          <?xml version="1.0" encoding="UTF-8" ?>

          <taglib xmlns=" ?????? xmlns:xsi=" ?????? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
          ?????? version="2.0">
          ????
          ?????? <description>My Taglib by JavaWorld.com.tw</description>
          ?????? <tlib-version>1.0</tlib-version>
          ?????? <jsp-version>2.0</jsp-version>
          ?????? <short-name>Mytaglib</short-name>
          ?????? <uri></uri>
          ......

          <tag>
          ???????? <description>Add Simple Tag</description>
          ???????? <name>Add</name>
          ???????? <tag-class>com.newould.taglib.AddSimpleTag</tag-class>
          ???????? <body-content>empty</body-content>
          ??????
          ???????? <attribute>
          ?????????? <name>num1</name>
          ?????????? <required>true</required>
          ?????????? <rtexprvalue>true</rtexprvalue>
          ???????? </attribute>

          ???????? <attribute>
          ?????????? <name>num2</name>
          ?????????? <required>true</required>
          ?????????? <rtexprvalue>true</rtexprvalue>
          ???????? </attribute>
          ????????????
          ?????? </tag>
          ???
          </taglib>

          第三步:編寫Jsp網頁
          AddSimpleTag.jsp

          %@ page contentType="text/html;charset=GB2312" %>
          <%@ taglib uri="/WEB-INF/tlds/MyTaglib.tld" prefix="mytag" %>

          <html>
          <head>
          <title>AddSimpleTag.jsp</title>
          </head>
          <body>

          <h2>AddSimpleTag 標簽</h2>

          <h1><mytag:Add num1="5" num2="9" /></h1>

          最后結果:${sum}

          </body>
          </html>

          =================================================================

          例3 RepeatSimpleTag標簽
          RepeatSimpleTag標簽 主要是用來重復顯示某段文字。
          這個例子在處理上與前兩個例子有點不同

          第一步:制作標簽處理類
          RepeatSimpleTag.java

          package com.newould.taglib;

          import java.io.*;
          import javax.servlet.jsp.*;
          import javax.servlet.jsp.tagext.*;

          public class RepeatSimpleTag extends SimpleTagSupport {

          private int count = 0;//重復的次數
          private JspFragment fragment;//重復的內容

          public void setCount(int count) {
          ????? this.count = count;
          }

          public void setFragment(JspFragment fragment) {
          ????? this.fragment = fragment;
          }

          public void doTag() throws JspException, IOException {

          ????? JspContext ctx = getJspContext();
          ????? JspWriter out = ctx.getOut();
          ???
          ????? for(int i=0 ; i<count ; i++) {
          ?????? fragment.invoke(null);//表示將fragment的內容顯示出來
          ????? }
          }
          }

          第二步:編寫標簽性質文件
          MyTaglib.tld

          <?xml version="1.0" encoding="UTF-8" ?>

          <taglib xmlns=" ?????? xmlns:xsi=" ?????? xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
          ?????? version="2.0">
          ????
          ?????? <description>My Taglib by JavaWorld.com.tw</description>
          ?????? <tlib-version>1.0</tlib-version>
          ?????? <jsp-version>2.0</jsp-version>
          ?????? <short-name>Mytaglib</short-name>
          ?????? <uri></uri>
          ......

          <tag>
          ???????? <description>Repeate Simple Tag</description>
          ???????? <name>Repeat</name>
          ???????? <tag-class>com.newould.taglib.RepeatSimpleTag</tag-class>
          ???????? <body-content>empty</body-content>
          ??????
          ???????? <attribute>
          ?????????? <name>count</name>
          ?????????? <required>true</required>
          ?????????? <rtexprvalue>true</rtexprvalue>
          ???????? </attribute>

          ???????? <attribute>
          ?????????? <name>fragment</name>
          ?????????? <required>true</required>
          ?????????? <fragment>true</fragment>
          ???????? </attribute>
          ?????? </tag>????
          ???
          </taglib>

          注意:<fragment>true</fragment>,一定要這樣設定fragment屬性。

          第三步:編寫Jsp網頁
          RepeatSimpleTag.jsp

          <%@ page contentType="text/html;charset=GB2312" %>
          <%@ taglib uri="/WEB-INF/tlds/MyTaglib.tld" prefix="mytag" %>

          <html>
          <head>
          <title>RepeatSimpleTag.jsp</title>
          </head>
          <body>

          <h2>RepeatSimpleTag 標簽</h2>

          <mytag:Repeat count="5" >
          <jsp:attribute name="fragment">
          重復執行 ....<br>
          </jsp:attribute>
          </mytag:Repeat>
          </body>
          </html>

          =================================================================

          DynamicAttributes接口
          只要制作的標簽實現了DynamicAttributes接口就有動態屬性的功能。
          例如:我們要做多個數的累加運算,則AddSimpleTag標簽就可以通過實現DynamicAttributes接口就可以實現了.

          實現DynamicAttributes接口,必須實現setDynamicAttributes()方法,此方法用來接收動態屬性.

          舉例:第一步:制作標簽處理類

          package com.newould.taglib;

          import java.io.*;
          import java.util.*;
          import javax.servlet.jsp.*;
          import javax.servlet.jsp.tagext.*;

          public class DynamicAdd extends SimpleTagSupport implements DynamicAttributes {

          //用來接收動態屬性
          private ArrayList keys = new ArrayList();
          private ArrayList values = new ArrayList();

          public void doTag() throws JspException, IOException {

          ????? JspContext ctx = getJspContext();
          ????? JspWriter out = ctx.getOut();
          ???
          ????? float num = 0;
          ????? float sum = Float.parseFloat((String)values.get(0));
          ????? out.print(sum);
          ???
          ????? for (int i = 1 ; i < keys.size() ; i++) {
          ?????? String temp = (String)values.get(i);
          ?????? num = Float.parseFloat(temp);
          ?????? sum = sum + num;
          ?????? out.print(" + " + num);
          ????? }
          ???
          ????? out.print(" = " + sum);
          ????? ctx.setAttribute("sum", Float.toString(sum));
          ???
          }

          public void setDynamicAttribute(String uri, String name, Object value) throws JspException {
          ????? keys.add(name);
          ????? values.add(value);
          }
          }

          第二步:編寫標簽性質文件
          <tag>
          ???????? <description>DynamicAttribute</description>
          ???????? <name>DynAdd</name>
          ???????? <tag-class>com.newould.taglib.DynamicAdd</tag-class>
          ???????? <body-content>empty</body-content>

          ???????? <dynamic-attributes>true</dynamic-attributes>
          ?????? </tag>????

          第三步:編寫Jsp網頁
          <%@ page contentType="text/html;charset=GB2312" %>
          <%@ taglib prefix="JSPBook" tagdir="/WEB-INF/tags/" %>
          <%@ taglib prefix="c" uri="

          <html>
          <head>
          <title>CH16 - DynAdd.jsp</title>
          </head>
          <body>

          <h2>Tag File 范例</h2>

          <JSPBook:DynAdd num1="111" num2="222" num3="444" >

          <jsp:attribute name="great">
          ????? <font color="red">SUM:${sum} ...</red>
          </jsp:attribute>
          <jsp:attribute name="less">
          ????? <font color="blue">SUM:${sum} ...</red>
          </jsp:attribute>
          </JSPBook:DynAdd>

          </body>
          </html>
          JSP2.0中為了簡化標簽的復雜性,增加了制作Simple Tag的標簽類SimpleTagSupport類。
          SimpleTagSupport類是實現SimpleTag接口的。它只需要實現一個doTag()方法即可,而不需要一堆回傳值。


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 尚志市| 宁乡县| 长葛市| 瓦房店市| 兴国县| 彭阳县| 淅川县| 孟村| 河间市| 荥阳市| 贞丰县| 漳州市| 祁阳县| 邛崃市| 成都市| 吴川市| 新化县| 唐河县| 卢湾区| 大同县| 中西区| 东方市| 桐乡市| 灵武市| 中方县| 康平县| 天津市| 桂东县| 和林格尔县| 沧州市| 宝丰县| 栾城县| 武功县| 扶余县| 正安县| 长阳| 牡丹江市| 乌苏市| 东乡族自治县| 海阳市| 德保县|