JSP2.0自定义标签

---| SimpleTag 接口

定义了标签处理类的生命周期方法。doTag()

-----| SimpleTagSupport 类

全部实现了SimpleTag接口的方法,因此后面我们只需要继承并重写该类即可。

实现自己的if….else标签

目标:

 1    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 2 
 3    <c:choose>
 4 
 5      <c:when test="<%= 12>1 %>">
 6 
 7             大于
 8 
 9      </c:when>
10 
11      <c:otherwise>
12 
13             小于
14 
15      </c:otherwise>
16 
17    </c:choose>

分析:

1. ChooseTag.java,必须定义一个标记字段属性

 1 public class ChooseTag extends SimpleTagSupport {
 2     private boolean tag = true;
 3     public boolean isTag() {
 4         return tag;
 5     }
 6     public void setTag(boolean tag) {
 7         this.tag = tag;
 8     }
 9     // 遇到标签自动执行
10     public void doTag() throws JspException, IOException {
11         // 获取标签体对象
12         JspFragment body = this.getJspBody();
13         // 执行标签体
14         body.invoke(null);
15         super.doTag();
16     }
17 }

2. WhenTag.java

 1 public class WhenTag extends SimpleTagSupport {
 2     private boolean test;
 3     public boolean isTest() {
 4         return test;
 5     }
 6     public void setTest(boolean test) {
 7         this.test = test;
 8     }
 9     // 遇到标签自动执行
10     public void doTag() throws JspException, IOException {
11         // 获取父元素
12         ChooseTag choose = (ChooseTag)this.getParent();
13         // 获取父元素的标记变量值
14         boolean parent = choose.isTag();
15         // 判断
16         if( parent && this.isTest() ){
17             // 设置父元素的标记变量
18             choose.setTag(false);
19             // 执行标签体
20             JspFragment body = this.getJspBody();
21             body.invoke(null);
22         }
23         super.doTag();
24     }
25 }

3. Otherwise.java

 1 public class OtherwiseTag extends SimpleTagSupport {
 2     
 3     // 遇到标签自动执行
 4     public void doTag() throws JspException, IOException {
 5         // 获取父元素
 6         ChooseTag choose = (ChooseTag)this.getParent();
 7         // 获取父元素的标记变量值
 8         boolean parent = choose.isTag();
 9         // 判断
10         if(parent){
11             // 执行标签体
12             JspFragment body = this.getJspBody();
13             body.invoke(null);
14         }
15         super.doTag();
16     }
17 }

4. 描述文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <taglib 
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
 6     version="2.1">
 7   <!-- 2. 编写标签库描述文件 -->  
 8   <tlib-version>1.0</tlib-version>
 9   <short-name>jnb</short-name>
10    <tag>  
11     <name>choose</name>
12     <tag-class>cn.itcast.tags.ChooseTag</tag-class>
13     <body-content>scriptless</body-content>     JSP2.0方式
14   </tag>
15   <tag>  
16     <name>when</name>
17     <tag-class>cn.itcast.tags.WhenTag</tag-class>
18     <body-content>scriptless</body-content>
19     <attribute>
20         <name>test</name>
21         <required>true</required>
22         <rtexprvalue>true</rtexprvalue>
23     </attribute>
24   </tag>
25    
26    <tag>  
27     <name>otherwise</name>
28     <tag-class>cn.itcast.tags.OtherwiseTag</tag-class>
29     <body-content>scriptless</body-content>
30   </tag>
31 </taglib>    

5. 引入和使用

1 <%@taglib uri="/WEB-INF/ifelse.tld" prefix="jnb"%>
2      <jnb:choose>
3         <jnb:when test="<%= 1>2 %>">
4                 小于
5         </jnb:when>
6         <jnb:otherwise>
7                   大于
8         </jnb:otherwise>
9      </jnb:choose>

打包自定义标签库

  1. 建立一个taglibs文件夹
  2. 将所有的标签处理类对应的class文件连同包拷贝到1中的目录中
  3. 在1中的文件夹中建立一个META-INF文件夹
  4. 将tld文件拷贝到META-INF目录
  5. 编辑tld文件引入uri元素

   <uri>http://www.jnb.com</uri>             ->提供引入的url路径

如果提早在tld文件中写入uri 然后在*.jsp中引入www.jnb.com会报错,uri 用于打包的时候用

      6.使用jar命令进行打包

   D:mytaglibs>jar cvf jnb.jar *

原文地址:https://www.cnblogs.com/friends-wf/p/3736670.html