自定义控件

[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;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值