Cool eye

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            63 Posts :: 4 Stories :: 3 Comments :: 0 Trackbacks
          tag
          >“山高人為峰”,這句話也可以用在技術(shù)的研究上。

          ?? jsp規(guī)范里,標(biāo)簽具有比javabean更豐富的運(yùn)行時(shí)協(xié)議。它可以非常機(jī)密的和jsp的表示邏輯聯(lián)系在一起,同時(shí),又具有javabean相同業(yè)務(wù)處理能力。所以,標(biāo)簽的學(xué)習(xí)成為迫切的需要,但為了滿足實(shí)際項(xiàng)目的開(kāi)發(fā),自定義標(biāo)簽的學(xué)習(xí)是不容錯(cuò)過(guò)的。

          ?? 過(guò)實(shí)現(xiàn)接口或者繼承現(xiàn)有的類,我們就可以開(kāi)發(fā)自定義的標(biāo)簽。

          ?? 用的接口有:?????????????JspTag

          ???????????????????????Tag?????? SimpleTag<----SimpleTagSupport

          ???????????????IterationTag?<----TagSupport???????????

          ????????????????????BodyTag<----BodyTagSupport

          ?? 定義標(biāo)簽的開(kāi)發(fā)包括兩個(gè)部分的開(kāi)發(fā):

          (1)、開(kāi)發(fā)標(biāo)簽的處理程序(java類)

          (2)、標(biāo)簽描述文件(.tld文件)

          ?? 定義標(biāo)簽的種類有許多,可以根據(jù)實(shí)際項(xiàng)目的需要進(jìn)行編寫(xiě)。但為了不重復(fù)的開(kāi)發(fā)輪子,jsp標(biāo)準(zhǔn)推出JSTL(標(biāo)準(zhǔn)標(biāo)簽庫(kù))。

          ?? 開(kāi)始我們的旅行吧,只要是快樂(lè)的,誰(shuí)都想干,做的好與不好就另當(dāng)別論了。

          (1)從Hello World開(kāi)始吧,自定義一個(gè)類它實(shí)現(xiàn)Tag接口,Tag接口主要定義的是標(biāo)簽聲明周期的方法,比如:doStartTag(),doEndTag()等,通過(guò)PageContext對(duì)象來(lái)訪問(wèn)Jsp頁(yè)面的上下文:

          package com.xmddl.tag.demo;

          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.PageContext;
          import javax.servlet.jsp.tagext.Tag;

          public class HelloTag implements Tag {
          ?private PageContext context;
          ?private Tag parent;
          ?public void setPageContext(PageContext ctx) {
          ??this.context=ctx;

          ?}

          ?public void setParent(Tag tag) {
          ??this.parent=tag;

          ?}

          ?public Tag getParent() {
          ??return this.parent;
          ?}

          ?public int doStartTag() throws JspException {
          ??System.out.println("doStartTag");
          ??return Tag.SKIP_BODY;
          ?}

          ?public int doEndTag() throws JspException {
          ??System.out.println("doStartTag");
          ??try {
          ???this.context.getOut().write("Hello World");
          ??} catch (Exception e) {
          ???e.printStackTrace();
          ??}
          ??return Tag.EVAL_PAGE;
          ?}

          ?public void release() {
          ??// TODO Auto-generated method stub
          ?}

          }

          ?Tag接口中靜態(tài)常量:

          Tag.SKIP_BODY

          Tag.EVAL_PAGE

          Tag.EVAL_BODY_INCLUDE

          Tag.SKIP_PAGE

          (2)編寫(xiě)標(biāo)簽描述文件(.tld):

          <?xml version="1.0" encoding="UTF-8" ?>
          <taglib xmlns="
          http://java.sun.com/xml/ns/j2ee "
          ??? xmlns:xsi="
          http://www.w3.org/2001/XMLSchema-instance "
          ??? xsi:schemaLocation="
          http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "
          ??? version="2.0">

          ? <description>xmddl 1.1 hello library</description>
          ? <display-name>xmddl hello</display-name>
          ? <tlib-version>1.1</tlib-version>
          ? <short-name>hello</short-name>
          ? <uri>http://www.xmddl.com/tag</uri>
          ?
          ?
          <tag>
          ??? <description>我的Hello World</description>
          ??? <name>hello</name>
          ??? <tag-class>com.xmddl.tag.demo.HelloTag</tag-class>
          ??? <body-content>empty</body-content>
          ? </tag>

          </taglib>

          (3)、在web.xml中對(duì)標(biāo)簽的引用:

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "
          http://java.sun.com/dtd/web-app_2_3.dtd ">
          <web-app>
          ?<taglib>
          ??
          <taglib-uri>http://www.xmddl.com/tag</taglib-uri>
          ??<taglib-location>/WEB-INF/mytag.tld</taglib-location>
          ?</taglib>
          </web-app>

          (4)、編寫(xiě)測(cè)試頁(yè)面:

          <%@ page language="java" pageEncoding="UTF-8" import="java.util.*"%>
          <%@ taglib uri="http://www.xmddl.com/tag" prefix="lu"%>
          <html>
          ? <head>
          ??? <title>MyJsp.jsp</title>
          ??? <meta http-equiv="pragma" content="no-cache">
          ??? <meta http-equiv="cache-control" content="no-cache">
          ??? <meta http-equiv="expires" content="0">???
          ??? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
          ??? <meta http-equiv="description" content="This is my page">
          ? </head>
          ?
          ? <body>
          ??? <h1>This is a test tag page for person.</h1> <br>
          ??
          <lu:hello/>
          ? </body>
          </html>
          下面看看標(biāo)簽中的屬性是如何使用的,我們直接繼承TagSupport類來(lái)編寫(xiě):

          ?package com.xmddl.tag.demo;

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

          public class HelloSupport extends TagSupport {
          ?private String value;
          ?private static final long serialVersionUID = 1L;
          ?public HelloSupport(){
          ??super();
          ?}
          ?public int doStartTag() throws JspException {
          ??System.out.println("doStartTag");
          ??return TagSupport.EVAL_BODY_INCLUDE;
          ?}

          ?public int doEndTag() throws JspException {
          ??System.out.println("doEndTag");
          ??try {
          ???this.pageContext.getOut().write("標(biāo)簽的屬性值:"+this.value);
          ??} catch (Exception e) {
          ???e.printStackTrace();
          ??}
          ??return Tag.EVAL_PAGE;
          ?}
          ?public int doAfterBody() throws JspException{
          ??System.out.println("doAfterBody");
          ??try {
          ???this.pageContext.getOut().write("<br>以上為標(biāo)簽的內(nèi)容<br>");
          ??} catch (Exception e) {
          ???e.printStackTrace();
          ??}
          ??return TagSupport.EVAL_BODY_INCLUDE;
          ??
          ?}
          ?public String getValue() {
          ??return value;
          ?}
          ?public void setValue(String value) {
          ??this.value = value;
          ?}
          }
          ======================

          ...

          <tag>
          ??? <name>hellosupport</name>
          ??? <tag-class>com.xmddl.tag.demo.HelloSupport</tag-class>
          ??? <body-content>JSP</body-content>
          ??? <attribute>
          ??????? <name>value</name>
          ??????? <required>true</required>
          ??????? <rtexprvalue>false</rtexprvalue>
          ??? </attribute>
          ? </tag>

          ...

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

          <%@ page language="java" pageEncoding="gb2312" import="java.util.*"%>
          <%@ taglib prefix="lu" uri="
          http://www.xmddl.com/tag " %>
          <html>
          ? <head>
          ? </head>
          ? <body>

          ??? <h1>This is a test tag page for person.</h1> <br>
          ??<lu:hellosupport value="red">xmddl</lu:hellosupport>
          ? </body>
          </html>

          ?利用TagSupport帶來(lái)很大的方便,只要重寫(xiě)其中的部分方法即可實(shí)現(xiàn)一個(gè)標(biāo)簽處理程序,以上開(kāi)發(fā)的標(biāo)簽程序和JSTL1.1標(biāo)簽庫(kù)一起使用時(shí)出現(xiàn)一些疑問(wèn),原來(lái)標(biāo)簽的uri=" http://java.sun.com/jsp/jstl/core ",但是,在如下的測(cè)試中會(huì)發(fā)現(xiàn)c:out中的值輸不出來(lái),所以,只能改為uri=" http://java.sun.com/jstl/core ",

          <%@ page language="java" pageEncoding="gb2312" import="java.util.*"%>
          <%@ taglib prefix="lu" uri="
          http://www.xmddl.com/tag " %>
          <%@ taglib prefix="c" uri="
          http://java.sun.com/jstl/core " %>

          <html>
          ? <head>
          ? </head>
          ? <body>

          ??? <h1>This is a test tag page for person.</h1> <br>
          ??? <%request.setAttribute("name","xmddl"); %>
          ??? <%session.setAttribute("names","xmddls"); %>
          ???
          ??<lu:hellosupport value="red">xmddl</lu:hellosupport><hr>
          ??<c:forEach begin="1" end="5" step="1">
          ???name=<c:out value="${name}"></c:out>
          ???names=<c:out value="${names}"></c:out><br>
          ??</c:forEach>
          ? </body>
          </html>
          輸出如下:

          This is a test tag page for person.
          xmddl
          以上為標(biāo)簽的內(nèi)容
          標(biāo)簽的屬性值:red


          name=xmddl names=xmddls
          name=xmddl names=xmddls
          name=xmddl names=xmddls
          name=xmddl names=xmddls
          name=xmddl names=xmddls

          接著看看迭代標(biāo)簽的開(kāi)發(fā),首先,我們關(guān)注的是接口IterationTag,還有它的實(shí)現(xiàn)類TagSupport,接口BodyTag對(duì)IterationTag的擴(kuò)展,還有BodyTag的實(shí)現(xiàn)類BodyTagSupport。

          package com.xmddl.tag.demo;

          import java.io.IOException;

          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.tagext.BodyTagSupport;
          public class MyIteratorSupport extends BodyTagSupport {
          ?private static final long serialVersionUID = 1L;
          ?public String count;
          ?public String strColor;
          ?private int cou;
          ?
          ?public void setCount(String count) {
          ??this.count = count;
          ??cou=Integer.parseInt(count);
          ?}
          ?
          ?public int doAfterBody() throws JspException {
          ??try {
          ???this.pageContext.getOut().write("<br>");
          ??} catch (IOException e) {
          ???// TODO Auto-generated catch block
          ???e.printStackTrace();
          ??}
          ??if(cou>1){
          ???cou--;
          ???return MyIteratorTag.EVAL_BODY_AGAIN;
          ??}else{
          ???return MyIteratorTag.SKIP_BODY;
          ??}
          ?}
          ?public int doStartTag() throws JspException {
          ??try {
          ???this.pageContext.getOut().write("<font color='"+this.strColor+"'>");
          ??} catch (IOException e) {
          ???// TODO Auto-generated catch block
          ???e.printStackTrace();
          ??}
          ??return MyIteratorTag.EVAL_BODY_INCLUDE;
          ?}

          ?public int doEndTag() throws JspException {
          ??try {
          ???this.pageContext.getOut().write("</font>");
          ??} catch (IOException e) {
          ???// TODO Auto-generated catch block
          ???e.printStackTrace();
          ??}
          ??return MyIteratorTag.EVAL_PAGE;
          ?}

          ?public int getCou() {
          ??return cou;
          ?}

          ?public void setCou(int cou) {
          ??this.cou = cou;
          ?}

          ?public String getCount() {
          ??return count;
          ?}

          ?public String getStrColor() {
          ??return strColor;
          ?}

          ?public void setStrColor(String strColor) {
          ??this.strColor = strColor;
          ?}

          }
          ==============

          。。。

          <tag>
          ?? ?<description>對(duì)顏色的設(shè)置</description>
          ??? <name>myColor</name>
          ??? <tag-class>com.xmddl.tag.demo.MyIteratorSupport</tag-class>
          ??? <body-content>JSP</body-content>
          ??? <attribute>
          ??????? <name>count</name>
          ??????? <required>true</required>
          ??? </attribute>
          ??? <attribute>
          ??????? <name>strColor</name>
          ??????? <required>false</required>
          ??? </attribute>
          ?? </tag>

          。。。

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

          <%@ page language="java" pageEncoding="gb2312" import="java.util.*"%>
          <%@ taglib prefix="lu" uri="<html>
          ? <head>
          ? </head>
          ? <body>

          ??? <h1>This is a test tag page for person.</h1> <br>
          ??? <lu:myColor count="3" strColor="blue">xmddl</lu:myColor>
          ? </body>
          </html>

          輸出如下

          This is a test tag page for person.


          xmddl
          xmddl
          xmddl

          主站蜘蛛池模板: 金湖县| 贵阳市| 松滋市| 北碚区| 赫章县| 沛县| 延长县| 厦门市| 永年县| 桃江县| 进贤县| 井研县| 石楼县| 鲁山县| 津市市| 来宾市| 霍城县| 融水| 云林县| 西城区| 鄂温| 旬阳县| 房产| 彭山县| 紫金县| 柘城县| 濮阳市| 达拉特旗| 丰原市| 元朗区| 潼南县| 汝城县| 新闻| 沂水县| 阿拉尔市| 沁阳市| 宾川县| 枣强县| 电白县| 达州市| 喀喇|