欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP自定义标签案例分析

程序员文章站 2023-10-27 15:26:28
本文为大家介绍了jsp自定义标签的案例,供大家参考,具体内容如下 案例一:实现一个基本防盗链标签 1. 标签处理类 public class myrefere...

本文为大家介绍了jsp自定义标签的案例,供大家参考,具体内容如下

案例一:实现一个基本防盗链标签

1. 标签处理类

public class myreferer extends bodytagsupport {
  private string site;
  private string back;
  public string getsite() {
    return site;
  }
  public void setsite(string site) {
    this.site = site;
  }
  public string getback() {
    return back;
  }
  public void setback(string back) {
    this.back = back;
  }
  public int doendtag() throws jspexception {
    // 获取jsp上下文环境对象
    pagecontext pagecontext = this.pagecontext;
    // 获取到request对象
    httpservletrequest request = (httpservletrequest)pagecontext.getrequest();
    // 判断
    string header = request.getheader("referer");
    if(header != null && header.startswith(getsite())){
      // 执行后续的页面
      return tag.eval_page;
    }else{
      // 页面的重定向
      httpservletresponse response = (httpservletresponse)pagecontext.getresponse();
      try {
        response.sendredirect(getback());
      } catch (ioexception e) {
        e.printstacktrace();
      }
      // 不执行
      return tag.skip_page;
    }
  }
}

2. 描述文件

<?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">
 <!-- 2. 编写标签库描述文件 --> 
 <tlib-version>1.0</tlib-version>
 <short-name>jnb</short-name>
  <tag> 
  <name>referer</name>
  <tag-class>cn.itcast.custom.myreferer</tag-class>
  <body-content>empty</body-content>
   <attribute>
    <name>site</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
   <attribute>
    <name>back</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
</taglib> 

3. 引入和使用

<%@taglib uri="/web-inf/referer.tld" prefix="my"%>
  <my:referer site=http://localhost:8080/day11/list.jsp
 back="/day11/list.jsp"/>

jsp2.0自定义标签

---| simpletag 接口

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

-----| simpletagsupport 类

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

案例二:实现自己的if….else标签

目标:

 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  <c:choose>
   <c:when test="<%= 12>1 %>">
      大于
   </c:when>
   <c:otherwise>
      小于
   </c:otherwise>
  </c:choose>

分析:

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

public class choosetag extends simpletagsupport {
  private boolean tag = true;
  public boolean istag() {
    return tag;
  }
  public void settag(boolean tag) {
    this.tag = tag;
  }
  // 遇到标签自动执行
  public void dotag() throws jspexception, ioexception {
    // 获取标签体对象
    jspfragment body = this.getjspbody();
    // 执行标签体
    body.invoke(null);
    super.dotag();
  }
}

2. whentag.java

public class whentag extends simpletagsupport {
  private boolean test;
  public boolean istest() {
    return test;
  }
  public void settest(boolean test) {
    this.test = test;
  }
  // 遇到标签自动执行
  public void dotag() throws jspexception, ioexception {
    // 获取父元素
    choosetag choose = (choosetag)this.getparent();
    // 获取父元素的标记变量值
    boolean parent = choose.istag();
    // 判断
    if( parent && this.istest() ){
      // 执行标签体
      jspfragment body = this.getjspbody();
      body.invoke(null);
    }
    super.dotag();
  }
}

3. otherwise.java

public class otherwisetag extends simpletagsupport {
  
  // 遇到标签自动执行
  public void dotag() throws jspexception, ioexception {
    // 获取父元素
    choosetag choose = (choosetag)this.getparent();
    // 获取父元素的标记变量值
    boolean parent = choose.istag();
    // 判断
    if(parent){
      // 执行标签体
      jspfragment body = this.getjspbody();
      body.invoke(null);
    }
    super.dotag();
  }
}

4. 描述文件

<?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">
 <!-- 2. 编写标签库描述文件 --> 
 <tlib-version>1.0</tlib-version>
 <short-name>jnb</short-name>
  <tag> 
  <name>choose</name>
  <tag-class>cn.itcast.tags.choosetag</tag-class>
  <body-content>scriptless</body-content>   jsp2.0方式
 </tag>
 <tag> 
  <name>when</name>
  <tag-class>cn.itcast.tags.whentag</tag-class>
  <body-content>scriptless</body-content>
  <attribute>
    <name>test</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
  
  <tag> 
  <name>otherwise</name>
  <tag-class>cn.itcast.tags.otherwisetag</tag-class>
  <body-content>scriptless</body-content>
 </tag>
</taglib>

5. 引入和使用

<%@taglib uri="/web-inf/ifelse.tld" prefix="jnb"%>
   <jnb:choose>
    <jnb:when test="<%= 1>2 %>">
        小于
    </jnb:when>
    <jnb:otherwise>
         大于
    </jnb:otherwise>
   </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路径

6.   使用jar命令进行打包:d:\mytaglibs>jar cvf jnb.jar *

总结

主要掌握如何使用jsp2.0进行自定义标签的开发和打包。

1.   建立一个taglibs文件夹

2.   将所有的标签处理类对应的class文件连同包拷贝到1中的目录中

3.   在1中的文件夹中建立一个meta-inf文件夹

4.   将tld文件拷贝到meta-inf目录

5.   编辑tld文件引入uri元素:<uri>http://www.jnb.com</uri>     à提供引入的url路径

6.   使用jar命令进行打包:d:\mytaglibs>jar cvf jnb.jar *

总结

主要掌握如何使用jsp2.0进行自定义标签的开发和打包。