JSP中的TagLib应用

1、前言:
写程序很重要的几点是要让我们的CODE可以复用, 可以扩展和具有灵活性.
jsp
基于面向对象的JAVA技术, 我们可以通过运用DESIGN PATTERNS使之具备这些特性.
jspxml的紧密结合使得我们在编码时又多了一种选择,写出精良的code已不是遥远的童话.
这里将讲述编程时使用我们自定义的或应用其他已定义好的 tag.以及对TagLib进行部署.
要应用TAG,
-------------
建立tld文件.
TLD(TLD:Tag Library Descriptor
标签库描述符)文件,标准的XML格式的标记定义文件.
定义tag和他的各种属性和处理文件等等.
-------------Tag Handler( TAG
处理器.)
实际上就是个JAVA类文件,用来处理tag. 需要在tld文件里的每个tag标记
中指明是应用哪一个类文件来对这个TAG进行处理.
-------------
JSP使用tag
可以通过jsp指令来使用
在建立TAGLIB,编写TAG处理文件的时候, 可能会显得有点复杂. 但当我们需要重复使用或添加功能的
时候, 就会发现我们做了多么伟大的事情! ^_- (呵呵 , 有点夸张)
2
、开始
现在让我们逐步深入的了解xmljsp中的应用吧.
首先需要定义tld文件和相应tag处理的java类文件.然后在jsp通过定义的语法使用tag,
让我们来看看下面这个XML文件。
==================taglib.tld===========================
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
1.0
1.1
Java Pet Store Demo custom tags
insert
com.sun.estore.taglib.InsertTag
JSP
An insertion tag
id
false
true
template
true
true
parameter
true
true
CreateTemplate
com.sun.estore.taglib.CreateTemplateTag
JSP
Create a template object and store in the request with the specified name
id
false
true
template
true
true
screen
true
true
Screen
com.sun.estore.taglib.TemplateScreenTag
JSP
A template screen object
screen
true
true
Parameter
com.sun.estore.taglib.TemplateParameterTag
JSP
A template parameter object
parameter
true
true
value
true
true
direct
true
true
===========================================================
这是J2EE Blueprints 里提供的sample------Pet Store里面的一个TLD文件。
下面对TLD文件进行说明
TagLib Descriptor

自定义tag位置

WEB-INF/tlds/xxxx.tld
注意:需要在tld文件里定义:
tlibversion--------Tag library
的版本
jspversion--------
这个Tag library要求的JSP版本。
shortname-------
缺省的名字。(这个例子里没有定义)
uri-------------------
这个Tag libraryURL
info-----------------Tag library
的使用信息
tag-----------------
自定义的tag
name---------------
自定义的tag的名字
tagclass-----------
处理这个tagjava类的名字.不同的tag可能对应不同的java类来处理。
Teiclass----------
bodycontent-----
标出属性值的类型,如果没有标识,隐含为JSP
JSP -------------------interpreted by page
EMPTY ------ -----------no body allowed
TAGDEPENDENT-----interpreted by tag
需要BodyTag
BodyTag can post-process JSP
info------------------
这个tag的使用信息
attribute-----------
属性。 每个tag可以有n个属性
在这个例子里,定义了四个tag

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

原文地址:https://www.cnblogs.com/kentyshang/p/800286.html