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>

 

工程目录如下:

相关文章
相关标签/搜索