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

JSP
JSP中有关时间和日期类的使用
JSP中引用JavaBean组件
如何在JSP中添加自己的Tag
使用JSP/Servlet上载文件
关于Java Servlet的Filter 技术
JSP数据库连接方式总结
不需ODBC可由IP地址与端口号建立与SQLSERVER的连接
J2ME学习札记(三)
java 设计模式之Observer
在JSP中如何从数据流中取得图片数据并按随意位置显示
三级级联下拉菜单实现
网页中在图层中部分显示图片(窗口形式)
MySQL5.0中文问题及JDBC数据库连接和JSP汉字编码问题解决方法总结
XSLT合并模板简述
JSP入门学习笔记
Structs中基本配置入门
利用jConfig获取xml文件中的配置信息
关于Applet做数字签名,授予访问本地资源(原创)
在JSP开发中使用jdom解析临时存放数据的XML文件
关于Hhtml嵌入打成jar包的Applet方法

JSP中的TagLib应用(4-1)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-04   浏览: 30 ::
收藏到网摘: 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中定义了一些接口.