Java学习路线-55:自定义JSP标签
【摘要】 第 10 章 : 自定义标签
编写步骤:
标签处理类编写 tld 文件taglib 导入 tld 文件
依赖
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <vers...
第 10 章 : 自定义标签
编写步骤:
- 标签处理类
- 编写 tld 文件
- taglib 导入 tld 文件
依赖
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope>
</dependency>
<dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
定义无内容标签
package com.pengshiyu.taglibs;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class HelloTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { this.getJspContext().getOut().println("hello"); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
声明
<?xml version="1.0" encoding="utf-8"?>
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD with Body</short-name> <tag> <name>Hello</name> <tag-class>com.pengshiyu.taglibs.HelloTag</tag-class> <body-content>empty</body-content> </tag>
</taglib>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
引入声明文件并使用标签
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="ext" uri="WEB-INF/custom.tld" %>
<ext:Hello />
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
定义有内容标签
package com.pengshiyu.taglibs;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class HelloTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspWriter out = this.getJspContext().getOut(); out.write("******"); this.getJspBody().invoke(out); out.write("******"); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="ext" uri="WEB-INF/custom.tld" %>
<ext:Hello>
你好
</ext:Hello>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
输出
****** 你好 ******
- 1
不再执行标签下面的内容
package com.pengshiyu.taglibs;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.SkipPageException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class HelloTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { this.getJspContext().getOut().write("以下内容不显示"); throw new SkipPageException(); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
带有属性的标签
标签类
package com.pengshiyu.taglibs;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
public class HelloTag extends SimpleTagSupport { private String name = ""; public void setName(String name) { this.name = name; } @Override public void doTag() throws JspException, IOException { this.getJspContext().getOut().write(this.name); }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
tld 配置
<tag> <name>Hello</name> <tag-class>com.pengshiyu.taglibs.HelloTag</tag-class> <body-content>scriptless</body-content> <attribute>
<!-- 属性名称--> <name>name</name>
<!-- 是否可选--> <required>true</required>
<!-- 支持表达式--> <rtexprvalue>true</rtexprvalue> </attribute>
</tag>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
使用
<ext:Hello name="Tom"></ext:Hello>
- 1
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/106889113
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)