当前位置: 首页 > 图文教程 > 网络编程 > JSP > JSP中的TagLib应用(4-1)

JSP
关于seeion的文章
jsp与javaBeans的结合使用
使用JavaBean,一句代码完成对文本文件读取和写入!!!
利用HttpSessionListener实现网站在线人数统计功能
第一章:taglib 原理和实现
用web_xml控制Web应用的行为(上)待徐
Taglib 原理和实现:第三章 tag之间的嵌套和属性读取
Taglib 原理和实现:第四章 循环的Tag
学习笔记:ServletContext接口
checkbox的使用
DelphiBBS的JSP实现(未经授权).
可以自动跳转到出错页面的servlet/jsp框架
学用Java Web Start 部署应用程序
Web页面数据批量录入----使用上传组件与JXL工具包联合实现
使用jsp实现word、excel格式报表打印
JSP页面查询显示常用模式
使用XML封装数据库操作语句的实现(完全版)--发布源码
深入浅出taglib
用Java实现Web服务器
如何利用xml,javascript绕开applet的安全问题

JSP中的TagLib应用(4-1)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-04   浏览: 44 ::
收藏到网摘: n/a

下面到了关键部分乐。 对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中定义了一些接口.