posts - 495,  comments - 11,  trackbacks - 0

          JSP2.0中為了簡(jiǎn)化標(biāo)簽的復(fù)雜性,增加了制作Simple Tag的標(biāo)簽類SimpleTagSupport類。
          SimpleTagSupport類是實(shí)現(xiàn)SimpleTag接口的。它只需要實(shí)現(xiàn)一個(gè)doTag()方法即可,而不需要一堆回傳值。

          舉例說明:
          例1:HelloSimpleTag標(biāo)簽
          第一步:制作標(biāo)簽處理類
          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");
          }
          }

          第二步:編寫標(biāo)簽性質(zhì)文件
          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網(wǎng)頁
          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 標(biāo)簽</h2>

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

          </body>
          </html>

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

          例2:AddSimpleTag標(biāo)簽
          第一步:制作標(biāo)簽處理類
          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);
          }
          }

          第二步:編寫標(biāo)簽性質(zhì)文件
          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網(wǎng)頁
          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 標(biāo)簽</h2>

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

          最后結(jié)果:${sum}

          </body>
          </html>

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

          例3 RepeatSimpleTag標(biāo)簽
          RepeatSimpleTag標(biāo)簽 主要是用來重復(fù)顯示某段文字。
          這個(gè)例子在處理上與前兩個(gè)例子有點(diǎn)不同

          第一步:制作標(biāo)簽處理類
          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;//重復(fù)的次數(shù)
          private JspFragment fragment;//重復(fù)的內(nèi)容

          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的內(nèi)容顯示出來
          ????? }
          }
          }

          第二步:編寫標(biāo)簽性質(zhì)文件
          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>,一定要這樣設(shè)定fragment屬性。

          第三步:編寫Jsp網(wǎng)頁
          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 標(biāo)簽</h2>

          <mytag:Repeat count="5" >
          <jsp:attribute name="fragment">
          重復(fù)執(zhí)行 ....<br>
          </jsp:attribute>
          </mytag:Repeat>
          </body>
          </html>

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

          DynamicAttributes接口
          只要制作的標(biāo)簽實(shí)現(xiàn)了DynamicAttributes接口就有動(dòng)態(tài)屬性的功能。
          例如:我們要做多個(gè)數(shù)的累加運(yùn)算,則AddSimpleTag標(biāo)簽就可以通過實(shí)現(xiàn)DynamicAttributes接口就可以實(shí)現(xiàn)了.

          實(shí)現(xiàn)DynamicAttributes接口,必須實(shí)現(xiàn)setDynamicAttributes()方法,此方法用來接收動(dòng)態(tài)屬性.

          舉例:第一步:制作標(biāo)簽處理類

          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 {

          //用來接收動(dòng)態(tài)屬性
          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);
          }
          }

          第二步:編寫標(biāo)簽性質(zhì)文件
          <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網(wǎng)頁
          <%@ 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中為了簡(jiǎn)化標(biāo)簽的復(fù)雜性,增加了制作Simple Tag的標(biāo)簽類SimpleTagSupport類。
          SimpleTagSupport類是實(shí)現(xiàn)SimpleTag接口的。它只需要實(shí)現(xiàn)一個(gè)doTag()方法即可,而不需要一堆回傳值。


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 黄山市| 抚松县| 东兴市| 哈密市| 元阳县| 任丘市| 谷城县| 子洲县| 襄汾县| 鹤岗市| 商河县| 高唐县| 阿瓦提县| 河北区| 满洲里市| 遵化市| 汾阳市| 新宾| 云阳县| 舞阳县| 卢湾区| 安溪县| 香河县| 凯里市| 县级市| 舞阳县| 新乡县| 寿阳县| 凤翔县| 汤阴县| 古交市| 南乐县| 韩城市| 康平县| 兖州市| 呈贡县| 井研县| 武定县| 石阡县| 安西县| 遵化市|