本文实例讲述了jsp自定义标签用法。分享给大家供大家参考。具体如下:
在JSP中有一种机制,可以让你在JSP页面中插入与HTML类似的标记。本文介绍JSP定制标记的基本概念和构成,以及如何开发和应用JSP定制标记。
什么是标记
使用HTML语言我们可以这样去编辑我们的网页:
1 2 3 4 5 6 7 8 9 10 |
|
在这里我们把34934661d7147ca926b095899343bad0,08eb756f81b28a01d33212cce3fbe742,a64997a0904a094b4570728d7f327acd称为标记。HTML 标记( HTML Markup)是HTML文档的控制语言,用于指定浏览器显示和打印文档的方式.它是用小于号"73fba53e3e558034b52e782854a23525"括起来的短语和符号,如 d90fcdb535d6081491edebb07224a674、4f8ea7115e78f861c264d09f6f145eb7等。许多HTMl标记以成对的方式出现,如08eb756f81b28a01d33212cce3fbe742611f9639240b30f69873ac7e66a1829c、9c5594a5fc8d2e506f1a8147102c836b4f8ea7115e78f861c264d09f6f145eb7 等。在JSP中我们也可以定制自己的标记,以供JSP页面使用,如下例所示
1 2 3 4 5 6 7 8 9 10 11 |
|
在上例中1297c44f078c851ada4c33aadd4f023f就是一个JSP定制标记符。widtht、height是这个标记的属性。87ecc62b36bc7a2fa256d982f103a6a7是一个标记库定义指令,在稍后我们将会讨论。在JSP中定制标记符,实质上就是以标记的形式封装了一个俱有独立功能的Java类。标记的使用减少了直接嵌入JSP页面的Java代码,方便了页面的布局,并且有利于代码的复用,提高了开发的效率。
JSP服务器解析标记的过程
那么当一个标记被嵌入JSP页面后,JSP服务器是如何对这个标记进行解析的呢?各对象的含义如下所示:
Client: 表示客户端。
JSP-Server:JSP服务器。
JSP-Page:JSP页面。
TLD: 标记库描述文件,定义标记和标记的各种属性和处理文件等。
TagClass 标记处理程序
当一个用户访问一个JSP页面时,这个请求被发送到JSP服务器,JSP服务器会根据这个请求去调用相应的页面,如果这个页面中有自定义的标记, JSP服务就会根据页面指令4000e405c537c31a6e93758fead4c19d去访问TLD得到处理程序的相关信息,接着调用该处理程序的构造器方法,启动标记符处理程序,并读取标记符的属性和相应值。对每个没有设置属性的,调用相应的set方法。当标记符第一次使用时,它的任何属性都不会做过设置,因此对每个属性都调用set方法。属性设置完以后,JSP服务器调用处理程序的doStartTag(),然后再调用doEndTag()方法。最后JSP服务器会继续处理剩下的页面,在页面结尾调用release ()方法,清理占用的所有资源。
TLD文件
TLD(TLD:Tag Library Descriptor标记库描述符)文件,标准的XML格式的标记定义文件,被用来存放标记符的信息,下面就是一个典型的TLD文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
在这个TLD文件中定义了只有一个标记符的标记符库,这个名为login的标记符会调用一个Applet以验证用户的合法性。处理这个标记的类就是 tagclass.login.login。width、height是这个标记的两个属性。属性是在使用标记符时作为参数发送的值。我们可以在上面的示例中增加几个标记,也可以为每个标记添加几个属性。我们开发标记符库时不一定非要从头开始,自己编写一个全新TLD。我们可以使用某个集成的开发的环境,也可以修改上面的例子。
TagLib指令
那么当JSP服务器在解析一个标记符时,它是如何定义一个标记库的呢?这就是TagLib指令的主要责任。
Taglib 指令
定义一个标记库以及其自定义标记的前缀.
JSP 语法
复制代码代码如下:
c5ed71a2155e0c907c9cc84ff0eb47e6
例子
1 2 3 4 5 6 7 8 9 10 |
|
描述
3e0568b0b3822a2e41864553dc4f94b1指令声明此JSP文件使用了自定义的标记,同时引用标记库,
也指定了他们的标记的前缀。 你必须在使用自定义标记之前使用3e0568b0b3822a2e41864553dc4f94b1指令。
属性
uri="URIToTagLibrary" :Uniform Resource Identifier (URI)根据标记的前缀对自定义的标记进行唯一的命名,URI可以是一个相对或绝对的路径。
prefix="tagPrefix":在自定义标记之前的前缀。如上例中的1297c44f078c851ada4c33aadd4f023f
标记符的处理程序(Tag handle)
我们还是以一个例子来看下如何实现一个Tag handle。首先是看一下它的类图:
让我们再看一下它的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
从以上我们可以看出,实现一个简单的标记符处理程序有几个要求:①增加一个类,使之继承 java.Servlet.jsp.tagext.TagSupport类。这个类提供了java.Servlet.jsp.tagext.Tag接口所要求的所有的方法。另外,还需要使用一些基本的API,使JSP容器能够调用我们自己提供的标记符处理程序。②必须为每个标记符属性分别创建一个 gete8aa4f7ce323535a9b01247c46fe6139和sete8aa4f7ce323535a9b01247c46fe6139方法,JSP容器需要使用这些方法处理程序传递参数。③要为标记符处理程序创建一个构造器和自毁器。JSP需要使用构造器启动处理程序。自毁器是在realease()方法中定义的。在处理程序的生命周期结束时,需要调用自毁器释放所占用的资源。④创建两个名为doStartTag()和doEndTag()的方法,执行具体的处理和输出动作。这两个方法是在处理自定义标记符的起始位置和结束位置调用的。它们的返回值是在Tag Interface里定义的静态int,这几个静态值分别是:
SKIP_BODY隐含0 :跳过了开始和结束标签之间的代码。
EVAL_BODY_INCLUDE隐含1:将body的内容输出到存在的输出流中
SKIP_PAGE隐含5 : 忽略剩下的页面。
EVAL_PAGE隐含6:继续执行下面的页
当然标记符也有它自己的缺点。很不方便的封装过程,有限的功能。对于一些不太复杂和功能单一的逻辑描述,需要传递的参数要求不高时,使用JSP标记,要方便的多。对于大多数的商业逻辑应用,还是使用bean要好的多,也宜于servlet控制。
附:文章中所用示例的完整代码
JSP代码:login.jsp
1 2 3 4 5 6 7 8 9 10 |
|
标记符描述库:taglib.tld
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
标记符处理程序:login.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
标记符处理程序中所使用的Applet : login.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
例子:
es.tld文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd" > <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>es</short-name> <tag> <name>hasPermission</name> <tag-class>com.jfit.core.tag.HasPermissionTag</tag-class> <body-content>JSP</body-content> <description> </description> <attribute> <name>name</name> <required>true</required> <rtexprvalue>false</rtexprvalue> <description>资源编码,多个之间以“;”分割.</description> </attribute> </tag> <tag> <name>lacksPermission</name> <tag-class>com.jfit.core.tag.LacksPermissionTag</tag-class> <body-content>JSP</body-content> <description> </description> <attribute> <name>name</name> <required>true</required> <rtexprvalue>false</rtexprvalue> <description>资源编码,多个之间以“;”分割.</description> </attribute> </tag> <tag> <name>hasRole</name> <tag-class>com.jfit.core.tag.HasRoleTag</tag-class> <body-content>JSP</body-content> <description> </description> <attribute> <name>name</name> <required>true</required> <rtexprvalue>false</rtexprvalue> <description>角色编码,多个之间以“;”分割.</description> </attribute> </tag> <tag> <name>lacksRole</name> <tag-class>com.jfit.core.tag.LacksRoleTag</tag-class> <body-content>JSP</body-content> <description> </description> <attribute> <name>name</name> <required>true</required> <rtexprvalue>false</rtexprvalue> <description>角色编码,多个之间以“;”分割.</description> </attribute> </tag> <tag> <name>isAdmin</name> <tag-class>com.jfit.core.tag.IsAdminTag</tag-class> <body-content>JSP</body-content> <description> </description> </tag> <tag> <name>lacksAdmin</name> <tag-class>com.jfit.core.tag.LacksAdminTag</tag-class> <body-content>JSP</body-content> <description> </description> </tag> <tag> <name>operation</name> <tag-class>com.jfit.core.tag.OperatiuonTag</tag-class> <body-content>empty</body-content> <attribute> <name>name</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>method</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>permission</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>type</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>iconCls</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>plain</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>imgSrc</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>dictionary</name> <tag-class>com.jfit.core.tag.DictionaryTag</tag-class> <body-content>empty</body-content> <attribute> <name>id</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>name</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>width</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>height</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>code</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>type</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description>可选“combobox”、“combotree”.</description> </attribute> <attribute> <name>selectType</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description>可选“select”、“all”.</description> </attribute> <attribute> <name>required</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>missingMessage</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>value</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>validType</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>multiple</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>editable</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
web.xml
<!--标签tag--> <jsp-config> <taglib> <taglib-uri>/essh-tags</taglib-uri> <taglib-location>/WEB-INF/tlds/es.tld</taglib-location> </taglib> </jsp-config>
其中一个标签类
public class IsAdminTag extends TagSupport { @Override public int doStartTag() throws JspException { if (SecurityUtils.isCurrentUserAdmin()) { return TagSupport.EVAL_BODY_INCLUDE; } return SKIP_BODY; } }
JSP页面使用
<%@ taglib prefix="e" uri="/essh-tags" %> <e:isAdmin> <c:choose> <c:when test="${content.mode eq 'reply' || content.mode eq 'reject'}"> <a href="${ctxAdmin}/labor/meetingProposal/form?id=${content.id}">查看</a> </c:when> <c:otherwise> <a href="${ctxAdmin}/labor/meetingProposal/audit?id=${content.id}">审核审批答复</a> </c:otherwise> </c:choose> </e:isAdmin>
The above is the detailed content of java jsp自定义标签相关介绍. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment