1. 程式人生 > >Java-Web學習筆記(第九章)

Java-Web學習筆記(第九章)

port pac clas tle code lang tran RR req

一:自定義標簽庫(步驟)
1>開發自定義標簽類(編寫一個實現SimpleTagSupport接口的java類)

package book07;

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.tagext.SimpleTagSupport;

/**
 * @author 國真
 *
 */
public class DateFormat extends SimpleTagSupport {
    /**
     * 如果標簽類中包含屬性,每個屬性都要有對應的setter和getter方法
     */
    Date date;
    String type;

    public Date getDate() {
        return date;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    /**
     * 重寫doTag()方法,該方法在標簽結束生成頁面內容
     */

    @Override
    public void doTag() throws JspException, IOException {
        SimpleDateFormat sdf = new SimpleDateFormat();
        if(type.equals("full")){
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        if(type.equals("date")){
            sdf = new SimpleDateFormat("yy-MM-dd");
        }
        if(type.equals("time")){
            sdf = new SimpleDateFormat("HH:mm:ss");
        }

        //將格式化後的結果輸出到頁面
        JspWriter out = super.getJspContext().getOut();
        out.print(sdf.format(date));
    }

}
***2>建立TLD文件(在tld文件中對標簽處理器類進行描述,tld文件的位置:WEB-INF下)***
<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE taglib  
    PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"  
    "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>        
    <!--定義標簽版本庫-->  
    <tlib-version>1.0</tlib-version>  
    <!--定義jsp版本庫-->  
    <jsp-version>2.0</jsp-version>

    <short-name>dateFormat</short-name>
    <uri>/dateFormat</uri>

    <tag>
        <name>dateFormat</name><!-- tag的名字 -->
        <tag-class>book07.DateFormat</tag-class><!-- tag對應的java類的名字 -->
        <body-content>empty</body-content>

        <description>format date</description>
        <attribute>                                 <!-- 這裏表示的是這個tag的一個參數  -->
            <name>date</name>                       <!-- 這個參數的名字 -->
            <required>true</required>               <!-- 是否是必填項 -->
            <rtexprvalue>true</rtexprvalue>         <!-- 這個參數是否可以寫入(這個參數是否可以動態賦值) -->
        </attribute>

        <attribute>
            <name>type</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>
***3>使用標簽庫(在JSP頁面中導入和使用自定義標簽)***
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="mydate" uri="/dateFormat" %>
<!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>自定義日期標簽</title>
</head>
<body>
    <mydate:dateFormat date="<%= new Date() %>" type="full"/>
    <br />
    <mydate:dateFormat date="<%= new Date() %>" type="date"/>
    <br />
    <mydate:dateFormat date="<%= new Date() %>" type="time"/>
</body>
</html>

Java-Web學習筆記(第九章)