JSP2.0自定义标签

2020-12-13 05:25

阅读:346

标签:style   class   blog   c   code   java   

---| SimpleTag 接口

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

-----| SimpleTagSupport 类

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

实现自己的if….else标签

目标:

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

分析:

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

soscw.com,搜素材
 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 }
soscw.com,搜素材

2. WhenTag.java

soscw.com,搜素材
 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 }
soscw.com,搜素材

3. Otherwise.java

soscw.com,搜素材
 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 }
soscw.com,搜素材

4. 描述文件

soscw.com,搜素材
 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     
 8   tlib-version>1.0tlib-version>
 9   short-name>jnbshort-name>
10    tag>  
11     name>choosename>
12     tag-class>cn.itcast.tags.ChooseTagtag-class>
13     body-content>scriptlessbody-content>    ? JSP2.0方式
14   tag>
15   tag>  
16     name>whenname>
17     tag-class>cn.itcast.tags.WhenTagtag-class>
18     body-content>scriptlessbody-content>
19     attribute>
20         name>testname>
21         required>truerequired>
22         rtexprvalue>truertexprvalue>
23     attribute>
24   tag>
25    
26    tag>  
27     name>otherwisename>
28     tag-class>cn.itcast.tags.OtherwiseTagtag-class>
29     body-content>scriptlessbody-content>
30   tag>
31 taglib>    
soscw.com,搜素材

 

5. 引入和使用

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

打包自定义标签库

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

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

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

      6.使用jar命令进行打包

   D:\mytaglibs>jar cvf jnb.jar *

JSP2.0自定义标签,搜素材,soscw.com

JSP2.0自定义标签

标签:style   class   blog   c   code   java   

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


评论


亲,登录后才可以留言!