愚僧

赢与输的差别通常是--不放弃

BlogJava 首页 新随笔 联系 聚合 管理
  23 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks

2013年3月4日 #



步骤:
1. 定义tld标签描述文件
2. 新建class继承SimpleTagSupport或者BodyTagSupport
3. taglib命令声明
4. 使用自定义标签

1. 定义tld标签描述文件custom_tag.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">
    <description>JSTL 1.1 core library</description>
    <display-name>JSTL core</display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>ct</short-name>
    <!-- 与 taglib 的 uri 对应 -->
    <uri>http://www.customtag.com/custom_tag</uri>
    <!-- 定义一个标签 -->
    <tag>
        <!-- 标签的名称 -->
        <name>date</name>
        <!-- 标签类 -->
        <tag-class>com.customtag.tags.DateTag</tag-class>
        <!-- 标签体 -->
        <body-content>empty</body-content>
        <attribute>
            <!-- 属性名称 -->
            <name>format</name>
            <!-- 是否必选 true:必选 -->
            <required>false</required>
            <!-- 是否允许使用表达式(EL), false:不能使用 -->
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>value</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>
注:
可参考jstl-[version].jar中META-INF下的c.tld文件

2. 新建DateTag继承SimpleTagSupport或者BodyTagSupport
package com.customtag.tags;

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;

public class DateTag extends SimpleTagSupport {
    
    @Override
    public void doTag() throws JspException, IOException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        PageContext pc = (PageContext) getJspContext();
        JspWriter out = pc.getOut();
        try{
            if(null != this.getValue()){
                out.print(sdf.format(new Date(this.getValue())));
            }else{
                out.print(sdf.format(new Date()));
            }
        }catch(IOException e){
            throw e;
        }catch(Exception e){
            out.print("");
        }
    }
    
    private String format="yyyy-MM-dd HH:mm:ss";
    
    private Long value = null;
    
    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public Long getValue() {
        return value;
    }

    public void setValue(Long value) {
        this.value = value;
    }
    
}

3. taglib命令声明
<%@taglib prefix="ct" uri="http://www.customtag.com/custom_tag"%>

4. 使用自定义标签
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    
String path = request.getContextPath();
    
String basePath = request.getScheme() + "://"
            
+ request.getServerName() + ":" + request.getServerPort()
            
+ path + "/";
%>
<%@taglib prefix="ct" uri="http://www.customtag.com/custom_tag"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        
-->
    </head>

    <body>
        自定义标签测试 : 
        <br>
        <ct:date/><br/>
        <ct:date format="MM/dd/yyyy"/><br/>
        <ct:date format="yyyy年MM月dd日 HH时mm分ss秒" value="<%=new Date().getTime() %>"/><br/>
    </body>
</html>

[运行结果]
自定义标签测试:
2013-03-04 16:18:29
03/04/2013
2013年03月04日 16时18分29秒
posted @ 2013-03-04 16:35 ywm 阅读(107) | 评论 (0)编辑 收藏