[color=blue]web.xml代码:下[/color]
<jsp-config>
<taglib>
<taglib-uri>/my-tag</taglib-uri>
<taglib-location>
/WEB-INF/tags/my-tag.tld
</taglib-location>
</taglib>
</jsp-config>
[color=blue]tag.tld代码:下[/color]
<?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>my tag</description>
<display-name>my tag</display-name>
<tlib-version>1.0</tlib-version>
<short-name>my-tag</short-name>
<uri>/my-tag</uri>
<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),如果为空则默认填充今天
</description>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
[color=blue]Tag.java代码:下
(在下面的doStartTag()方法中可根据自己需要写入代码)[/color]
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});</script>");
pageContext.getOut().println(html.toString());
} catch (Exception e) {
throw new JspException(e.getMessage());
}
return this.SKIP_BODY;
}
}
<jsp-config>
<taglib>
<taglib-uri>/my-tag</taglib-uri>
<taglib-location>
/WEB-INF/tags/my-tag.tld
</taglib-location>
</taglib>
</jsp-config>
[color=blue]tag.tld代码:下[/color]
<?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>my tag</description>
<display-name>my tag</display-name>
<tlib-version>1.0</tlib-version>
<short-name>my-tag</short-name>
<uri>/my-tag</uri>
<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),如果为空则默认填充今天
</description>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
[color=blue]Tag.java代码:下
(在下面的doStartTag()方法中可根据自己需要写入代码)[/color]
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});</script>");
pageContext.getOut().println(html.toString());
} catch (Exception e) {
throw new JspException(e.getMessage());
}
return this.SKIP_BODY;
}
}