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

JSP自定义标签(笔记)

程序员文章站 2022-04-25 21:50:11
...

自定义jsp标签:

       step1,写一个java类,继承SimpleTagSupport类。
       step2,在doTag方法里面,编写处理逻辑。
       step3,在.tld文件里面,描述标签。
          <body-content>empty</body-content>:
          empty: jsp标签没有标签体。
                     <body-content>scriptless</body-content>
          scriptless:jsp标签可以有标签体,但是,标签体
                          的内容不能够出现java代码。即不能够使用
                          <% %>,<%= %>,<%!  %>
                          <body-content>JSP</body-content>
          JSP:jsp标签有标签体,并且标签体的内容可以是
                 java代码。只有复杂标签技术才支持这个值,简
                 单标签技术不支持。

 

自定义JSP标签的例子:

       1.新建一个java类:

               package jspTagTest;

               import java.io.IOException;
               import java.text.SimpleDateFormat;
               import java.util.Date;

               import javax.servlet.jsp.JspException;
               import javax.servlet.jsp.JspWriter;
               import javax.servlet.jsp.PageContext;
               import javax.servlet.jsp.tagext.SimpleTagSupport;

               /**
               * @author Leinuoa
               * 自定义JSP标签
               */
               public class CustomJspTag extends SimpleTagSupport {
                   private String pattern;
 
                   public void setPattern(String pattern) {
                        this.pattern = pattern;
                    }

                   @Override
                   public void doTag() throws JspException, IOException {
                        PageContext ctx = (PageContext) getJspContext();
                        Date date = new Date();
                        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
                        JspWriter out = ctx.getOut();
  
                        out.println(sdf.format(date));
                   }
              }

         2.在WEB-INF目录下新建一个 xx.tld 文件,配置相关信息:

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

                <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee                http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
                version="2.0">
 
                <tlib-version>1.1</tlib-version>
                <short-name>c1</short-name>
                <uri>http://webTest7/mytag</uri>
                <tag>
                <name>date</name>
                <tag-class>jspTagTest.CustomJspTag</tag-class>
                <body-content>empty</body-content>
                <attribute>
                    <name>pattern</name>
                    <required>true</required>
                    <rtexprvalue>false</rtexprvalue>
                </attribute>
                </tag>
 
                </taglib>

 

        3.应用到JSP中:

               <%@page pageEncoding="utf-8" contentType="text/html;charset=utf-8" %>
               <%@taglib uri="http://webTest7/mytag" prefix="c1" %>

                   ... ...

                   <c1:date pattern="yyyy年MM月dd日" /><!-- date为tld文件中的<name>date</name> 名字必 须一致 -->

                   ... ...

 

相关标签: JSP 自定义标签