jsp自定义标签

今天和大家分享jsp自定义标签的实现,首先介绍自定义标签的整体实现流程,然后介绍几种常见的标签的实现方法,根据这些内容可以影射出各种各样的标签。

一、jsp自定义标签的实现过程

自定义标签的实现分三步,第一步就是编写tld文件;第二步,编写java文件;第三步,在jsp页面使用自定义标签。下面分步骤实现(以显示本地IP地址标签为例):
1、第一步:编写login.tld文件 
不会写tld文件没关系,在项目下有jstl标签的tld文件,直接copy一个过来修改就行了,文件路径:
(https://img-blog.csdn.net/20170428102439876?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2F0X3Bw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)   

这里我们修改文件中的内容即可:

<?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.0</tlib-version>
    <!-- 标签库的前缀 -->
    <short-name>cat</short-name>
    <!-- 标签库的唯一标识 -->
    <uri>com.catpp.tag</uri>

    <!-- 显示本地IP地址的标签 -->
    <tag>
        <!-- 标签名称 -->
        <name>showIp</name>
        <!-- 标签处理器类的全名 -->
        <tag-class>com.catpp.tag.Tag</tag-class>
        <!-- 输出标签体内容格式 -->
        <body-content>scriptless</body-content>
    </tag>

    <!-- 后面的内容是其他的标签,暂时不用管,一会讲别的实例标签的时候我会往里添加内容的 -->
    <!-- if标签 -->
    <tag>
        <name>if</name>
        <tag-class>com.catpp.tag.IfTag</tag-class>
        <body-content>scriptless</body-content>
        <!-- 属性配置 -->
        <attribute>
            <!-- 属性名称 -->
            <name>test</name>
            <!-- 是否必填 -->
            <required>true</required>
            <!-- 是否支持EL表达式 -->
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>

    <!-- choose标签 -->
    <tag>
        <name>choose</name>
        <tag-class>com.catpp.ChooseTag<tag-class>
        <body-content>scriptless<body-content>
    </tag>
    <!-- when标签 -->
    <tag>
        <name>when</name>
        <tag-class>com.catpp.WhenTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>test</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    <!-- otherwise标签 -->
    <tag>
        <name>otherwise</name>
        <tag-class>com.catpp.OhterwiseTag</tag-class>
        <body-content>scriptless</body-content>
    </tag>

    <!-- forEach标签 -->
    <tag>
        <name>forEach</name>
        <tag-class>com.catpp.ForEachTag</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>items<name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>var</name>
            <required>true</required>
            <rtexprvalue>false<rtexprvalue>
        </attribute>
    </tag>
</tablib>

2、实现标签处理器类Tag.java

标签处理器类一定要继承SimpleTagSupport类,重写doTage()方法

public class Tag extends SimpleTagSupport{
    @Override
    public void doTag() throws JspException, IOException{
        /**
         * 输出本地IP地址
         */
        PageContext pageContext = (PageContext)this.getJspContext();
        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
        String id = request.getRemoteHost();
        JspWriter out = pageContext.getOut();
        out.write("使用自定义标签输出本地IP地址:"+ip);
    }
}

3、在jsp页面使用自定义标签

<%@ page language="java" pageEncoding="utf-8"%>
<!-- 引入自定义标签 -->
<%@taglib uri="com.catpp.tag" prefix="cat"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
      <title>显示本地IP地址</title>  
  </head>

  <body>
      <!-- 使用自定义标签显示本地IP地址 --> 
      <cat:showIp></cat:showIp>
      <hr>
      <!-- 自定义if标签 -->
      <cat:if test="2>1">自定义标签</cat:if>
      <hr>
      <!-- 自定义choose标签、when标签和otherwise标签 -->
      <cat:choose>
          <cat:when test="1>2">显示when标签的内容</when>
          <cat:otherwise>显示otherwise标签的内容</otherwise>
      </cat:choose>
      <hr>
      <!-- 自定义forEach标签 -->
      <cat:forEach items="${dataList}" var="data">
          自定义forEach标签内容:${data.value}
      </cat:forEach>
  </body>
</html>

jsp自定义标签的实现步骤就是这样的,下面介绍几种常见的标签的实现方法,以c标签中的if、choose、forEach标签等为例。
二、常见标签的实现

1、if标签

因为实现的不同之处主要在于标签处理器的不同,所以接下来的关于jsp和tld的内容直接在上面的jsp和tld文件中。
public class IfTag extends SimpleTagSupport{
    private boolean test;
    //属性一定要配置setter方法
    public void setTest(boolean test){
        this.test = test;
    }
    @Override
    public void doTag() throws JspException, IOException{
        //根据test的返回值判断是否输出内容
        if(test){
            //jspContext、jspBody(JspFragment:标签体内容)等都是在SimpleTagSupport类中定义好的,所以不用声明,直接用this调用就可以
            //invoke()方法传入null,把标签体内容输出到浏览器
            //如果传入其他Writer对象也可以
            this.getJspBody().invoke(null);
        }
    }
}

2、choose标签

choose标签要和when、otherwise标签一起使用,先说一下总体的逻辑:
choose里的内容是一定要显示的,然后when标签内有一个test属性,当test返回的值为true时,显示when标签内的内容,当test返回的值为false时,显示otherwise标签内的内容,那么otherwise标签如何判断test的返回值呢,这里用到了父标签的内容,choose标签作为when和otherwise标签的父标签
//Choose标签的处理器类:
public class ChooseTag extends SimpleTagSupport{
    //这个不是属性,只是一个临时变量,用来接收when标签的test属性的返回值,然后交给otherwise标签
    private boolean flag;
    public boolean isFlag(){
        retrun flag;
    }
    public void setFlag(boolean flag){
        this.flag = flag;
    }

    @Override
    public void doTag() throws JspException, IOException{
        //显示标签体的内容
        this.getJspBody().invoke(null);
    }
}
//when标签的处理器类:
public class WhenTag extends SimpleTagSupport{
    private boolean test;
    public void setTest(boolean test){
        this.test = test;
    }

    @Override
    public void doTag() throws JspException, IOException{
        if(test){
            this.getJspBody.invoke(null);
        }
        //为父标签的flag属性赋值
        ChooseTag parent = (ChooseTag)this.getParent();
        parent.setFlag(test);
    }
}
//otherwise标签的处理器类:
public class OtherwiseTag extends SimpleTagSupport{
    @Override
    public void doTag() throws JspException, IOException{
        ChooseTag parent = (ChooseTag)this.getParent();
        boolean test = parent.isFlag();
        //如果父标签的flag属性为false,显示otherwise标签体内容
        if(!test){
            this.getJspBody().invoke(null);
        }
    }
}

3、forEach标签

public class ForEachTag extends SimpleTagSupport{
    private Object items;
    private String var;
    public void setItems(Object items){
        this.items = items;
    }
    public void setVar(String var){
        this.var = var;
    }

    public void doTag() throws JspException, IOException{
        PageContext pageContext = (PageContext)this.getJspContext();
        Collection coll = null;
        if(items instanceof List){
            coll = (List)items;
        }
        if(items instanceof Map){
            Map map = (Map)items;
            coll = map.entrySet();
        }
        for(Object obj : items){
            pageContext.setAttribute(var,object);
            this.getJspBody().invoke(null);
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值