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

JSP
解决JSP中使用request乱码问题
高性能、高弹性JSP和Servlet性能优化
解决JSP开发Web程序中文显示三种方法
实例讲解在JSP开发中的对象和范围属性
JSP+XML实现网页内容动态显示的方案
weblogic的jsp问题解决方法
JSP处理异常及一些高级主题
JavaBean 在jsp和serlvle中传递技术要点
JSP和Servlet对中文的处理过程
用 servlet 将jsp文件内容转为html
JSP动态网站环境搭建的详细步骤
JSP/Servlet/JSF:自定义标签
Jsp常用功能:CSV文件的生成与分析
Jsp页面实现文件上传下载
JSP网站开发环境的目录结构标准
Windows 2000 server下搭建JSP网站环境
JSP教程:tomcat配置数据库连接池
tomcat下post方式提交服务器乱码
jsp环境下FCKeditor编辑器的应用
JSP网站开发环境配置和使用fckeditor

JSP中的TagLib应用(4-1)


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