<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    愚僧

    贏與輸的差別通常是--不放棄

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      23 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks


    步驟:
    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 on 2013-03-04 16:35 ywm 閱讀(119) 評論(0)  編輯  收藏 所屬分類: servlet & jspstruts
    主站蜘蛛池模板: 三上悠亚电影全集免费| jzzijzzij在线观看亚洲熟妇| 一级黄色免费大片| 免费国产成人高清在线观看麻豆| 亚洲性无码AV中文字幕| 影音先锋在线免费观看| 亚洲人成网站18禁止| 成年18网站免费视频网站| 亚洲综合成人婷婷五月网址| 午夜毛片不卡高清免费| 在线亚洲v日韩v| 亚洲欧洲国产成人综合在线观看| 特色特黄a毛片高清免费观看| 亚洲无码高清在线观看| CAOPORN国产精品免费视频| 久久精品国产精品亚洲色婷婷| 日日麻批免费40分钟无码| 亚洲网站免费观看| 在线观看免费成人| 免费无码午夜福利片 | 亚洲不卡中文字幕| 最近2019中文字幕mv免费看| 久久精品国产亚洲av天美18| 亚洲精品综合久久| 无码人妻久久一区二区三区免费| 亚洲的天堂av无码| 性色av免费观看| h片在线播放免费高清| 亚洲美免无码中文字幕在线| 成年美女黄网站色大免费视频| 国产亚洲人成在线影院| 亚洲精品高清无码视频| 日本阿v免费费视频完整版| 国产成人+综合亚洲+天堂| 亚洲成av人在线视| 丁香花在线观看免费观看| 国产高潮流白浆喷水免费A片 | 亚洲精品国产免费| 内射无码专区久久亚洲| 永久看日本大片免费35分钟| 疯狂做受xxxx高潮视频免费|