当前位置:网站首页>How to customize JSP Tags
How to customize JSP Tags
2022-07-23 11:26:00 【_ Leaf1217】
Catalog
One 、 Characteristics of tag language
Two 、 The development of user-defined label and its application steps
3、 ... and 、 The life cycle of labels
Four 、 Practice custom labels :if、set、out
One 、 Characteristics of tag language
1.1 Before we want to customize our own labels , We must first understand
Label structure :
We'll take if Tag as an example :
Understand the label structure
<c:if test="true"> The start tag
true Tagging body
</c:if> End tagLet's look at the classification of labels : Control tags 、 Data labels 、UI label
Tags that can output content in a web page without a tag body are :UI label
After testing, I found : Click on c:if The tag can jump to c.tld file in , and c.tld file Namely c Tag library definition profile !
<c:if test="true">true</c:if>
<c:if test="false">false</c:if>
<c:set var="l" value="Leaf"></c:set>
<c:out value="${l }"></c:out>Two 、 The development of user-defined label and its application steps
2.1 After understanding some basic structures and characteristics of labels , Let's explore further ,
Click if label Find out Custom tags are associated with tld file Relevant ;
We found that c.tld file Medium tag Elements The relevant tags cannot be used after deletion ;
So we get two conclusions :
<!--
1、 Custom tags are associated with tld Document related
2、 Tags in tag library and tld Medium tag The elements are related to , That is to say, with tag Element is related to the corresponding helper class .
-->
So we thought if we put this tld Make a copy of the document , Change what needs to be changed into your own , Can it also be used ?
So we put tld Make a copy of the document :Leaf.tld, According to the introduced code :
<%@ taglib prefix="l" uri="http://jsp.leaf.com" %>
We thought about tld Is there this in the file uri, therefore Ctrl+F Look for , As expected tld Found this in the file uri, So we changed it into our own definition uri:
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>http://jsp.leaf.com</uri>
And then create a new one Jsp file , Introduce the customized tld file , It was found that there was !
Then we use this self-defined tld File test those labels to see if they can be used :
notes : Copy of the tld The file must be saved in WEB-IFN In a directory or subdirectory
<l:if test="true">true</l:if>
<l:if test="false">false</l:if>
<l:set var="l" value="Leaf"></l:set>
<l:out value="${l }"></l:out>Run web page :

We found normal availability !
3、 ... and 、 The life cycle of labels
3.1 We also learned about custom tags tld file 、tag Elements ,
Next, let's take a look at its life cycle :
Let's create a new class , Inherit tag The helper class corresponding to the element :BodyTagSupport
DemoTag1 class :
package com.leaf.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Helper : Must inherit bodytagSupport
* @author Leaf
*
* 2022 year 6 month 17 Japan Afternoon 7:32:58
*/
public class DemoTag1 extends BodyTagSupport {
@Override
public int doStartTag() throws JspException {
System.out.println("========= The start tag doStartTag=========");
//return SKIP_BODY;// Skip the label body
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
System.out.println("========= Tagging body doAfterBody=========");
return super.doAfterBody();
//return EVAL_BODY_AGAIN;// Loop label body
}
@Override
public int doEndTag() throws JspException {
System.out.println("========= End tag doEndTag=========");
return super.doEndTag();
//return SKIP_PAGE;// Don't calculate the following content
}
}After many running tests, we get some Conclusion :
<!--
1、 With label body , By default, the helper class will be called doStartTag、doAfterBody、doEndTag Method ;
2、 If the start tag doStartTag The value returned is SKIP_BODY, Then label body doAfterBody There will be no call to execute ;
3、 If the label will start doStartTag The return value of is changed to EVAL_BODY_INCLUDE, Then label body doAfterBody Will call to execute ;
4、 If the label will start doStartTag The return value of is changed to EVAL_BODY_AGAIN, Then the tag body will be called all the time doAfterBody Method , Into the loop ;
5、 If the end tag doEndTag The return value of is changed to SKIP_PAGE, Then the following content will not be calculated .
-->
Four 、 Practice custom labels :if、set、out
Learned so much theoretical knowledge , Most of them are experimental tests , Not very good-looking running results ,
Next, we will use our theoretical knowledge to practice :
4.1 Customize a few of your own tags ~
4.2 if Definition of label
We need to build one first if The tag class , Inherit helper class : Define a if attribute test:boolean type
package com.leaf.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
* @author Leaf
*
* 2022 year 6 month 17 Japan Afternoon 8:29:39
*/
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 {
return test ? EVAL_BODY_INCLUDE : SKIP_BODY;
}
}After we write this tag class, we will go Leaf.tld Write the relevant configuration in the file , We delete all other configurations ,
Leave a template :
4.3 To configure tld file :<c:if test=""></c:if>
<?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>l</short-name>
<uri>http://jsp.leaf.com</uri>
<validator>
<description>
Provides core validation features for JSTL tags.
</description>
<validator-class>
org.apache.taglibs.standard.tlv.JstlCoreTLV
</validator-class>
</validator>
<tag>
<!-- Represents the name of the tag library tag -->
<name>if</name>
<!-- The helper class corresponding to this tag -->
<tag-class>com.leaf.tag.IfTag</tag-class>
<!-- The representative is a JSP label -->
<body-content>JSP</body-content>
<attribute>
<!-- This custom JSP The attribute name of the tag -->
<name>test</name>
<!-- Whether this attribute is required -->
<required>true</required>
<!-- Whether the attribute value supports the expression -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>Let's use it to test :
<l:if test="true">true</l:if>
<l:if test="false">false</l:if>
function :

