JSTL.自定义标签
来源:互联网 发布:手机淘宝就是淘小铺吗 编辑:程序博客网 时间:2024/06/14 18:41
自定义标签:
- 实现Tag接口,即传统标签。不建议使用!
- 实现SimpleTag接口,即简单标签。建议使用!
JavaWeb中还提供了SimpleTagSupport类,继承它要比实现SimpleTag接口方便。
自定义标签
1. 步骤
*标签处理类(标签也是一个对象,那么就需要先有类!)
*tld文件,它是一个xml
*页面中使用<%@taglib%>来指定tld文件的位置
2. 标签处理类
SimpleTag接口:
*void doTag():每次执行标签时都会调用这个方法;
*JspTag getParent():返回父标签(非生命周期方法)
*void setParent(JspTag):设置父标签
*void setJspBody(JspFragment):设置标签体
*void seetJspContext(JspContext):设置jsp上下文对象,它儿子是PageContext
其中doTag()会在其他三个方法之后被tomcat调用。
标签处理类:继承SimpleTagSupport类
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
// 获取JspContext对象,再获取out对象,再向页面输出
// 获取到的JspContext其实就是当前页面的pageContext对象
this.getJspContext().getOut().write(“
Hello SimpleTag!</p>”) ;
}
}
3. 配置tld文件,一个tld文件中可以部署多个标签 <tag> </tag>
tld文件一般都放到WEB-INF之下,这样保证客户端访问不到!
<tag>
<name>myTag1</name> 指定当前标签的名称
<tag-class>cn.itcast.tag.MyTag1</tag-class> 指定当前标签的标签处理类!
<body-content>empty</body-content> 指定标签体的类型,我们这里使用的是空标签!
</tag>
标签描述符文件(tld)
/WEB-INF/tlds/itcast.tld<?xml version="1.0" encoding="UTF-8"?><taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"; xmlns:xml="http://www.w3.org/XML/1998/namespace"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "> <tlib-version>1.0</tlib-version> <short-name>itcast</short-name> <uri>http://www.itcast.cn/tags</uri>; <tag> <name>hello</name> <!--标签名称--> <tag-class>cn.itcast.tag.HelloTag</tag-class> <!--标签处理类名称--> <body-content>empty</body-content> <!--标签体为空,即空标签--> </tag></taglib>
4. jsp页面中使用自定义标签
<%@ taglib prefix=”it” uri=”/WEB-INF/tlds/itcast-tag.tld” %>
……
<it:hello/>
导标签库,就是为页面指定tld文件的位置!
有标签体的标签
1. 标签处理类
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
PageContext pc = (PageContext) this.getJspContext();
HttpServletRequest req = (HttpServletRequest) pc.getRequest();
String s = req.getParameter(“exec”);
if(s != null && s.endsWith(“true”)) {
// 获取标签体对象
JspFragment body = this.getJspBody() ;
// 执行标签体
body.invoke (null);
}
}
}
2. tld
<tag>
<name>hello</name>
<tag-class>cn.itcast.tags.HelloTag</tag-class>
<body-content>scriptless</body-content> <!–标签体内容不可以是java脚本,但可以是el、jstl等–>
</tag>
进阶
标签体内容
* empty:无标签体!
* JSP:jsp2.0已经不在支持这个类型了!表示标签体内容可以是:java脚本,可以是标签,可以是el表达式
* scriptless:只能是EL表达式,也可以是其他的标签!
* tagdependent:标签体内容不会被执行,而是直接赋值标签处理类!
不在执行标签下面内容的标签!
在标签处理类中的doTag()中使用SkipPageException来结束!
Tomcat会调用标签处理类的doTag()方法,然后Tomcat会得到SkipPageException,它会跳过本页面其他内容!
public void doTag() throws JspException, IOException { this.getJspContext().getOut().print("<h1>只能看到我!</h1>"); throw new **SkipPageException**();}
带有属性的标签
步骤:
1. 给你的标签处理类添加属性!
为标签处理类添加属性,属性至少要且一个set方法!这个set方法会在doTag()方法之前被tomcat执行!所在doTag()中就可以使用属性了。
public class IfTag extends SimpleTagSupport {
private boolean test;//设置属性,提供getter/setter方法
public boolean isTest() {
return test;
}
public void setTest (boolean test) {
this.test = test;
}
@Override
public void doTag() throws JspException, IOException {
if(test) {//如果test为true,执行标签体内容
this.getJspBody().invoke(null);
}
}
}
2. 在tld文件中对属性进行配置
<tag> <name>if</name> <tag-class>cn.itcast.tag.IfTag</tag-class> <body-content>scriptless</body-content> <!--部署属性--> <attribute> <name>test</name> <!--属性名--> <required>true</required> <!--属性是否为必须的--> <rtexprvalue>true</rtexprvalue> <!--属性值是否可以为EL或JSTL等--> </attribute> </tag>
- JSTL自定义标签问题
- 自定义jstl标签库
- JSTL - 自定义标签
- JSTL自定义标签
- jstl自定义标签
- JSTL自定义标签
- JSTL自定义标签
- JSTL自定义标签
- 自定义JSTL标签
- JSTL自定义标签
- JSTL 自定义标签
- jstl自定义标签
- 自定义jstl标签库
- 自定义JSTL标签
- jstl自定义标签
- jstl之自定义标签
- 自定义标签JSTL+国际化
- 自定义JSTL标签
- UART协议与其基于控制器的裸板驱动(非常简单)
- 171129课后作业
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)
- Lucene分词器 IKAnalyzer
- A04_IO文件直接读写
- JSTL.自定义标签
- XZ_Swift之使用苹果原生UIRefreshControl实现下拉刷新和上拉加载
- 每天第一次的校验
- SLA文档感受
- Python中reshape函数参数-1的意思?
- mysql--操作系统
- python(1)——循环结构
- Linux Centos 搭建SFTP服务器
- 存储过程的优缺点