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

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

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


主站蜘蛛池模板: 色综合天天综合网站中国 | 亚洲va久久久噜噜噜久久 | 手机看片福利在线 | 午夜影院免费版 | 色播放| 日韩成人免费视频播放 | 性a视频 | 亚洲a成人7777777久久 | 日本视频中文字幕 | 欧美一区二区激情三区 | 四虎免费在线观看视频 | 日本黄色app | 又粗又硬又大又爽免费视频播放 | 伊人精品视频在线观看 | 日日碰碰视频播放 | 一级@片| 亚洲精品免费在线观看 | 亚洲欧美久久精品 | 日韩欧美精品综合一区二区三区 | 天天操天天看 | 亚洲国产一区二区三区最新 | 最新欧美一级视频 | 手机在线观看毛片 | 鸥美性生交xxxxx久久久 | 日韩精品永久免费播放平台 | 日韩孕交| 日韩精品免费看 | 四虎影视国产884a精品亚洲 | 日韩欧美网站 | 三级黄色片在线播放 | 色之综合网 | 欧美性色黄在线视频 | 日日摸夜夜夜夜夜添 | 日本性网址 | 最新国产福利在线观看 | 欧美一级欧美三级在线观看 | 五月综合激情视频在线观看 | 西瓜影院日韩大片在线观看 | 啪啪免费小视频 | 天天天天做夜夜夜做 | 日韩美女在线视频网站免费观看 |