如果一个标签有了属性,则这个标签将是更加灵活而且好使用。下面来列举一下开发带属性的标签的步骤。
首先还是写一个类继承SimpleTagSupport,然后为属性声明一个值,当然,也得为了这个值进行按照JavaBean那样设置Set方法以便服务器进行调用。下面的内容和写正常的标签类就差不多了。当然,在书写TLD标签的时候还是有些不一样的。
package com.bird.web.simpleTag;
import java.io.IOException;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* 开发带属性的标签
* @author Bird
*
*/
public class SimpleTagDemo5 extends SimpleTagSupport {
private int count;
private Date date;
public void setDate(Date date) {
this.date = date;
}
public void setCount(int count) {
this.count = count;
}
@SuppressWarnings("deprecation")
@Override
public void doTag() throws JspException, IOException {
JspFragment jf = this.getJspBody();
String time = date.toLocaleString();
this.getJspContext().getOut().write(time);
for(int i = 0; i < count; i++){
jf.invoke(null);
}
}
}
然后是对这个标签进行描述的文件
<tag>
<name>demo5</name>
<tag-class>com.bird.web.simpleTag.SimpleTagDemo5</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>date</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<required>true</required>
这句话的意思是这个标签的属性是否是必须的,设置为True意思为必须
<rtexprvalue>true</rtexprvalue>