1.自訂標籤庫(DateTag.tld)
<?xml version="1.0" encoding="GB2312" ?> <!DOCTYPE taglibPUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN""http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_2.dtd"><taglib><tlibversion>1.0</tlibversion><jspversion>1.2</jspversion><shortname>Date Tag Library</shortname><info>日期錄入標籤庫</info><tag><name>inputButton</name><tagclass>com.company.demo.tags.DateTag</tagclass><bodycontent>empty</bodycontent><attribute><name>name</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute><attribute><name>type</name><required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag></taglib>
2.Tag類(DateTag.java)
package com.company.demo.tags; import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;import javax.servlet.*;/*** <p>Title: </p>* <p>Description: </p>* <p>Copyright: Copyright (c) 2003</p>* <p>Company: </p>* @author 段洪傑* @version 1.0* 產生日期錄框*/public class DateTag extends TagSupport{String type,name; //類型和參數名public void setType(String type)throws JspException {this.type=type;}public void setName(String name)throws JspException {this.name=name;}public int doStartTag() throws JspException {String string,dialogHeight,dialogWidth,size;if(type=="date") {dialogHeight="290";dialogWidth="240";size="10";}else if(type=="datetime") {dialogHeight="315";dialogWidth="240";size="18";}else {throw new JspTagException("型別參數錯誤,只能選擇date或datetime!");}string="<SCRIPT language=javascript>"+"function selectDate"+name+"(oSource){"+"window.showModalDialog('content/dtpicker.jsp?rn='+Math.random(),oSource,
'dialogHeight:"+dialogHeight+"px; dialogWidth: "+dialogWidth+"px;center: Yes;
help: No; resizable: No;scroll:No;status: No;')"+"}"+"</script>"+"<INPUT readOnly size="+size+" name="+name+" DataType=/"date/" comparer=/"compareToLastUsedOn/" dateTimeType="+type+">"+"<IMG src=/"images/select.gif/" width=/"23/" height=/"23/" align=absMiddle"+" onclick=selectDate"+name+"(document.all(this.sourceIndex-1))> ";try{JspWriter out=pageContext.getOut();out.println(string);}catch(Exception ex){throw new JspTagException("程式調用標籤時出錯: "+ex.getMessage());}return SKIP_BODY;}public int doEndTag() throws JspException {return EVAL_PAGE;}}