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中定义了一些接口.

本文转自kenty博客园博客,原文链接http://www.cnblogs.com/kentyshang/archive/2007/06/29/800286.html如需转载请自行联系原作者


kenty

相关文章
|
3月前
|
SQL 前端开发 Java
JSP技术详解及其在Web开发中的应用
【1月更文挑战第2天】本文将对JSP(Java Server Pages)技术进行详细的介绍和分析。JSP是一种基于Java的服务器端编程技术,它允许开发者在HTML或XML等文档中直接嵌入Java代码片段,从而动态地生成Web页面内容。本文将首先阐述JSP的基本原理和工作机制,然后讨论其在Web开发中的各种应用场景,包括表单处理、数据库访问、会话管理等,并通过实例代码展示JSP的实际应用。最后,本文将对JSP的优缺点进行评述,并对未来的发展趋势进行展望。
114 10
|
4月前
|
Java 数据库
Jsp应用
Jsp应用
26 0
|
4月前
|
Java 数据安全/隐私保护
Shiro - JSP页面标签应用
Shiro - JSP页面标签应用
28 0
|
5月前
|
Java 物联网 Shell
Jsp Webshell在物联网的应用
Jsp Webshell在物联网的应用
|
10月前
|
Java 应用服务中间件 数据库连接
JSP的基础使用及应用案例
JSP的基础使用及应用案例
53 0
|
Java 应用服务中间件 程序员
无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]
无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]
1136 0
无法在web.xml或使用此应用程序部署的jar文件中解析绝对uri:[http://java.sun.com/jsp/jstl/core]
|
存储 Java
2022年jsp课时十三——Cookie和Session的应用
2022年jsp课时十三——Cookie和Session的应用
77 0
2022年jsp课时十三——Cookie和Session的应用
|
Java
JSP Session应用
JSP Session应用
68 0
|
Java 应用服务中间件
使用最新版IDEA(21.3.2)创建一个最小的JAVAWEB应用(JSP,Servlet运行方法)
1.IDEA内创建一个基于Tomcat的项目 首先新建一个普通的项目
230 1
使用最新版IDEA(21.3.2)创建一个最小的JAVAWEB应用(JSP,Servlet运行方法)
|
XML JavaScript Java
Java Web之JSP操作XML(XML的文档结构 语法和注释、dom4j的下载与配置 应用dom4j创建、解析和修改XML)
Java Web之JSP操作XML(XML的文档结构 语法和注释、dom4j的下载与配置 应用dom4j创建、解析和修改XML)
185 0
Java Web之JSP操作XML(XML的文档结构 语法和注释、dom4j的下载与配置 应用dom4j创建、解析和修改XML)