ToB企服应用市场:ToB评测及商务社交产业平台

标题: JSTL自定义标签 [打印本页]

作者: 写过一篇    时间: 2022-9-16 17:17
标题: JSTL自定义标签
​ 
  1. /*
  2. *作者:呆萌老师
  3. *☑csdn认证讲师
  4. *☑51cto高级讲师
  5. *☑腾讯课堂认证讲师
  6. *☑网易云课堂认证讲师
  7. *☑华为开发者学堂认证讲师
  8. *☑爱奇艺千人名师计划成员
  9. *在这里给大家分享技术、知识和生活
  10. *各种干货,记得关注哦!
  11. *vx:it_daimeng
  12. */
复制代码
  
1 自定义标签概述
1.1 自定义标签的步骤
其实我们在JSP页面中使用标签就等于调用某个对象的某个方法一样,例如:,这就是在调用对象的方法一样。自定义标签其实就是自定义类一样!
 
SimpleTag接口是JSP2.0中新给出的接口,用来简化自定义标签,所以现在我们基本上都是使用SimpleTag。
Tag是老的,传统的自定义标签时使用的接口,现在不建议使用它了。
 
1.2 SimpleTag接口介绍
SimpleTag接口内容如下:
 
请记住,万物皆对象!在JSP页面中的标签也是对象!你可以通过查看JSP的“真身”清楚的知道,所有标签都会变成对象的方法调用。标签对应的类我们称之为“标签处理类”!
标签的生命周期:
 
HelloTag.java
  1. public class HelloTag implements SimpleTag {
  2.     private JspTag parent;
  3.     private PageContext pageContext;
  4.     private JspFragment jspBody;
  5.    
  6.     public void doTag() throws JspException, IOException {
  7.         pageContext.getOut().print("Hello Tag!!!");
  8.     }
  9.     public void setParent(JspTag parent) {
  10.         this.parent = parent;
  11.     }
  12.     public JspTag getParent() {
  13.         return this.parent;
  14.     }
  15.     public void setJspContext(JspContext pc) {
  16.         this.pageContext = (PageContext) pc;
  17.     }
  18.     public void setJspBody(JspFragment jspBody) {
  19.         this.jspBody = jspBody;
  20.     }
  21. }
复制代码
  
1.3 标签库描述文件(TLD)
标签库描述文件是用来描述当前标签库中的标签的!标签库描述文件的扩展名为tld,你可以把它放到WEB-INF下,这样就不会被客户端直接访问到了。
hello.tld
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"
  3.     xmlns:xml="http://www.w3.org/XML/1998/namespace"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  6.                         http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd ">
  7.     <tlib-version>1.0</tlib-version>
  8.     <short-name>eduask</short-name>
  9.     <uri>http://www.eduask.cn/tags</uri>
  10.     <tag>
  11.         <name>hello</name>
  12.         <tag-class>cn.eduask.tag.HelloTag</tag-class>
  13.         <body-content>empty</body-content>
  14.     </tag>
  15. </taglib>
复制代码
  
1.4 使用标签
在页面中使用标签分为两步:
  1. <%@ taglib prefix="eduask" uri="/WEB-INF/hello.tld" %>
  2. ......
  3. <it:hello/>
复制代码
  
 
2 自定义标签进阶
2.1 继承SimpleTagSupport
  继承SimpleTagSuppport要比实现SimpleTag接口方便太多了,现在你只需要重写doTag()方法,其他方法都已经被SimpleTagSuppport完成了。
 
  1. public class HelloTag extends SimpleTagSupport {
  2.     public void doTag() throws JspException, IOException {
  3.         this.getJspContext().getOut().write("<p>Hello SimpleTag!</p>");
  4.     }
  5. }
复制代码
  
 
2.2 有标签体的标签
我们先来看看标签体内容的可选值:
元素的可选值有:
 
自定义有标签体的标签需要:
 
public class HelloTag extends SimpleTagSupport {
    public void 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);
        }
    }
}
   
        hello
        cn.eduask.tags.HelloTag
        scriptless
   
   
        哈哈哈~


   
 
2.3 不执行标签下面的页面内容
  如果希望在执行了自定义标签后,不再执行JSP页面下面的东西,那么就需要在doTag()方法中使用SkipPageException。
public class SkipTag extends SimpleTagSupport {
    public void doTag() throws JspException, IOException {
        this.getJspContext().getOut().print("只能看到我!

");
        throw new SkipPageException();
    }
}
   
        skip
        cn.eduask.tags.SkipTag
        empty
   
 
  看不见我!


 
2.4 带有属性的标签
  一般标签都会带有属性,例如,其中test就是一个boolean类型的属性。完成带有属性的标签需要:
 
public class IfTag extends SimpleTagSupport {
    private boolean test;
    public boolean isTest() {
        return test;
    }
    public void setTest(boolean test) {
        this.test = test;
    }
    @Override
    public void doTag() throws JspException, IOException {
        if(test) {
            this.getJspBody().invoke(null);
        }
    }
}
   
        if
        cn.eduask.tag.IfTag
        scriptless
       
            test
            true
            true
       
   

xixi
haha
hehe
 
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4