当前位置:网站首页>自定义标签——jsp标签基础
自定义标签——jsp标签基础
2022-06-23 08:40:00 【安离九歌】
目录
前言
上次我们分享了反射,今天分享的内容是自定义标签。
一、认识jsp标签及其特点
1、形式
<开始标签 属性="属性值">标签体</结束标签>
2、分类
- 空标签 例如:br,hr
- ui标签 例如:input,table
- 控制标签 例如:if.foreach (没有标签体但是有输出作用的标签)
- 数据标签 例如:out标签
二、自定义标签的开发及其使用步骤
1、认识tld文件
点击c标签我们可以跳转到c.tld文件中,而c.tld文件就是c标签库的定义配置文件。
所以我们要想自定义标签,首先我们需要自定义该标签的定义配置文件.tld
2、 .tld配置文件方法:
1)新建.tld文件

2)修改uri标签

3、jsp通过taglib指令导入标签库
找到自己定义的uri
<uri>http://jsp.zhwLouis</uri>

接下来就是自定义标签代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="z" uri="http://jsp.zhwLouis"%>
<!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>
<!--
1.自定义标签库与tld相关
2.标签库中的标签与tld中的tag有关,跟tag元素对应的助手类有关
-->
<z:if test="true">true</z:if>
<z:if test="false">false</z:if>
<z:set var="name" value="李四"></z:set>
<z:out value="${name }"></z:out>
</body>
</html>运行结果如下:

4、创建助手类
上文的自定义标签,我们只是更改了uri和short-name,现在开始才是定义自己的标签
1)必须继承bodytagSupport
package com.zhw.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* 助手类 必须继承bodytagSupport
* @author zhw
*
*/
public class Demo1Tag extends BodyTagSupport{
@Override
public int doStartTag() throws JspException {
System.out.println("============doStartTag=============");
return super.doStartTag();
}
@Override
public int doAfterBody() throws JspException {
System.out.println("============doAfterBody=============");
return super.doAfterBody();
}
@Override
public int doEndTag() throws JspException {
System.out.println("============doEndTag=============");
return super.doEndTag();
}
}
5、更改tld文件中的tag
<?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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>zz</short-name>
<uri>http://jsp.zhwLouis</uri>
<validator>
<validator-class>
org.apache.taglibs.standard.tlv.JstlCoreTLV
</validator-class>
</validator>
<tag>
<name>demo</name>
<tag-class>com.zhw.tag.Demo1Tag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
我们来试用一下:

我们在zz里只定义了一个demo所以这里只会显示demo
三、标签生命周期

四、案例
1、z:if标签
助手类
package com.zhw.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; /** * if标签: * 分析 * 如果满足条件,就打印标签体--->doStartTag的返回值:EVAL_BODY_INCLUDE * 如果不满足条件,就不输出标签体--->doStartTag的返回值:SKIP_BODY * 需要获取到是否满足条件的结果值,那么该标签就有一个属性,属性值是boolean */ public class IfTag extends BodyTagSupport{ private boolean test; public boolean isTest() { return test; } public void setTest(boolean test) { this.test = test; } @Override public int doStartTag() throws JspException { // 如果不满足条件,就不输出标签体->dostarttag的返回值skip_body // 需要获取到是否满足条件的结果集,那么该标签就有一个属性,属性值是boolean return test?EVAL_BODY_INCLUDE:SKIP_BODY; } }
.tld文件配置
<?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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>JSTL 1.1 core library</description> <display-name>JSTL core</display-name> <tlib-version>1.1</tlib-version> <short-name>zz</short-name> <uri>http://jsp.zhwLouis</uri> <validator> <validator-class> org.apache.taglibs.standard.tlv.JstlCoreTLV </validator-class> </validator> <tag> <name>demo</name> <tag-class>com.zhw.tag.Demo1Tag</tag-class> <body-content>JSP</body-content> <attribute> <name>var</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <tag> <name>if</name> <tag-class>com.zhw.tag.IfTag</tag-class> <body-content>JSP</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
试用代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://jsp.zhwLouis" prefix="zz" %> <!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> <zz:if test="true">true</zz:if> </body> </html>结果如下:
2、z:set标签 z:out标签
1、set
助手类:
package com.zhw.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; public class SetTag extends BodyTagSupport{ private String var; private Object value; public String getVar() { return var; } public void setVar(String var) { this.var = var; } public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } @Override public int doStartTag() throws JspException { // 要存储数据,一键值对的方式存储,分析得来,该标签有2个属性 pageContext.setAttribute(var, value); return super.doStartTag(); } }
.tld文件:
<?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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>JSTL 1.1 core library</description> <display-name>JSTL core</display-name> <tlib-version>1.1</tlib-version> <short-name>zz</short-name> <uri>http://jsp.zhwLouis</uri> <validator> <validator-class> org.apache.taglibs.standard.tlv.JstlCoreTLV </validator-class> </validator> <tag> <name>demo</name> <tag-class>com.zhw.tag.Demo1Tag</tag-class> <body-content>JSP</body-content> <attribute> <name>var</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <tag> <name>if</name> <tag-class>com.zhw.tag.IfTag</tag-class> <body-content>JSP</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>set</name> <tag-class>com.zhw.tag.SetTag</tag-class> <body-content>JSP</body-content> <attribute> <name>var</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>value</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
2 、out
助手类:
package com.zhw.tag; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; public class OutTag extends BodyTagSupport { private Object value; public Object getValue() { return value; } public void setValue(Object value) { this.value = value; } @Override public int doStartTag() throws JspException { JspWriter out = pageContext.getOut(); try { out.print(value); } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); } }
.tld文件:
<?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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0"> <description>JSTL 1.1 core library</description> <display-name>JSTL core</display-name> <tlib-version>1.1</tlib-version> <short-name>zz</short-name> <uri>http://jsp.zhwLouis</uri> <validator> <validator-class> org.apache.taglibs.standard.tlv.JstlCoreTLV </validator-class> </validator> <tag> <name>demo</name> <tag-class>com.zhw.tag.Demo1Tag</tag-class> <body-content>JSP</body-content> <attribute> <name>var</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <tag> <name>if</name> <tag-class>com.zhw.tag.IfTag</tag-class> <body-content>JSP</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>set</name> <tag-class>com.zhw.tag.SetTag</tag-class> <body-content>JSP</body-content> <attribute> <name>var</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>value</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>out</name> <tag-class>com.zhw.tag.OutTag</tag-class> <body-content>JSP</body-content> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
试用代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.zhwLouis" prefix="zz" %>
<!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>
<zz:if test="true">true</zz:if>
<zz:set var="name" value="张三"></zz:set>
<zz:out value="${name }"></zz:out>
</body>
</html>结果如下:

总结
这次分享的内容是自定义标签库基础,希望对你能有帮助,下期预告自定义标签库加强。
我是九歌,一个喜欢编程的程序员。
如有错误还望指正,谢谢啦。
边栏推荐
- 6月《中國數據庫行業分析報告》發布!智能風起,列存更生
- Use newbeecoder UI implements data paging
- Object. Defineproperty() and data broker
- Basic use of check boxes and implementation of select all and invert selection functions
- 1、 Software architecture evaluation
- Balls and cows of leetcode topic analysis
- 528. Random Pick with Weight
- 【云计算】GFS思想优势以及架构
- On the light application platform finclip and the mobile application development platform mpaas
- Single core driver module
猜你喜欢

Analysis of JMeter pressure measurement results

The first day of employment more than ten years ago

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册

Flink错误--Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered “time“

Point cloud library PCL from introduction to mastery Chapter 10
![[cloud computing] GFS ideological advantages and architecture](/img/98/2a4ef0ca805add24d431dac9808903.png)
[cloud computing] GFS ideological advantages and architecture

Qualcomm 9x07 two startup modes

Open source technology exchange batch stream integrated data synchronization engine Chunjun data restore DDL function module analysis

GeoServer adding mongodb data source

986. Interval List Intersections
随机推荐
Geoserver添加mongoDB数据源
6月《中國數據庫行業分析報告》發布!智能風起,列存更生
[qnx hypervisor 2.2 user manual]6.1 using the QNX hypervisor system
5-rotating Daisy - rotating canvas and timer
Lighthouse cloud desktop experience
What are the PCB characteristics inspection items?
How to use the template library of barcode label software
Single core driver module
[QNX Hypervisor 2.2用户手册]6.1 使用QNX Hypervisor系统
In depth interpretation of poca smart contract platform gear: the road to parallel architecture public chain
USB peripheral driver - debug
Paper reading [quovadis, action recognition? A new model and the dynamics dataset]
Which one is better for rendering renderings? 2022 latest measured data (IV)
Chapter 1 open LDAP master-slave synchronization tower construction
3. Caller 服务调用 - dapr
TDesign update weekly report (the first week of January 2022)
单编内核驱动模块
vector的深度剖析及模拟实现
Hongmeng reads the resource file
如何评价代码质量
