1.自定义标签库(DateTag.tld)
<?xml version="1.0" encoding="GB2312" ?> <!DOCTYPE taglib PUBLIC "-//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; } }