当前位置:网站首页>JSP custom tag
JSP custom tag
2022-06-27 11:20:00 【Chasing dream Zichen】
JSP Custom tag :
1. Tag language features
< The start tag attribute =“ Property value ”> Tagging body </ End tag >
Empty label
< The start tag ></ End tag >
< The start tag />
2. Development and use steps of custom labels
2.1 Create a label helper class ( Inherit BodyTagSupport)
The tag property must correspond to the property of the helper class 、 And corresponding information shall be provided get/set Method
rtexprvalue
2.2 Create a label library description file (tld), Add custom label configuration
notes :tld The file must be saved to WEB-INF Directory or its subdirectories
jstl Tag library
2.3 stay JSP adopt taglib Instruction import label Library , And access custom tags by specifying suffixes
3. Tag life cycle
technological process A:
SKIP_BODY
3.1 Instantiate the tag helper class ->doStartTag()------------->doEndTag()
// Mainly used to develop simple labels
technological process B:
EVAL_BODY_INCLUDE SKIP_BODY
3.2 Instantiate the tag helper class ->doStartTag()------------->doAfterBody---------------->doEndTag()…
EVAL_BODY_AGAIN
3.3 …
jrebal Thermal loading
SKIP_BODY: Skip body
EVAL_BODY_INCLUDE: Calculate the main content of the label and [ Output ]
EVAL_PAGE: Calculate subsequent parts of the page
SKIP_PAGE: Skip the rest of the page
EVAL_BODY_AGAIN: Calculate the subject again
jsp Customize the life cycle of the tag :
Custom label profile :
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- Tag library descriptor -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
<!-- Represents the version number of the tag library -->
<tlib-version>1.0</tlib-version>
<!-- representative jsp Version of -->
<jsp-version>1.2</jsp-version>
<!-- The abbreviation of your tag library -->
<short-name>z</short-name>
<!-- Reference to your tag library uri -->
<uri>/zking</uri>
<tag>
<!-- Tag name -->
<name>if</name>
<!-- Label tool class -->
<tag-class>com.king.text.IfTag</tag-class>
<!-- The content type of the tag :empty Represents an empty label ,jsp It can be any legal JSP Elements -->
<body-content>JSP</body-content>
<!-- Custom label attribute definitions , Please note that the corresponding... Must be provided in the tag class get/set Method -->
<attribute>
<!-- The attribute name of the custom tag -->
<name>test</name>
<!-- true It means required -->
<required>true</required>
<!-- true Support dynamic values , You can fill in the value jsp expression 、EL expression ,false Do not support -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<!-- Tag name -->
<name>out</name>
<!-- Label tool class -->
<tag-class>com.king.text.outTag</tag-class>
<!-- The content type of the tag :empty Represents an empty label ,jsp It can be any legal JSP Elements -->
<body-content>JSP</body-content>
<!-- Custom label attribute definitions , Please note that the corresponding... Must be provided in the tag class get/set Method -->
<attribute>
<!-- The attribute name of the custom tag -->
<name>value</name>
<!-- true It means required -->
<required>true</required>
<!-- true Support dynamic values , You can fill in the value jsp expression 、EL expression ,false Do not support -->
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<!-- Tag name -->
<name>foreach</name>
<!-- Label tool class -->
<tag-class>com.king.text.foreachTag</tag-class>
<!-- The content type of the tag :empty Represents an empty label ,jsp It can be any legal JSP Elements -->
<body-content>JSP</body-content>
<!-- Custom label attribute definitions , Please note that the corresponding... Must be provided in the tag class get/set Method -->
<attribute>
<!-- The attribute name of the custom tag -->
<name>items</name>
<!-- true It means required -->
<required>true</required>
<!-- true Support dynamic values , You can fill in the value jsp expression 、EL expression ,false Do not support -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<!-- The attribute name of the custom tag -->
<name>var</name>
<!-- true It means required -->
<required>true</required>
<!-- true Support dynamic values , You can fill in the value jsp expression 、EL expression ,false Do not support -->
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<!-- The attribute name of the custom tag -->
<name>varStudent</name>
<!-- true It means required -->
<required>false</required>
<!-- true Support dynamic values , You can fill in the value jsp expression 、EL expression ,false Do not support -->
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
Custom label method :
package com.king.text;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.util.Iterator;
import java.util.List;
public class foreachTag extends BodyTagSupport {
private List items; // aggregate
private String var;
private varStudent varstu;
private String varstudent;
//getset Method
public String getVarstudent() {
return varstudent;
}
public void setVarstudent(String varstudent) {
this.varstudent = varstudent;
}
public List getItems() {
return items;
}
public void setItems(List items) {
this.items = items;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public varStudent getVarstu() {
return varstu;
}
public void setVarstu(varStudent varstu) {
this.varstu = varstu;
}
@Override
public int doStartTag() throws JspException {
// Determines if the set is empty
// Get the iterator
Iterator its =(Iterator) pageContext.getAttribute("it");
// Determine whether a set or iterator has a value
if(items.size() > 0 || its!=null){
Iterator it = items.iterator();
if(its != null){
// Judge iterators
it=its;
}
if(it.hasNext()){
// Judge whether there is next
ne(it);// If there is a next one, call the method
return super.doAfterBody();// call doAfterBody
}else{
return EVAL_PAGE;
}
}else{
return EVAL_PAGE;
}
}
private void ne(Iterator it) {
// Get the iterator and move the subscript down one bit
Object next = it.next();
// Save the object to the scope
pageContext.setAttribute(var,next);
// Save the iterator to the scope
pageContext.setAttribute("it",it);
// Assign default
int index=0;
int count=1;
// Save to scope
varStudent v =(varStudent) pageContext.getAttribute("varstu");
if(v!=null){
// If varStudent If not empty, give index,count assignment
index = v.getIndex()+1;
count = v.getCount()+1;
}
// to varStudent assignment
varstu.setCount(count);
varstu.setCount(index);
// hold varstudent Save to scope
pageContext.setAttribute(varstudent,varstu);
}
@Override
public int doAfterBody() throws JspException {
// Get the iterator
Iterator it =(Iterator) pageContext.getAttribute("it");
if(it.hasNext()){
// Judge whether there is next
ne(it);// If there is a next one, call the method
return super.doStartTag();
}else{
return EVAL_PAGE;
}
}
}
边栏推荐
- 【TcaplusDB知识库】TcaplusDB-tcaplusadmin工具介绍
- 0基础了解电商系统如何对接支付渠道
- 中科院微生物所招聘青年PI 20比特,2百萬安家費,千萬啟動經費(長期有效)
- Mqtt protocol stack principle and interaction flow chart
- Ci/cd automatic test_ 16 best practices for CI / CD pipeline to accelerate test automation
- 面试突击60:什么情况会导致 MySQL 索引失效?
- [tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)
- Red envelope rain: a wonderful encounter between redis and Lua
- 15+城市道路要素分割应用,用这一个分割模型就够了!
- Oracle group statistics query
猜你喜欢

Ci/cd automatic test_ 16 best practices for CI / CD pipeline to accelerate test automation

飞桨产业级开源模型库:加速企业AI任务开发与应用

Future & CompletionService
![[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (I)](/img/04/b1194ca3340b23a4fb2091d1b2a44d.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb tcapsvrmgr tool (I)

Glide缓存机制
![Jerry's serial port communication serial port receiving IO needs to set digital function [chapter]](/img/d1/5beed6c86c4fdc024c6c9c66fbffb8.png)
Jerry's serial port communication serial port receiving IO needs to set digital function [chapter]

嵌入式软件架构设计-模块化
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area](/img/b7/2358e8cf1cdaeaba77e52d04cc74d4.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area

ci/cd自动化测试_CI / CD管道加快测试自动化的16种最佳实践

Native JS implements page scroll bar loading data and page drop-down loading content
随机推荐
Native JS implements page scroll bar loading data and page drop-down loading content
[tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
深入理解 happens-before 原则
Leetcode 729. My schedule I (awesome, solved)
[methodot topic] what kind of low code platform is more suitable for developers?
ECMAScript 6(es6)
【TcaplusDB知识库】TcaplusDB表数据缓写介绍
【TcaplusDB知识库】TcaplusDB运维单据介绍
Matlab exercises - create 50 rows and 50 columns of all zero matrix, all 1 matrix, identity matrix, diagonal matrix, and output the 135 element of the matrix.
Future & CompletionService
C语言0长度数组的妙用
器审科普:创新医疗器械系列科普——胸骨板产品
[tcapulusdb knowledge base] tcapulusdb system user group introduction
[tcapulusdb knowledge base] Introduction to tcapulusdb analytical text export
嵌入式软件架构设计-模块化
"Internet +" contest topic hot docking | I figure to understand 38 propositions of Baidu
QStyle类用法总结(三)
deep learning statistical arbitrage
【TcaplusDB知识库】TcaplusDB业务数据备份介绍
Oracle multi table query