如何创建JSTL自定义标签?

2026-06-10 05:271阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1916个文字,预计阅读时间需要8分钟。

如何创建JSTL自定义标签?

/+ 作者:老和尚 + CSDN认证讲师 + 51CTO高级讲师 + 腾讯课堂认证讲师 + 网易云课堂认证讲师 + 华为开发者学院认证讲师 + 爱奇艺千万人名讲师 + 在这里给家长们分享技术、知识+


/*
*作者:呆萌老师
*☑csdn认证讲师
*☑51cto高级讲师
*☑腾讯课堂认证讲师
*☑网易云课堂认证讲师
*☑华为开发者学堂认证讲师
*☑爱奇艺千人名师计划成员
*在这里给大家分享技术、知识和生活
*各种干货,记得关注哦!
*/

1 自定义标签概述

1.1 自定义标签的步骤

其实我们在JSP页面中使用标签就等于调用某个对象的某个方法一样,例如:<c:if test=””>,这就是在调用对象的方法一样。自定义标签其实就是自定义类一样!

  • 定义标签处理类:必须是Tag或SimpleTag的实现类;
  • 编写标签库描述符文件(TLD);
  • SimpleTag接口是JSP2.0中新给出的接口,用来简化自定义标签,所以现在我们基本上都是使用SimpleTag。

    Tag是老的,传统的自定义标签时使用的接口,现在不建议使用它了。

    1.2 SimpleTag接口介绍

    SimpleTag接口内容如下:

  • void doTag():标签执行方法;
  • JspTag getParent():获取父标签;
  • void setParent(JspTag parent):设置父标签
  • void setJspContext(JspContext context):设置PageContext
  • void setJspBody(JspFragment jspBody):设置标签体对象;
  • 请记住,万物皆对象!在JSP页面中的标签也是对象!你可以通过查看JSP的“真身”清楚的知道,所有标签都会变成对象的方法调用。标签对应的类我们称之为“标签处理类”!

    标签的生命周期:

  • 当容器(Tomcat)第一次执行到某个标签时,会创建标签处理类的实例;
  • 然后调用setJspContext(JspContext)方法,把当前JSP页面的pageContext对象传递给这个方法;
  • 如果当前标签有父标签,那么使用父标签的标签处理类对象调用setParent(JspTag)方法;
  • 如果标签有标签体,那么把标签体转换成JspFragment对象,然后调用setJspBody()方法;
  • 每次执行标签时,都调用doTag()方法,它是标签处理方法。
  • HelloTag.java

    public class HelloTag implements SimpleTag {

    private JspTag parent;

    private PageContext pageContext;

    private JspFragment jspBody;



    public void doTag() throws JspException, IOException {

    pageContext.getOut().print("Hello Tag!!!");

    }

    public void setParent(JspTag parent) {

    this.parent = parent;

    }

    public JspTag getParent() {

    return this.parent;

    }

    public void setJspContext(JspContext pc) {

    this.pageContext = (PageContext) pc;

    }

    public void setJspBody(JspFragment jspBody) {

    this.jspBody = jspBody;

    }

    }

    1.3 标签库描述文件(TLD)

    标签库描述文件是用来描述当前标签库中的标签的!标签库描述文件的扩展名为tld,你可以把它放到WEB-INF下,这样就不会被客户端直接访问到了。

    hello.tld

    <?xml version="1.0" encoding="UTF-8"?>

    <taglib version="2.0" xmlns="java.sun.com/xml/ns/j2ee"

    xmlns:xml="www.w3.org/XML/1998/namespace"

    xmlns:xsi="www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="java.sun.com/xml/ns/j2ee

    java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd ">



    <tlib-version>1.0</tlib-version>

    <short-name>eduask</short-name>

    <uri>www.eduask.cn/tags</uri>

    <tag>

    <name>hello</name>

    <tag-class>cn.eduask.tag.HelloTag</tag-class>

    <body-content>empty</body-content>

    </tag>

    </taglib>

    1.4 使用标签

    在页面中使用标签分为两步:

  • 使用taglib导入标签库;
  • 使用标签;
  • <%@ taglib prefix="eduask" uri="/WEB-INF/hello.tld"

    2 自定义标签进阶

    2.1 继承SimpleTagSupport

      继承SimpleTagSuppport要比实现SimpleTag接口方便太多了,现在你只需要重写doTag()方法,其他方法都已经被SimpleTagSuppport完成了。

    public class HelloTag extends SimpleTagSupport {

    public void doTag() throws JspException, IOException {

    this.getJspContext().getOut().write("<p>Hello SimpleTag!</p>");

    }

    }

    2.2 有标签体的标签

    如何创建JSTL自定义标签?

    我们先来看看标签体内容的可选值:

    <body-content>元素的可选值有:

  • empty:无标签体。
  • JSP:传统标签支持它,SimpleTag已经不再支持使用<body-content>JSP</body-content>。标签体内容可以是任何东西:EL、JSTL、<%=%>、<%%>,以及html;
  • scriptless:标签体内容不能是Java脚本,但可以是EL、JSTL等。在SimpleTag中,如果需要有标签体,那么就使用该选项;
  • tagdependent:标签体内容不做运算,由标签处理类自行处理,无论标签体内容是EL、JSP、JSTL,都不会做运算。这个选项几乎没有人会使用!
  • 自定义有标签体的标签需要:

  • 获取标签体对象:JspFragment jspBody = getJspBody();;
  • 把标签体内容输出到页面:jspBody.invoke(null);
  • tld中指定标签内容类型:scriptless。
  • publicclass HelloTag extends SimpleTagSupport {

    publicvoid doTag() throws JspException, IOException {

    PageContext pc = (PageContext) this.getJspContext();

    HttpServletRequest req = (HttpServletRequest) pc.getRequest();

    String s = req.getParameter("exec");

    if(s != null && s.endsWith("true")) {

    JspFragment body = this.getJspBody();

    body.invoke(null);

    }

    }

    }

    <tag>

    <name>hello</name>

    <tag-class>cn.eduask.tags.HelloTag</tag-class>

    <body-content>scriptless</body-content>

    </tag>

    <eduask:hello>

    <h1>哈哈哈~</h1>

    </eduask:hello>

    2.3 不执行标签下面的页面内容

      如果希望在执行了自定义标签后,不再执行JSP页面下面的东西,那么就需要在doTag()方法中使用SkipPageException。

    publicclass SkipTag extends SimpleTagSupport {

    publicvoid doTag() throws JspException, IOException {

    this.getJspContext().getOut().print("<h1>只能看到我!</h1>");

    thrownew SkipPageException();

    }

    }

    <tag>

    <name>skip</name>

    <tag-class>cn.eduask.tags.SkipTag</tag-class>

    <body-content>empty</body-content>

    </tag>

    <eduask:skip/>

    <h1>看不见我!</h1>

    2.4 带有属性的标签

      一般标签都会带有属性,例如<c:if test=””>,其中test就是一个boolean类型的属性。完成带有属性的标签需要:

  • 在处理类中给出JavaBean属性(提供get/set方法);
  • 在TLD中部属相关属性。
  • publicclass IfTag extends SimpleTagSupport {

    privatebooleantest;

    publicboolean isTest() {

    returntest;

    }

    publicvoid setTest(boolean test) {

    this.test = test;

    }

    @Override

    publicvoid doTag() throws JspException, IOException {

    if(test) {

    this.getJspBody().invoke(null);

    }

    }

    }

    <tag>

    <name>if</name>

    <tag-class>cn.eduask.tag.IfTag</tag-class>

    <body-content>scriptless</body-content>

    <attribute>

    <name>test</name>

    <required>true</required>

    <rtexprvalue>true</rtexprvalue>

    </attribute>

    </tag>

    <%

    pageContext.setAttribute("one", true);

    pageContext.setAttribute("two", false);

    %>

    <it:iftest="${one }">xixi</it:if>

    <it:iftest="${two }">haha</it:if>

    <it:iftest="true">hehe</it:if>

    更多了解

    ​​edu.51cto.com/course/20519.html​​

    本文共计1916个文字,预计阅读时间需要8分钟。

    如何创建JSTL自定义标签?

    /+ 作者:老和尚 + CSDN认证讲师 + 51CTO高级讲师 + 腾讯课堂认证讲师 + 网易云课堂认证讲师 + 华为开发者学院认证讲师 + 爱奇艺千万人名讲师 + 在这里给家长们分享技术、知识+


    /*
    *作者:呆萌老师
    *☑csdn认证讲师
    *☑51cto高级讲师
    *☑腾讯课堂认证讲师
    *☑网易云课堂认证讲师
    *☑华为开发者学堂认证讲师
    *☑爱奇艺千人名师计划成员
    *在这里给大家分享技术、知识和生活
    *各种干货,记得关注哦!
    */

    1 自定义标签概述

    1.1 自定义标签的步骤

    其实我们在JSP页面中使用标签就等于调用某个对象的某个方法一样,例如:<c:if test=””>,这就是在调用对象的方法一样。自定义标签其实就是自定义类一样!

  • 定义标签处理类:必须是Tag或SimpleTag的实现类;
  • 编写标签库描述符文件(TLD);
  • SimpleTag接口是JSP2.0中新给出的接口,用来简化自定义标签,所以现在我们基本上都是使用SimpleTag。

    Tag是老的,传统的自定义标签时使用的接口,现在不建议使用它了。

    1.2 SimpleTag接口介绍

    SimpleTag接口内容如下:

  • void doTag():标签执行方法;
  • JspTag getParent():获取父标签;
  • void setParent(JspTag parent):设置父标签
  • void setJspContext(JspContext context):设置PageContext
  • void setJspBody(JspFragment jspBody):设置标签体对象;
  • 请记住,万物皆对象!在JSP页面中的标签也是对象!你可以通过查看JSP的“真身”清楚的知道,所有标签都会变成对象的方法调用。标签对应的类我们称之为“标签处理类”!

    标签的生命周期:

  • 当容器(Tomcat)第一次执行到某个标签时,会创建标签处理类的实例;
  • 然后调用setJspContext(JspContext)方法,把当前JSP页面的pageContext对象传递给这个方法;
  • 如果当前标签有父标签,那么使用父标签的标签处理类对象调用setParent(JspTag)方法;
  • 如果标签有标签体,那么把标签体转换成JspFragment对象,然后调用setJspBody()方法;
  • 每次执行标签时,都调用doTag()方法,它是标签处理方法。
  • HelloTag.java

    public class HelloTag implements SimpleTag {

    private JspTag parent;

    private PageContext pageContext;

    private JspFragment jspBody;



    public void doTag() throws JspException, IOException {

    pageContext.getOut().print("Hello Tag!!!");

    }

    public void setParent(JspTag parent) {

    this.parent = parent;

    }

    public JspTag getParent() {

    return this.parent;

    }

    public void setJspContext(JspContext pc) {

    this.pageContext = (PageContext) pc;

    }

    public void setJspBody(JspFragment jspBody) {

    this.jspBody = jspBody;

    }

    }

    1.3 标签库描述文件(TLD)

    标签库描述文件是用来描述当前标签库中的标签的!标签库描述文件的扩展名为tld,你可以把它放到WEB-INF下,这样就不会被客户端直接访问到了。

    hello.tld

    <?xml version="1.0" encoding="UTF-8"?>

    <taglib version="2.0" xmlns="java.sun.com/xml/ns/j2ee"

    xmlns:xml="www.w3.org/XML/1998/namespace"

    xmlns:xsi="www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="java.sun.com/xml/ns/j2ee

    java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd ">



    <tlib-version>1.0</tlib-version>

    <short-name>eduask</short-name>

    <uri>www.eduask.cn/tags</uri>

    <tag>

    <name>hello</name>

    <tag-class>cn.eduask.tag.HelloTag</tag-class>

    <body-content>empty</body-content>

    </tag>

    </taglib>

    1.4 使用标签

    在页面中使用标签分为两步:

  • 使用taglib导入标签库;
  • 使用标签;
  • <%@ taglib prefix="eduask" uri="/WEB-INF/hello.tld"

    2 自定义标签进阶

    2.1 继承SimpleTagSupport

      继承SimpleTagSuppport要比实现SimpleTag接口方便太多了,现在你只需要重写doTag()方法,其他方法都已经被SimpleTagSuppport完成了。

    public class HelloTag extends SimpleTagSupport {

    public void doTag() throws JspException, IOException {

    this.getJspContext().getOut().write("<p>Hello SimpleTag!</p>");

    }

    }

    2.2 有标签体的标签

    如何创建JSTL自定义标签?

    我们先来看看标签体内容的可选值:

    <body-content>元素的可选值有:

  • empty:无标签体。
  • JSP:传统标签支持它,SimpleTag已经不再支持使用<body-content>JSP</body-content>。标签体内容可以是任何东西:EL、JSTL、<%=%>、<%%>,以及html;
  • scriptless:标签体内容不能是Java脚本,但可以是EL、JSTL等。在SimpleTag中,如果需要有标签体,那么就使用该选项;
  • tagdependent:标签体内容不做运算,由标签处理类自行处理,无论标签体内容是EL、JSP、JSTL,都不会做运算。这个选项几乎没有人会使用!
  • 自定义有标签体的标签需要:

  • 获取标签体对象:JspFragment jspBody = getJspBody();;
  • 把标签体内容输出到页面:jspBody.invoke(null);
  • tld中指定标签内容类型:scriptless。
  • publicclass HelloTag extends SimpleTagSupport {

    publicvoid doTag() throws JspException, IOException {

    PageContext pc = (PageContext) this.getJspContext();

    HttpServletRequest req = (HttpServletRequest) pc.getRequest();

    String s = req.getParameter("exec");

    if(s != null && s.endsWith("true")) {

    JspFragment body = this.getJspBody();

    body.invoke(null);

    }

    }

    }

    <tag>

    <name>hello</name>

    <tag-class>cn.eduask.tags.HelloTag</tag-class>

    <body-content>scriptless</body-content>

    </tag>

    <eduask:hello>

    <h1>哈哈哈~</h1>

    </eduask:hello>

    2.3 不执行标签下面的页面内容

      如果希望在执行了自定义标签后,不再执行JSP页面下面的东西,那么就需要在doTag()方法中使用SkipPageException。

    publicclass SkipTag extends SimpleTagSupport {

    publicvoid doTag() throws JspException, IOException {

    this.getJspContext().getOut().print("<h1>只能看到我!</h1>");

    thrownew SkipPageException();

    }

    }

    <tag>

    <name>skip</name>

    <tag-class>cn.eduask.tags.SkipTag</tag-class>

    <body-content>empty</body-content>

    </tag>

    <eduask:skip/>

    <h1>看不见我!</h1>

    2.4 带有属性的标签

      一般标签都会带有属性,例如<c:if test=””>,其中test就是一个boolean类型的属性。完成带有属性的标签需要:

  • 在处理类中给出JavaBean属性(提供get/set方法);
  • 在TLD中部属相关属性。
  • publicclass IfTag extends SimpleTagSupport {

    privatebooleantest;

    publicboolean isTest() {

    returntest;

    }

    publicvoid setTest(boolean test) {

    this.test = test;

    }

    @Override

    publicvoid doTag() throws JspException, IOException {

    if(test) {

    this.getJspBody().invoke(null);

    }

    }

    }

    <tag>

    <name>if</name>

    <tag-class>cn.eduask.tag.IfTag</tag-class>

    <body-content>scriptless</body-content>

    <attribute>

    <name>test</name>

    <required>true</required>

    <rtexprvalue>true</rtexprvalue>

    </attribute>

    </tag>

    <%

    pageContext.setAttribute("one", true);

    pageContext.setAttribute("two", false);

    %>

    <it:iftest="${one }">xixi</it:if>

    <it:iftest="${two }">haha</it:if>

    <it:iftest="true">hehe</it:if>

    更多了解

    ​​edu.51cto.com/course/20519.html​​