当前位置:网站首页>What is the function of the JSP Taglib directive?
What is the function of the JSP Taglib directive?
2022-08-02 00:18:00 【qq_25073223】
转自:
JSP TaglibWhat is the function of the command?
下文讲述JSP中TaglibA brief description of the function of the command,如下所示:
Taglib指令的功能:
Defines a tag library and prefixes for its custom tags 一个自定义的tagA label is a user-defined typeJSP标记 when one contains custom tag标签的JSP页面被jsp引擎编译成servlet时 此时tag标签被转化成了对一个称为tagHandles operations performed by objects of a class 当JSP页面被jsp引擎转化为servlet后,实际上tagLabels are converted into pairstag处理类的操作
Taglib指令的语法
<%@ taglib uri="" prefix="c"%>
prefix:is a tag library alias
taglib之uri:引入jsp的标签库
Taglib子元素
| Element | Description |
| tlib-version | Tag库的版本 |
| jsp-version | Tag库所需要的jsp的版本 |
| short-name: | 助记符,tag的一个别名(可选) |
| uri | Used to determine a uniquetag库 |
| display-name | be visualized(诸如Jbuilder)The name used to display(可选) |
| small-icon | be visualized(诸如Jbuilder)Small icon to display(可选) |
| large-icon | be visualized(诸如Jbuilder)Large icons for display(可选) |
| description | 对tag库的描述(可选) |
| listener | 一个tagA library may define some classes as its event listener classes,这些类在TLD中被称为listener 元素,jspThe server will instantiate these listener classes,并且注册它们.ListenerOne of the elements is calledlistener-class的子元素,The value of this element must be the full class name of the listener class |
| tag | tag元素在tagThe library needs to indicate its name、类名、脚本变量、tag的属性 The value of the script variable can be directly inTLDdefined in or passedtagAdditional information for the class to obtain Each attribute describes whether the attribute can be omitted,Whether its value can be passed<%= …%> 这样的JSPgrammar to get,and the type of the property tag子元素如下: name:Unique element name tag-class:Tag标签对应的tag处理类 tei-class:javax.servlet.jsp.tagext.TagExtraInfo的子类,Used to express script variables(可选) body-content:Tag标签body的类型 display-name:be visualized(诸如Jbuilder)The name used to display(可选) small-icon:be visualized(诸如Jbuilder)Small icon to display(可选) large-icon:be visualized(诸如Jbuilder)Large icons for display(可选) description:此tag标签的描述 variable:Provides information about script variables(同tei-class)(可选) attribute:Tag标签的属性名 |
Taglib引入方式
jsp文件:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="myjstl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:out value="${param.username}"/>
</body>
</html>
web.xml
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
...
<jsp-config>
<taglib>
<taglib-uri>myjstl</taglib-uri>
<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib>
</jsp-config>
...
</web-app>
Standard definition labels
jsp Taglib指令tld例子
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>collection</title>
</head>
<body>
<c:forEach var="l" items="${list}">
<table>
<th>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
<td>时间</td>
</th>
<tr>
<td>${l.id}</td>
<td><a href="*****${l.id}">${l.name}</a></td>
<td>${l.age}</td>
<td><a href="*****${l.url}">编辑</a><a href="*****${l.url}">删除</a></td>
</tr>
</table>
</c:forEach>
</body>
</html>
开发自定义标签
开发自定义标签,The following methods are required:
步骤一:
必须继承一个父类:javax.servlet.jsp.tagext.SimpleTagSupport
每个属性都有对应的getter和setter方法.
重写doTag()或者doStartTag()或doEndTag()方法方法
This method is responsible for generating the page content.
public class TestTag extends TagSupport {
private static final long serialVersionUID = -3382691015235241708L;
@Override
public int doEndTag() throws JspException {
try {
pageContext.getOut().write("MyTag");
return super.doEndTag();
} catch(JspException e) {
e.printStackTrace();
return 0;
} catch(IOException e) {
e.printStackTrace();
return 0;
}
}
@Override
public int doStartTag() {
try {
pageContext.getOut().write("MyTag");
return super.doStartTag();
} catch(JspException e) {
e.printStackTrace();
return 0;
} catch(IOException e) {
e.printStackTrace();
return 0;
}
}
}
步骤二:
建立一个*.tld文件,每个*.tldThe file corresponds to a tag library,Each tag library corresponds to multiple tags
注意事项:
TLD(Tag Library Definition)That is, the tag library definition
文件的后缀是tld
每个TLDThe file corresponds to a tag library
A tag library can contain multiple tags
TLDThe file is also known as a tag library definition file
The root element of the tag library definition file is taglib
它可以包含多个tag子元素
每个tagChild elements all define a label
<?xml version="1.0" encoding="utf-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd" version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>myhelloworld</short-name>
<!-- 定义该标签库的URI Must be added but can be empty-->
<uri/>
<!-- 定义第一个标签 -->
<tag>
<!-- 定义标签名 -->
<name>thisTag</name>
<!-- 定义标签处理类 -->
<tag-class>com.java265.taglib.TestTag</tag-class>
<!-- 定义标签体为空 -->
<body-content>empty</body-content>
</tag>
</taglib>边栏推荐
- 链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
- NFT工具合集
- DOM 事件及事件委托
- IP核:FIFO
- JSP如何使用page指令让JSP文件支持中文编码呢?
- SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
- Excel文件读写(创建与解析)
- Arduino 基础语法
- Study Notes: The Return of Machine Learning
- Short video SEO optimization tutorial Self-media SEO optimization skills and methods
猜你喜欢

Zadig 面向开发者的自测联调子环境技术方案详解

单片机遥控开关系统设计(结构原理、电路、程序)

IP核:FIFO

An interesting project--Folder comparison tool (1)

How to reinstall Win11?One-click method to reinstall Win11

一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置

Study Notes: The Return of Machine Learning

已知中序遍历数组和先序遍历数组,返回后序遗历数组

Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers

GetHashCode方法与=
随机推荐
TCP 可靠吗?为什么?
路由策略
els 方块边界变形处理
JSP out.print()和out.write()方法的不同之处
【三子棋】C语言实现简易三子棋
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
go语言标准库fmt包怎么使用
632. 最小区间
After an incomplete recovery, the control file has been created or restored, the database must be opened with RESETLOGS, interpreting RESETLOGS.
Arduino 基础语法
LeetCode_279_完全平方数
单片机遥控开关系统设计(结构原理、电路、程序)
一文概览最实用的 DeFi 工具
好好活就是做有意义的事,有意义的事就是好好活
contentEditable属性
EasyExcel的简单读取操作
async/await 原理及执行顺序分析
security 会话并发管理
扑克牌问题
短视频SEO搜索运营获客系统功能介绍