当前位置:网站首页>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>边栏推荐
猜你喜欢

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

【HCIP】BGP小型实验(联邦,优化)

链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?

【21天学习挑战赛】顺序查找和二分查找的小总结

如何重装Win11?一键重装Win11方法

Double queue implementation stack?Dual stack implementation queue?

如何设计循环队列?快进来学习~

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

Study Notes: The Return of Machine Learning
![[Three sons] C language implements simple three sons](/img/96/c3f6c331cbc6d794dc5381cf176ba7.png)
[Three sons] C language implements simple three sons
随机推荐
[头条]笔试题——最小栈
短视频SEO搜索运营获客系统功能介绍
How to solve the error when mysql8 installs make
PHP从txt文件中读取数据的方法
IP核:FIFO
OpenCV DNN blogFromImage() detailed explanation
C language Qixi is coming!It's time to show the romance of programmers!
TCL:在Quartus中使用tcl脚本语言进行管脚约束
【无标题】
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
Simpson's paradox
以交易为生是一种什么体验?
已知中序遍历数组和先序遍历数组,返回后序遗历数组
【Leetcode】478. Generate Random Point in a Circle(配数学证明)
An interesting project--Folder comparison tool (1)
What is it like to trade for a living?
微软电脑管家V2.1公测版正式发布
07-SDRAM: FIFO control module
IO流基础
SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture