JSP自定義標籤(一)

Demo下載

分四次介紹吧,先來實現一些簡單的,結尾附上項目工程目錄。

一、打造最簡單的自定義標籤

package com.yt.tag.simple;

import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTagSupport;

/**
 * 沒有任何作用的標籤
 * @author ChenSS on 2017年6月15日
 */
public class SimpleTag extends SimpleTagSupport {

  /**
   * 自定義標籤的處理內容
   */
  @Override
  public void doTag() throws JspException, IOException {
    getJspBody().invoke(null);
    //TODO: 定義代碼邏輯,上面一行標籤內容原樣輸出
  }

  /**
   * 父級標籤:標籤嵌套可能用到,不需要重寫此方法
   */
  @Override
  public JspTag getParent() {
    return super.getParent();
  }
  
  /**
   * 獲取上下文:獲取一些頁面信息會用到,不需要重寫此方法
   */
  @Override
  protected JspContext getJspContext() {
    return super.getJspContext();
  }
}

 

二、自定義標籤屬性

 

package com.yt.tag.simple;

import java.io.IOException;
import java.io.StringWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;

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

public class DateTag extends SimpleTagSupport {
    StringWriter writer = new StringWriter();
    /**
     * 自定義屬性
     */
    String pattern;

    /**
     * JSP頁面上的屬性值會通過set方法注入
     * 
     * @param pattern
     */
    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    @Override
    public void doTag() throws JspException, IOException {
        if (pattern == null) {
            getJspBody().invoke(null);
        } else {
            //日期轉換
            getJspBody().invoke(writer);
            Date date = Date.valueOf(writer.toString());
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            getJspContext().getOut().println(formatter.format(date));
        }
    }
}

 

三、實現日期轉換標籤

package com.yt.tag.simple;

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

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

public class DatePlusTag extends SimpleTagSupport {
    private String pattern;
    private String date;

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public void setDate(String date) {
        this.date = date;
    }

    @Override
    public void doTag() throws JspException, IOException {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        String strDate = null;
        try {
            if (date.length() < 10) {
                strDate = formatter.format(Date.valueOf(date));
            } else {
                strDate = formatter.format(Timestamp.valueOf(date));
            }
        } catch (Exception e) {
            strDate = e.getMessage();
        }
        getJspContext().getOut().println(strDate);
    }
}

四、.tld配置文件

寫完自定義標籤的實現類,在WEB-INF目錄下編寫.tld文件,simple.tld文件內容如下:

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>2.0</jsp-version>
    <short-name>simple</short-name>
    <uri>/css_simple_tag</uri>
    <!-- 最簡單的標籤 -->
    <tag>
        <name>simple</name>
        <tag-class>com.yt.tag.simple.SimpleTag</tag-class>
        <body-content>scriptLess</body-content>
    </tag>
    <!-- 測試自定義標籤屬性 -->
    <tag>
        <name>date</name>
        <tag-class>com.yt.tag.simple.DateTag</tag-class>
        <body-content>scriptLess</body-content>
        <attribute>
            <name>pattern</name>
            <required>true</required>
            <type>String</type>
        </attribute>
    </tag>
    <!-- 實現日期轉換標籤,包含兩個屬性 -->
    <tag>
        <name>date2</name>
        <tag-class>com.yt.tag.simple.DatePlusTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>pattern</name>
            <required>true</required>
            <type>String</type>
        </attribute>
        <attribute>
            <name>date</name>
            <required>true</required>
            <type>String</type>
        </attribute>
    </tag>
</taglib> 

五、JSP頁面中使用自定義標籤

JSP頁面名爲simple_tag.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 引入命名空間 -->
<%@ taglib uri="/css_simple_tag" prefix="tag"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <tag:simple>sdsafadsfaads</tag:simple>
    <tag:date pattern="yyyy-MM-dd HH:mm:ss">2013-3-4</tag:date>
    <tag:date2 pattern="yyyy-MM-dd HH:mm:ss" date="2013-3-4 23:12:4.1" />
</body>
</html>

 

工程目錄如下: