当前位置:网站首页>Custom tag - JSP tag Foundation
Custom tag - JSP tag Foundation
2022-06-23 08:57:00 【An Li Jiu Ge】
Catalog
One 、 know jsp Label and its characteristics
Two 、 The development of user-defined label and its application steps
2、 .tld Configuration file method :
3、jsp adopt taglib Instruction import label Library
5、 change tld In the document tag
Preface
Last time we shared reflections , Today's sharing is about custom tags .
One 、 know jsp Label and its characteristics
1、 form
< The start tag attribute =" Property value "> Tagging body </ End tag >
2、 classification
- Empty label for example :br,hr
- ui label for example :input,table
- Control tags for example :if.foreach ( A label that has no label body but has an output function )
- Data labels for example :out label
Two 、 The development of user-defined label and its application steps
1、 know tld file 
Click on c Tag we can jump to c.tld In file , and c.tld The document is c Tag library definition profile .
So we want to customize the label , First of all, we need Customize the definition profile of this tag .tld
2、 .tld Configuration file method :
1) newly build .tld file

2) modify uri label

3、jsp adopt taglib Instruction import label Library
Find your own definition uri
<uri>http://jsp.zhwLouis</uri>

Next is the custom tag code :
<%@ 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. Custom label library and tld relevant
2. Tags in tag library and tld Medium tag of , Follow tag Element is related to the corresponding helper class
-->
<z:if test="true">true</z:if>
<z:if test="false">false</z:if>
<z:set var="name" value=" Li Si "></z:set>
<z:out value="${name }"></z:out>
</body>
</html>The operation results are as follows :

4、 Create helper class
Custom labels above , We just changed uri and short-name, Now it's time to define your own label
1) Must inherit bodytagSupport
package com.zhw.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Helper Must inherit 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、 change tld In the document 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>
Let's try it out :

We are zz There is only one defined in demo So it just shows demo
3、 ... and 、 Tag life cycle

Four 、 Case study
1、z:if label
Helper
package com.zhw.tag; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.BodyTagSupport; /** * if label : * analysis * If the conditions are met , Just print the label body --->doStartTag The return value of :EVAL_BODY_INCLUDE * If you don't do that , The label body is not output --->doStartTag The return value of :SKIP_BODY * You need to get the result value of whether the condition is met , Then the tag has an attribute , The property value is 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 { // If you don't do that , The label body is not output ->dostarttag The return value of skip_body // You need to get the result set that meets the conditions , Then the tag has an attribute , The property value is boolean return test?EVAL_BODY_INCLUDE:SKIP_BODY; } }
.tld File configuration
<?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>
The trial code is as follows :
<%@ 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>give the result as follows :
2、z:set label z:out label
1、set
Helper :
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 { // To store data , One key value pairs , From analysis , The label has 2 Attributes pageContext.setAttribute(var, value); return super.doStartTag(); } }
.tld file :
<?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
Helper :
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 file :
<?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>
Trial code :
<%@ 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=" Zhang San "></zz:set>
<zz:out value="${name }"></zz:out>
</body>
</html>give the result as follows :

summary
The content shared this time is the basis of custom tag library , I hope it can help you , In the next issue, the user-defined label library will be enhanced .
I'm nine songs , A programmer who likes programming .
If there is any mistake, please correct it , Thank you very much! .
边栏推荐
- @Response
- Map (set) operation in go language
- Intelligent operation and maintenance exploration | anomaly detection method in cloud system
- How to sort a dictionary by value or key?
- Leetcode topic analysis 3sum closest
- Use newbeecoder UI implements data paging
- How can I handle the "unable to load" exception when easyplayer plays webrtcs?
- 670. Maximum Swap
- Implementing an open source app store with swiftui
- [QNX Hypervisor 2.2用户手册]5.6.1 Guest关机时静默设备
猜你喜欢

6月《中国数据库行业分析报告》发布!智能风起,列存更生

JS mask important data of ID card and mobile phone number with * *

社区文章|MOSN 构建 Subset 优化思路分享

测试-- 自动化测试selenium(关于API)

Geoserver添加mongoDB数据源
![[cloud native | kubernetes] kubernetes principle and installation (II)](/img/db/dd93bbcac6d0404d44f67d2da12880.png)
[cloud native | kubernetes] kubernetes principle and installation (II)

173. Binary Search Tree Iterator
![[cloud computing] GFS ideological advantages and architecture](/img/98/2a4ef0ca805add24d431dac9808903.png)
[cloud computing] GFS ideological advantages and architecture

Quartz Crystal Drive Level Calculation

力扣之滑动窗口《循序渐进》(209.长度最小的子数组、904. 水果成篮)
随机推荐
Leetcode topic analysis 3sum closest
[cloud native | kubernetes] kubernetes principle and installation (II)
523. Continuous Subarray Sum
Unique paths for leetcode topic resolution
[event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24
USB peripheral driver - debug
【学习资源】理解数学和热爱数学
How to use the template library of barcode label software
438. Find All Anagrams in a String
Linux MySQL installation
node request模塊cookie使用
What exactly is RT?
MySQL故障案例 | mysqldump: Couldn’t execute ‘SELECT COLUMN_NAME
Lighthouse cloud desktop experience
How to sort a dictionary by value or key?
Leetcode topic analysis group anagrams
Driver Architecture & platform platform bus driver model
297. Serialize and Deserialize Binary Tree
6-shining laser application of calayer
636. Exclusive Time of Functions
