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

JSP
Taglib原理和实现 第六章:标签内常用方法总结
用web_xml控制Web应用的行为(下)转贴
用Servlet实现下载
再论Session事件的捕获
Javascript传递中文出现乱码问题
JSP作为客户方访问CORBA服务对象
JSF(JavaServer Faces) 介绍
exception 隐含对象
ServletConfig和ServletConfig参数访问.
正确优雅的解决用户退出问题??JSP和Struts解决方案
JSP的9种基本内置组件
JSP处女作:commons-fileupload-1.0.jar + Oracle数据库文件上传
如何给jpg图片添加水印
利用Ant和XDoclet自动产生映射文件例子
Servlet设计
用servlet显示图片
Web应用中避免Form重复提交的三种方案
Web开发学习笔记
Servlet、Jsp性能优化
用Javascript实现Agent(网页精灵)(1)

JSP中的TagLib应用(4-1)


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