We found no problem !
Then let's continue to try Set as well as Out The definition of label :
Set The tag class : This is where we get a place to store data :pageContext
package com.leaf.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Data labels : Storing data
* Scope :pagecontext、request、session、application(servletContext)
*
* To store data , Store in the form of key value pairs , Analysis shows that the label has 2 Attributes
* @author Leaf
*
* 2022 year 6 month 18 Japan In the morning 12:23:18
*/
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 , Store in the form of key value pairs , Analysis shows that the label has 2 Attributes
pageContext.setAttribute(var, value);
return super.doStartTag();
}
}Let's continue to define tag classes :
Out The tag class : We mainly get an output stream :pageContext.getOut();
package com.leaf.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Out label
* servlet Background code Foreground output :out.print
* Output the data to the foreground , First get the output stream
* @author Leaf
*
* 2022 year 6 month 18 Japan In the morning 12:32:10
*/
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 {
// Get the output stream
JspWriter out = pageContext.getOut();
try {
// Output value
out.print(value);
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
}After writing the label class, we will go to Leaf.tld file Configure it :
Leaf.tld File configuration :
<tag>
<name>set</name>
<tag-class>com.leaf.tag.SetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.leaf.tag.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>Finally, let's test it together :
<l:if test="true">true</l:if>
<l:if test="false">false</l:if>
<l:set var="l" value="Leaf"></l:set>
<l:out value="${l }"></l:out>
Run web page :

We found that we all succeeded ~
OK, such Leaf That's all for today's learning notes about custom tags ~
边栏推荐
- Request data acquisition and response
- Handwritten promise.resolve, promise reject, Promise.all
- Application of higher-order functions: handwritten promise source code (III)
- Vite X Figma 打造设计师专属的 i18n 插件
- js中类数组对象以及类数组转换的方法(ES6, ES5)
- BurpSuite学习笔记
- Scattered notes of machine learning: some concepts and notes
- 文件上传漏洞原理
- laravel api接口+令牌认证登录
- Spectral clustering | Laplace matrix
猜你喜欢

Goodbye if else

通用查询&分页代码

Custom formula input box

机器学习零散笔记:一些概念和注意

flex+js实现内部盒子高度跟随其中最大的高度
[email protected]‘] failed with code 1"/>npm init vite-app <project-name> 报错 Install for [‘[email protected]‘] failed with code 1

Common errors in C language debugging -- brief answer

中间人攻击arp欺骗及与beef-xss联动

【C语言】什么是函数?函数的分类和侧重(帮你快速分类和记忆函数)
D2dengine edible tutorial (1) -- the simplest program
随机推荐
JS event loop
[Python flask note 5] Blueprint simple à utiliser
Application of higher-order functions: handwritten promise source code (I)
高阶函数的应用:手写Promise源码(一)
MySQL索引&&执行计划
After the formula in word in WPS is copied, there is a picture
How to merge the website content with video and audio separated? How to write batch download code?
如何自定义Jsp标签
页面实现 “实时数据响应” 的注意事项
Analysis of two-part search method or half search method of C language (classic example, classic analysis)
Simple implementation of rectangular area block
C语言中的分支和循环语句归属
[Hudi]hudi的编译及hudi&spark和hudi&flink的简单使用
【6.28】
Hyperlink de underlined code
Install enterprise pycharm and Anaconda
some、every、find、findIndex的用法
构造函数,原型链,instanceOf
Flex+js realizes that the height of the internal box follows the maximum height
js的闭包的理解