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

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

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情況自己完成吧. 


主站蜘蛛池模板: 天天干天天射天天舔 | 亚洲国产天堂久久综合图区 | 深爱五月婷婷 | 四虎影永久在线高清免费 | 特级黄一级播放 | 日本高清网| 中文字幕第38页永久乱码 | 色香欲亚洲天天综合网 | 欧美亚洲图区 | 天天影视色香欲 | 香港三日本三级人妇三级99 | 亚洲天天综合网 | 日韩免费看片 | 色综合久久六月婷婷中文字幕 | 日韩在线三级视频 | 欧美做a一级视频免费观看 欧美坐爱视频 | 日本后进式猛烈xx00动态图 | 四虎永久在线精品视频播放 | 亚洲天堂男人在线 | 天天干天天干 | 亚洲国产日韩在线观频 | 日韩中文字幕在线有码视频网 | 日本高清视频一区二区三区 | 日本欧美高清全视频 | 日本高清网站 | 欧美性三级 | 无码精品日韩中文字幕 | 星辰影院在线 | 亚洲视频在线免费 | 热热热热色 | 午夜爽视频| 日韩a在线观看免费观看 | 日韩精品小视频 | 日韩美女在线视频网站免费观看 | 视频在线观看91 | 欧美又硬又粗又长又大 | 伊人二区| 亚洲性在线 | 亚洲在成人网在线看 | 亚洲黄色一区二区 | 欧洲97色综合成人网 |