第6章 JSP 与XML 联合开发技术
<!ELEMENT tlibversion (#PCDATA) >
<!--
Describes the JSP version (number) this taglibrary requires in
order to function (dewey decimal)
The default is 1.1
#PCDATA ::= [0-9]*{ "."[0-9] }0..3
-->
<!ELEMENT jspversion (#PCDATA) >
<!-- Defines a short (default) shortname to be used for tags and variable names used/created by this tag library. Do not use white space and do not start with digits or underscore. #PCDATA ::= NMTOKEN -->
<!ELEMENT shortname (#PCDATA) > <!--
Defines a public URI that uniquely identifies this version of the taglibrary Leave it empty if it does not apply. -->
<!ELEMENT uri (#PCDATA) > <!--
Defines an arbitrary text string descirbing the tag library -->
<!ELEMENT info (#PCDATA) > <!-- The tag defines a unique tag in this tag library defining: - the unique tag/element name - the subclass of javax.servlet.jsp.tagext.Tag implementation class - an optional subclass of javax.servlet.jsp.tagext.TagExtraInfo - the body content type (hint) - optional tag-specific information - any attributes --> <!ELEMENT tag (name tagclass teiclass?bodycontent?info?attribute*) > <!--
Defines the subclass of javax.serlvet.jsp.tagext.Tag that implements the request time semantics for this tag. (required) #PCDATA ::= fully qualified Java class name -->
<!ELEMENT tagclass (#PCDATA) > <!--
Defines the subclass of javax.servlet.jsp.tagext.TagExtraInfo for this tag. (optional)
第二部分 JSP 技术和XML 技术
If this is not given
the class is not consulted at translation time. #PCDATA ::= fully qualified Java class name --> <!ELEMENT teiclass (#PCDATA) >
<!--
Provides a hint as to the content of the body of this tag. Primarilyjsp定义
intended for use by page composition tools.
There are currently three values specified:
tagdependent The body of the tag is interpreted by the tag
implementation itself and is most likely in a
different "language" e.g embedded SQL statements. JSP
The body of the tag contains nested JSP syntax
empty The body must be empty The default (if not defined) is JSP #PCDATA ::= tagdependent | JSP | empty -->
<!ELEMENT bodycontent (#PCDATA) > <!--
The attribute tag defines an attribute for the nesting tag An attribute definition is composed of: - the attributes name (required) - if the attribute is required or optional (optional) - if the attributes value may be dynamically calculated at runtime by a scriptlet expression (optional) --> <!ELEMENT attribute (name required? rtexprvalue?) > <!--
Defines the canonical name of a tag or attribute being defined #PCDATA ::= NMTOKEN -->
<!ELEMENT name (#PCDATA) > <!-- Defines if the nesting attribute is required or optional. #PCDATA ::= true | false | yes | no If not present then the default is "false"i.e the attribute is optional. -->
<!ELEMENT required (#PCDATA) > <!--
Defines if the nesting attribute can have scriptlet expressions as a value i.e the value of the attribute may be dynamically calculated at request time as opposed to a static value determined at translation time.
第6章 JSP 与XML 联合开发技术 #PCDATA ::= true | false | yes | no
If not present then the default is "false"i.e the attribute
has a static value
-->
<!ELEMENT rtexprvalue (#PCDATA) >
<!ATTLIST tlibversion id ID #IMPLIED>
<!ATTLIST jspversion id ID #IMPLIED>
<!ATTLIST shortname id ID #IMPLIED>
<!ATTLIST uri id ID #IMPLIED>
<!ATTLIST info id ID #IMPLIED>
<!ATTLIST tag id ID #IMPLIED>
<!ATTLIST tagclass id ID #IMPLIED>
<!ATTLIST teiclass id ID #IMPLIED>
<!ATTLIST bodycontent id ID #IMPLIED>
<!ATTLIST attribute id ID #IMPLIED>
<!ATTLIST name id ID #IMPLIED>
<!ATTLIST required id ID #IMPLIED>
<!ATTLIST rtexprvalue id ID #IMPLIED> 在程序清单 6.4所列出来的DTD 文件中定义了所有在TLD 文件中可用的XML 标记在这个DTD 文档中有很多英文注释这些注释解释了每个标记的用途看不懂!不要紧下面我们就来结合一个具体的TLD
文件实例逐个解释这些标记的意义与用途 考虑到读者都是Tag Library 技术的初学者要从头编写一个TLD 文件有很大的难度我们就以一个现成的例子为基础本章所选用的JSP 服务器为Tomcat 3.2Tomcat 3.2服
务器自带了一个使用Tag Library 的例子我们就以这个例子为基础再进行扩展这样不但见效快降低了难度读者也更容易理解些这个例子的JSP 文件如程序清单6.3所示为了正确运行这个JSP 程序(foo.jsp)我们需要把TOMCA THOME\webapps\examples\WEB-INF 文件夹全部替代TOMCATHOME\webapps\ROOT\WEB-INF 文件夹并且把程序清单 6.3foo.jsp 保存ROOT 文件夹的根目录下面这样就可以正确运行这个JSP 程序了运行效果如图 6.4所示
图6.4 foo.jsp
第二部分 JSP 技术和XML 技术
我们在上文已经提到过程序清单6.3所使用的Tag Library
为simple 先看看这个Tag Library 的TLD 文件使用记事本打开TOMCATHOME\webapps\ROOT\WEB-INF\jsp\example -taglib.tld 文件如程序清单6.5
程序清单6.5(example-taglib.tld)
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems Inc.//DTD JSP Tag Library 1.1//EN" "java.sun/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<!-- a tag library descriptor -->
<taglib>
<!-- after this the default space is
"java.sun/j2ee/dtds/jsptaglibrary_1_2.dtd"
-->
<tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>simple</shortname> <uri></uri> <info>
A simple tab library for the examples </info>
<tag>
<name>ShowSource</name> <tagclass>examples.ShowSource</tagclass> <info> Display JSP
sources </info> <attribute> <name>jspFile</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
<!-- A simple Tag --> <!-- foo tag --> <tag>
<name>foo</name> <tagclass>examples.FooTag</tagclass> <teiclass>examples.FooTagExtraInfo</teiclass> <bodycontent>JSP</bodycontent> <info>
Perform a server side action; uses 3 mandatory attributes </info>
第6章 JSP 与XML 联合开发技术
<attribute>
<name>att1</name>
<required>true</required>
</attribute>
<attribute>
<name>att2</name>
<required>true</required>
</attribute>
<attribute>
<name>att3</name>
<required>true</required>
</attribute>
</tag>
<!-- Another simple tag --> <!-- log tag --> <tag>
<name>log</name> <tagclass>examples.LogTag</tagclass> <bodycontent>TAGDEPENDENT</bodycontent> <info>
Perform a server side action; Log the message. </info> <attribute> <name>toBrowser</name> <required>false</required> </attribute> </tag> </taglib> 下面我们就来详细分析程序清单 6.5粗粗看来程序清单6.5可以分为三大部分第一部分就是前面四行这是所谓的XML 指令了这一部分必不可少但是和本章的关系不密切在这里我们不用讨论它第二部分描述了Tag Library 的详细信息这部分的代码如下 <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>simple</shortname> <uri></uri> <info> A simple tab library for the examples
</info> 读者应该对上面的代码所使用的标记有印象吧不错它们都在程序清单6.4中定义
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论