六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

JSP中的TagLib應用(4)--zt

[摘要]JSP中的TagLib應用(4)作者:False / aspcn ---------------------------------------------------------------------- 下面到了關鍵部分樂。 對tag進行處理。其實很多情況下我們是使用已經提供的taglib. ...
JSP中的TagLib應用(4)


作者:False / aspcn

----------------------------------------------------------------------

下面到了關鍵部分樂。 對tag進行處理。其實很多情況下我們是使用已經提供的taglib.

別人/公司已經做好了tag和處理部分,打好了包 我們需要做的只是在我們的jsp中去應用.

但是當我們自己做個taglib時, 就需要編寫這部分tag handler了.

這里只針對上面文件里提到的insert tag,其他的為了避免重復,就不一一說明了

==================== InsertTag.java==============================
/*

* $Id: InsertTag.java,v 1.13 2000/03/04 02:54:57 brydon Exp $

* Copyright 1999 Sun Microsystems, Inc. All rights reserved.

* Copyright 1999 Sun Microsystems, Inc. Tous droits réservés.

*/



package com.sun.estore.taglib;



import javax.servlet.jsp.JspTagException;

import javax.servlet.jsp.tagext.TagSupport;



import com.sun.estore.util.Debug;



/**

* This class is an easy interface to the JSP template or other

* text that needs to be inserted.

* @author Greg Murray

*/



public class InsertTag extends TagSupport {



private boolean directInclude = false;

private String parameter = null;

private String templateName = null;

private Template template = null;

private TemplateParameter templateParam = null;



/**

* default constructor

*/

public InsertTag() {

super();

}



public void setTemplate(String templateName){

this.templateName = templateName;

}



public void setParameter(String parameter){

this.parameter = parameter;

}



public int doStartTag() {

try{

if (templateName != null){

template = (Template)pageContext.getRequest().getAttribute("template");

}

} catch (NullPointerException e){

Debug.println("Error extracting template from session: " + e);

}



if (parameter != null && template != null) templateParam = (TemplateParameter)template.getParam(parameter);

if (templateParam != null) directInclude = templateParam.isDirect();

return SKIP_BODY;

}



public int doEndTag() throws JspTagException {

try{

pageContext.getOut().flush();

} catch (Exception e){

// do nothing

}

try {

if (directInclude && templateParam != null) {

pageContext.getOut().println(templateParam.getValue());

} else if (templateParam != null) {

if (templateParam.getValue() != null) pageContext.getRequest().getRequestDispatcher(templateParam.getValue()).include(pageContext.getRequest(), pageContext.getResponse());

}

} catch (Throwable ex) {

ex.printStackTrace();

}

return EVAL_PAGE;

}

}
可以看到。InsertTag.java繼承了javax.servlet.jsp.tagext.TagSupport類. 因為在TagSupport中定義了一些接口.
  我們在處理自定義的tag時, 對父類的doStartTag() 和doEndTag() 要進行重載,如果在tld文件中定義了tag的屬性, 就需要在tag handler里對每個屬性定義相應的setxxx/getxxx方法.

在doStartTag()中是從Template類所定義的Hashtable中取得TemplateParameter對象.

在doEndTag()中

pageContext.getRequest().getRequestDispatcher(templateParam.getValue()).include(pageContext.getRequest(), pageContext.getResponse());

這是在頁面里包含通過jsp頁的上下文返回通過tag的屬性值指定的資源對象(RequestDispatcher)所產生的內容..

doStartTag()和doEndTag()返回值是在Tag Interface里定義的靜態int

SKIP_BODY隱含0

Skip body evaluation. Valid return value for doStartTag and doAfterBody. 跳過對body的處理。

就是跳過了開始和結束標簽之間的代碼。



EVAL_BODY_INCLUDE 隱含1

Evaluate body into existing out stream. Valid return value for doStartTag.

This is an illegal return value for doStartTag when the class implements BodyTag,

since BodyTag implies the creation of a new BodyContent.

將body的內容輸出到存在的輸出流中。包括是jsp代碼,也可以被輸出

SKIP_PAGE 隱含5

Skip the rest of the page. Valid return value for doEndTag.

忽略剩下的頁面。

EVAL_PAGE 隱含6

Continue evaluating the page. Valid return value for doEndTag().

繼續執行下面的頁

在這個類里還有對其他類的引用,我就不列出來了. 各位可以自己去研究.

自定義標簽(Custom tags)實現了 javax.servlet.jsp.tagext.Tag or javax.servlet.jsp.tagext.BodyTag

interface. 應用javax.servlet.jsp.JspWriter 來輸出.


TagSupport class 提供了對interface Tag的隱含實現. 序列化編發數據.

public class TagSupport extends java.lang.Object implements Tag, java.io.Serializable


BodyTagSupport class提供了對interface BodyTag的隱含實現.繼承TagSupport

public class BodyTagSupport extendsTagSupport implements BodyTag



我們在編寫tag handler時需要繼承TagSupport類或BodyTagSupport類,然后重載doStartTag()和doEndTag().

還可以再進一步分離.將具體實現放到bean里

這些自定義標簽的類所應放的位置應該在WEB-INF/classes或WEB-INF/lib

最后將custom tag libraries 打包成.war文件.關于.war文件,如下圖所示給出了資源結構圖。




通過部署描述符來控制映射。

部署描述符是個XML文件。詳細的請詳見有關文檔資料。這里只說明

部署TagLib的部分。

JSP custom tag libraries (optional)

Specifies URL for locating Tag Library Descriptor





uri

path







/**************自定義TagLib例子: JSP page *******************

………








*******************************************************/

文件位置如下:




這個例子很簡單, 只是作為練習使用.你也可以自己進行擴展,多練習幾種處理方法.比方說標簽帶有屬性,

標簽帶有內容, 使用bean處理邏輯等等

關于將這些文件打包成.war文件.這里就不詳細說明了.請根據你使用的application server情況自己完成吧. 


主站蜘蛛池模板: 亚洲综合亚洲综合网成人 | 日本特黄特色aa大片免费 | 五月天婷婷在线视频 | 日本综合在线观看 | 欧洲另类一二三四区 | 欧美一区二区激情视频 | 五月丁香六月综合av | 色综合一区二区三区 | 在线视频综合网 | 日韩欧美亚洲综合一区二区 | 婷婷综合七月激情啪啪 | 青青青国产在线观看免费网站 | 欧美性猛交乱大交xxxx | 小明www永久在线看 小黄鸭b站视频在线 | 天堂网www在线资源链接 | 欧美呦 | 四虎成人4hutv影院 | 四虎影院黄色 | 深夜福利国产 | 欧洲真实呦女网站 | 亚洲 欧美 日韩在线综合福利 | 午夜在线播放免费高清观看 | 亚洲成a人v欧美综合天堂下载 | 一个色亚洲 | 天天躁日日躁狠狠躁欧美日韩 | 色综合网天天综合色中文男男 | 天天影视色香欲综合影视大全 | 亚洲国产中文字幕 | 揄拍成人国产精品视频 | 欧美一级看片a免费观看 | 亚洲第一综合网 | 亚洲成人在线网站 | 日本精品视频在线 | 中文天堂在线视频 | 午夜影院免费观看 | 亚州黄网 | 亚洲一区二区黄色 | 亚洲第一男人网站 | 亚洲风情网 | 亚洲欧美系列 | 午夜网站视频 |