第一步.编写标签处理类
TagSupport 三个重要的方法:
1)public int doStartTag() throws JspException
标签开始执行此方法。此方法返回值有两个
Tag.SKIP_BODY:表示...之间的内容被忽略
Tag.EVAL_BODY_INCLUDE:表示标签之间的内容被正常执行
2)public int doEndTag() throws JspException
标签结束执行此方法。此方法返回值也有两个
Tag.SKIP_PAGE:表示立即停止执行网页,网页上未处理的静态内容和JSP均被忽略
Tag.EVAL_PAGE:表示按照正常的流程执行JSP网页
3)public void setPageContext(PageContext pageContext)
得到PageContext上下文对象
hello Silver实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package top.code666.utils;
import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.TagSupport;
public class MyTag extends TagSupport{
PageContext pageContext; private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override public int doStartTag() throws JspException { JspWriter out = pageContext.getOut(); try { out.println("hello"); } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); } @Override public int doEndTag() throws JspException { return super.doEndTag(); } @Override public void setPageContext(PageContext pageContext) { this.pageContext = pageContext; } }
|
从名字可知,继承这个类使用起来是非常Simple的,它只要重新一个方法即可完成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package top.code666.utils;
import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.SimpleTagSupport;
public class HelloTag extends SimpleTagSupport{ @Override public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); try { out.write("hello"); } catch (IOException e) { e.printStackTrace(); } } }
|
第二步.tld文件详解
此文件一般放在WEB-INF文件下
1 2 3 4 5
| <?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> </taglib>
|
此tld文件的一些描述
1.description标签:从名字可知,这个标签只是用来描述tag或者taglib的,实际意义并不大。但是此标签要写在首位,不能写其它标签属性的后面,否则编辑器(myeclipse10.0)会报错。
<description>my tag</description>
2.short-name标签: jsp自定义标签的缩写
<short-name>silver</short-name>
3.uri标签:定义该标签的URI.
<uri>http://code666.top/taglib/util</uri>
tag标签重要属性的详解
这个标签是用来定义我们一些要操作的标签方法的
tag标签一些重要的属性汇总表
属性 |
描述 |
description |
描述、此方法要写在最前面 |
name | 定义属性或当前tag的名称。每个标签的是属性名称必须是唯一的。
tag-class | 定义当前tag所在类的全限定名
attribute | 当前标签的一些属性。例如c:if 、c:forEach中if && forEach属性
required | 指定属性是否是必须的或者可选的,如果设置为false为可选
rtexprvalue | rtexprvalue的全称是 Run-time Expression Value,表示是否可以使用jsp表达式
body-content | body-content有四个属性值:tagdependent、JSP、empty、scriptless。具体解释见下
1 2 3 4 5 6 7 8 9 10 11
| body-content有四个属性值: tagdependent:标签体内容直接被写入BodyContent,由自定义标签类来进行处理,而不被JSP容器解释 <test:myList>select name,age from users</test:myList>
JSP:接受所有JSP语法,如定制的或内部的tag、scripts、静态HTML、脚本元素、JSP指令和动作 <my:test><%=request.getProtocol()%> // ②</my:test>
empty:空标记,即起始标记和结束标记之间没有内容。 <test:mytag /><test:mytag uname="Tom" /><test:mytag></test:mytag
scriptless:接受文本、EL和JSP动作。如上述②使用<body-content> scriptless </body-content>则报错
|
hello Silver实例(my.tld)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <tlib-version>1.1</tlib-version>
<short-name>silver</short-name> <uri>http://code666.top/taglib/util</uri> <tag> <name>hello</name> <tag-class>top.code666.utils.MyTag</tag-class> <body-content>empty</body-content> <attribute> <name>name</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> </taglib>
|
第三步.html界面插入自定义标签
1.先在jsp页面下引入taglib <%@taglib uri=”http://code666.top/taglib/util" prefix=”silver” %>
2.body标签下插入标签 <silver:hello name=”silver”>
3.ok,运行测试
1 2
| ====运行结果===== hello silver
|