晨风资讯网
新闻资讯网络冲浪网页设计网络编程图形图像数据库网络媒体服务器网络安全网站运营软件教程黑客认证Wap技术
教程搜索
教程搜索:
  首页 > 网络编程 > JSP教程 > 正文  

JSP中的TagLib应用(4-1)
日期:2006-1-19 21:54:07 来源:网络 作者:无名 浏览:

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

上一篇: JSP中的TagLib应用(3-4) 下一篇:

JSP中的TagLib应用(4-2)

返回列表 打印此页 加入收藏 资讯论坛 关闭窗口 点击复制本页地址,发送给QQ/MSN好友
关于我们 - 联系我们 - 版权声明 - 帮助(?) - 广告服务 - 友情链接 - 服务项目 - 人才招聘
2003-2008 版权所有 © 晨风资讯网 未经授权禁止复制或建立镜像
CopyRight 2003-2008 www.Net118.com,All Rights Reserved.Design By ChenFeng Network Studio