当前位置:网站首页>JSP自定义标签
JSP自定义标签
2022-06-27 10:58:00 【追梦梓辰】
JSP自定义标签:
1. 标签语言特点
<开始标签 属性=“属性值”>标签体</结束标签>
空标签
<开始标签></结束标签>
<开始标签/>
2. 自定义标签的开发及使用步骤
2.1 创建一个标签助手类(继承BodyTagSupport)
标签属性必须与助手类的属性对应、且要提供对应get/set方法
rtexprvalue
2.2 创建标签库描述文件(tld),添加自定义标签的配置
注:tld文件必须保存到WEB-INF目录或其子目录
jstl标签库
2.3 在JSP通过taglib指令导入标签库,并通过指定后缀访问自定义标签
3. 标签生命周期
流程A:
SKIP_BODY
3.1 实例化标签助手类->doStartTag()------------->doEndTag()
//主要用开发简单标签
流程B:
EVAL_BODY_INCLUDE SKIP_BODY
3.2 实例化标签助手类->doStartTag()------------->doAfterBody---------------->doEndTag()…
EVAL_BODY_AGAIN
3.3 …
jrebal 热加载
SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面的后续部分
EVAL_BODY_AGAIN:再计算主体一次
jsp自定义标签的生命周期:
自定义标签配置文件:
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- 标签库描述符 -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
<!-- 代表标签库的版本号 -->
<tlib-version>1.0</tlib-version>
<!-- 代表jsp的版本 -->
<jsp-version>1.2</jsp-version>
<!-- 你的标签库的简称 -->
<short-name>z</short-name>
<!-- 你标签库的引用uri -->
<uri>/zking</uri>
<tag>
<!-- 标签名 -->
<name>if</name>
<!-- 标签工具类 -->
<tag-class>com.king.text.IfTag</tag-class>
<!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
<body-content>JSP</body-content>
<!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
<attribute>
<!-- 自定义标签的属性名称 -->
<name>test</name>
<!-- true表示必填 -->
<required>true</required>
<!-- true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<!-- 标签名 -->
<name>out</name>
<!-- 标签工具类 -->
<tag-class>com.king.text.outTag</tag-class>
<!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
<body-content>JSP</body-content>
<!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
<attribute>
<!-- 自定义标签的属性名称 -->
<name>value</name>
<!-- true表示必填 -->
<required>true</required>
<!-- true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持 -->
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<!-- 标签名 -->
<name>foreach</name>
<!-- 标签工具类 -->
<tag-class>com.king.text.foreachTag</tag-class>
<!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
<body-content>JSP</body-content>
<!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
<attribute>
<!-- 自定义标签的属性名称 -->
<name>items</name>
<!-- true表示必填 -->
<required>true</required>
<!-- true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<!-- 自定义标签的属性名称 -->
<name>var</name>
<!-- true表示必填 -->
<required>true</required>
<!-- true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持 -->
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<!-- 自定义标签的属性名称 -->
<name>varStudent</name>
<!-- true表示必填 -->
<required>false</required>
<!-- true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持 -->
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
自定义标签方法:
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; //集合
private String var;
private varStudent varstu;
private String varstudent;
//getset方法
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 {
//判断集合是否为空
//拿到迭代器
Iterator its =(Iterator) pageContext.getAttribute("it");
//判断集合或者迭代器有没有值
if(items.size() > 0 || its!=null){
Iterator it = items.iterator();
if(its != null){
//判断迭代器
it=its;
}
if(it.hasNext()){
//判断是否有下一个
ne(it);//如果有下一个则调用方法
return super.doAfterBody();//调用doAfterBody
}else{
return EVAL_PAGE;
}
}else{
return EVAL_PAGE;
}
}
private void ne(Iterator it) {
//拿到迭代器并且下标向下移一位
Object next = it.next();
//把对象存到作用域中
pageContext.setAttribute(var,next);
//把迭代器存到作用域中
pageContext.setAttribute("it",it);
//赋默认值
int index=0;
int count=1;
//存入作用域
varStudent v =(varStudent) pageContext.getAttribute("varstu");
if(v!=null){
//如果varStudent不为空则给index,count赋值
index = v.getIndex()+1;
count = v.getCount()+1;
}
//给varStudent赋值
varstu.setCount(count);
varstu.setCount(index);
//把varstudent存入到作用域中
pageContext.setAttribute(varstudent,varstu);
}
@Override
public int doAfterBody() throws JspException {
//拿到迭代器
Iterator it =(Iterator) pageContext.getAttribute("it");
if(it.hasNext()){
//判断是否有下一个
ne(it);//如果有下一个则调用方法
return super.doStartTag();
}else{
return EVAL_PAGE;
}
}
}
边栏推荐
- One copy ten, CVPR oral is accused of plagiarizing a lot
- 20 jeunes Pi recrutés par l'Institut de microbiologie de l'Académie chinoise des sciences, 2 millions de frais d'établissement et 10 millions de fonds de démarrage (à long terme)
- 【TcaplusDB知识库】Tmonitor后台一键安装介绍(二)
- Working at home is more tiring than going to work at the company| Community essay solicitation
- ci/cd自动化测试_CI / CD管道加快测试自动化的16种最佳实践
- 隐私计算FATE-离线预测
- What is the experience of telecommuting in a foreign company| Community essay solicitation
- Istio related information
- 深入理解 happens-before 原则
- Leetcode 729. My schedule I (provides an idea)
猜你喜欢

Go zero micro Service Practice Series (VII. How to optimize such a high demand)

ECMAScript 6(es6)

从零开始搭建物联网系统
![[worthy of collection] centos7 installation MySQL complete operation command](/img/23/7c4b69e1abc3a3ceba9b79cebe1c9b.png)
[worthy of collection] centos7 installation MySQL complete operation command
![[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)](/img/0a/3eae294b335c120c4aabd05e4230c3.png)
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)

记一次 .NET 某物管后台服务 卡死分析

机器学习系统在生产中的挑战
![[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)](/img/6d/8b1ac734cd95fb29e576aa3eee1b33.png)
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)

嵌入式软件架构设计-模块化

How to deploy jupyterlab in methodot?
随机推荐
Jerry's serial port communication serial port receiving IO needs to set digital function [chapter]
【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
ci/cd自动化测试_CI / CD管道加快测试自动化的16种最佳实践
Prevent being rectified after 00? I. The company's recruitment requires that employees cannot sue the company
ECMAScript 6(es6)
Oracle group statistics query
One copy ten, CVPR oral is accused of plagiarizing a lot
"Internet +" contest topic hot docking | I figure to understand 38 propositions of Baidu
Imeta: a collection of imagegp+ video tutorials of high-value drawing websites, which has been cited 360 times (220625 updates)
【TcaplusDB知识库】TcaplusDB数据构造介绍
中科院微生物所招聘青年PI 20位,2百万安家费,千万启动经费(长期有效)
KDD 2022 | 基于分层图扩散学习的癫痫波预测
Queue, two-way queue, and its application
【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
【TcaplusDB知识库】TcaplusDB新增机型介绍
Leetcode 729. My schedule I (provides an idea)
Jerry's seamless looping [chapter]
深入理解 happens-before 原则
[tcapulusdb knowledge base] Introduction to tmonitor background one click installation (I)
[tcapulusdb knowledge base] tcapulusdb tmonitor module architecture introduction