Cool eye

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

          ?? jsp規范里,標簽具有比javabean更豐富的運行時協議。它可以非常機密的和jsp的表示邏輯聯系在一起,同時,又具有javabean相同業務處理能力。所以,標簽的學習成為迫切的需要,但為了滿足實際項目的開發,自定義標簽的學習是不容錯過的。

          ?? 過實現接口或者繼承現有的類,我們就可以開發自定義的標簽。

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

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

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

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

          ?? 定義標簽的開發包括兩個部分的開發:

          (1)、開發標簽的處理程序(java類)

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

          ?? 定義標簽的種類有許多,可以根據實際項目的需要進行編寫。但為了不重復的開發輪子,jsp標準推出JSTL(標準標簽庫)。

          ?? 始我們的旅行吧,只要是快樂的,誰都想干,做的好與不好就另當別論了。

          (1)從Hello World開始吧,自定義一個類它實現Tag接口,Tag接口主要定義的是標簽聲明周期的方法,比如:doStartTag(),doEndTag()等,通過PageContext對象來訪問Jsp頁面的上下文:

          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接口中靜態常量:

          Tag.SKIP_BODY

          Tag.EVAL_PAGE

          Tag.EVAL_BODY_INCLUDE

          Tag.SKIP_PAGE

          (2)編寫標簽描述文件(.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中對標簽的引用:

          <?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)、編寫測試頁面:

          <%@ 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>
          下面看看標簽中的屬性是如何使用的,我們直接繼承TagSupport類來編寫:

          ?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("標簽的屬性值:"+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>以上為標簽的內容<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帶來很大的方便,只要重寫其中的部分方法即可實現一個標簽處理程序,以上開發的標簽程序和JSTL1.1標簽庫一起使用時出現一些疑問,原來標簽的uri=" http://java.sun.com/jsp/jstl/core ",但是,在如下的測試中會發現c:out中的值輸不出來,所以,只能改為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
          以上為標簽的內容
          標簽的屬性值:red


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

          接著看看迭代標簽的開發,首先,我們關注的是接口IterationTag,還有它的實現類TagSupport,接口BodyTag對IterationTag的擴展,還有BodyTag的實現類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>對顏色的設置</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

          主站蜘蛛池模板: 顺平县| 上虞市| 石城县| 察隅县| 洛宁县| 武乡县| 冷水江市| 棋牌| 阳东县| 普定县| 略阳县| 姜堰市| 静安区| 砀山县| 丰镇市| 将乐县| 兴化市| 勃利县| 时尚| 金塔县| 博白县| 确山县| 高平市| 罗定市| 台前县| 嘉义县| 泸水县| 滁州市| 岐山县| 临武县| 云林县| 偃师市| 泾源县| 张家口市| 蒙山县| 元朗区| 武乡县| 新沂市| 绩溪县| 怀远县| 通城县|