当前位置:网站首页>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;
}
}
}
边栏推荐
- Leetcode 729. 我的日程安排表 I(牛逼,已解决)
- [tcapulusdb knowledge base] Introduction to tmonitor system upgrade
- Uniform Asymptotics by Alexei
- MQTT协议栈原理及交互流程图
- KDD 2022 | epileptic wave prediction based on hierarchical graph diffusion learning
- Codeforces Round #786 (Div. 3) ABCDE
- Unity Shader学习(二)第一个Shader
- AUTOCAD——三种修剪方式
- 直播電子商務應用程序開發需要什麼基本功能?未來發展前景如何?
- 深入理解 happens-before 原则
猜你喜欢
随机推荐
【TcaplusDB知识库】Tmonitor系统升级介绍
Leetcode 177 The nth highest salary (June 26, 2022)
【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
Mqtt protocol stack principle and interaction flow chart
NAACL 2022 | TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
Metadata of database
【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
Challenges of machine learning system in production
JSP自定义标签
KDD 2022 | 基于分层图扩散学习的癫痫波预测
Quelles sont les fonctions de base nécessaires au développement d'applications de commerce électronique en direct? Quelles sont les perspectives d'avenir?
[tcapulusdb knowledge base] tcapulusdb doc acceptance - create business introduction
杰理之一直喂狗会频繁开关中断导致定时器【篇】
VPT Model Video Explanation
Jerry added an input capture channel [chapter]
Istio related information
Institute of Microbiology, Chinese Academy of Sciences recruited 20 young PI, with a resettlement fee of 2million yuan and a start-up fund of 10million yuan (long-term effective)
Write it down once Net analysis of a property management background service stuck
[tcapulusdb knowledge base] tcapulusdb business data backup introduction
杰理之睡眠以后定时唤醒系统继续跑不复位【篇】