当前位置:网站首页>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>
边栏推荐
- 08-SDRAM:汇总
- TCL: Pin Constraints Using the tcl Scripting Language in Quartus
- 短视频seo搜索优化主要内容
- Difference between JSP out.print() and out.write() methods
- An overview of the most useful DeFi tools
- 06-SDRAM : SDRAM control module
- ROS 动态参数
- TCL:在Quartus中使用tcl脚本语言进行管脚约束
- C language Qixi is coming!It's time to show the romance of programmers!
- 含外部储能的电力系统暂态稳定分布式控制
猜你喜欢
mysql8安装make报错如何解决
如何设计循环队列?快进来学习~
How to reinstall Win11?One-click method to reinstall Win11
图解LeetCode——1161. 最大层内元素和(难度:中等)
08-SDRAM: Summary
background-image使用
Is TCP reliable?Why?
短视频seo搜索优化主要内容
【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
随机推荐
An overview of the most useful DeFi tools
【Leetcode】478. Generate Random Point in a Circle(配数学证明)
微软电脑管家V2.1公测版正式发布
控制电机的几种控制电路原理图
如何重装Win11?一键重装Win11方法
ROS 动态参数
基于数据驱动的变电站巡检机器人自抗扰控制
[头条]笔试题——最小栈
JSP 如何获取request对象中的路径信息呢?
Quick solution for infix to suffix and prefix expressions
链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
如何优雅的消除系统重复代码
async/await 原理及执行顺序分析
Axure tutorial - the new base (small white strongly recommended!!!)
security CSRF漏洞保护
为什么要使用MQ消息中间件?这几个问题必须拿下
Using the "stack" fast computing -- reverse polish expression
After an incomplete recovery, the control file has been created or restored, the database must be opened with RESETLOGS, interpreting RESETLOGS.
利用“栈”快速计算——逆波兰表达式
中缀转后缀、前缀表达式快速解决办法