自定义控件
web.xml代码:下
tag.tld代码:下
<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"> my tag my tag 1.0 my-tag /my-tag <!-- 使用说明: jsp中使用范例:<my-tag:DateTag name="date" value="${date}"/>
-->
<description>日期控件</description>
<name>DateTag</name>
<tag-class>com.my.util.tag.DateTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<description>控件名</description>
<name>name</name>
<required>true</required>
</attribute>
<attribute>
<description>日期值(格式:yyyy-mm-dd),如果为空则默认填充今天
Tag.java代码:下 (在下面的doStartTag()方法中可根据自己需要写入代码)
public class DateTag extends TagSupport { public int doStartTag() throws JspException {
if (name == null || name.length() == 0) {
throw new JspException("日期控件名不能为空!");
}
if (value == null || value.length() != 10)
value = MyFormat.getToday();
StringBuffer html = new StringBuffer();
try {
html.append("<input style=\"cursor:pointer\" name=\"");
html.append(name);
html.append("\" id=\"");
html.append(name);
html.append("\" type=\"text\" size=\"10\" value=\"");
html.append(value);
html
.append("\" readonly=\"true\" /><script
type="text/javascript">"); html.append("Calendar.setup({inputField : ""); html.append(name); html.append("",ifFormat:"%Y-%m-%d",showsTime:false,button:""); html.append(name); html.append("",singleClick:false,step:1});"); pageContext.getOut().println(html.toString()); } catch (Exception e) { throw new JspException(e.getMessage()); } return this.SKIP_BODY; } }