自定义标签
一、SimpleTagSupport
1.建一个java类AddTag.java,继承SimpleTagSupport,重写doTag()方法:
public class AddTag extends SimpleTagSupport{
private int num1;
private int num2;
public void setNum1(int num1) {
this.num1 = num1;
}
public void setNum2(int num2) {
this.num2 = num2;
}
@Override
public void doTag()throws JspException, IOException {
JspContext ctx=getJspContext();
JspWriter out=ctx.getOut();
out.write(num1+"+"+num2+"="+(num1+num2));
}
}
2.建一个.tld文件(放在WEB-INF下).
<tag>
<name>add</name> <!--标签名--> <tag-class>com.zh.custag.AddTag</tag-class> <!--类名--> <body-content>empty</body-content>
<attribute>
<name>num1</name> <!--属性一--> <required>true</required>
<rtexprvalue>true</rtexprvalue> <!--是否能在运行时计算-->
<type>java.lang.Integer</type>
</attribute>
<attribute>
<name>num2</name> <!--属性二-->
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Integer</type>
</attribute>
</tag>
3..jsp文件内容:
<body>
<math:add num2="2" num1="3"/>
</body>
二、TagSupport
1.建一个java类MyHr.java,继承TagSupport,重写doStartTag()和doEndTag()方法:
public class MyHr extends TagSupport {
@Override
public int doStartTag()throws JspException { //当读到开始标签时,调用该方法
// TODO Auto-generated method stub
JspWriter out=pageContext.getOut(); //得到网络输出流,是从父类继承过来的成员
try {
out.println("<h4>开始执行doStartTag()......</h4>");//向网页输出内容
for(inti=1;i<=5;i++){ //输出5条水平线
out.print("<hr>");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EVAL_BODY_INCLUDE; //处理标记体
//returnTag.SKIP_BODY; //跳过标记体
}
@Override
public int doEndTag()throws JspException { //读取到结束标签时,调用该方法
// TODO Auto-generated method stub
JspWriter out=pageContext.getOut();
try {
out.println("<h3>开始执行doEndingTag()......</h3>.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//return Tag.SKIP_PAGE; //返回这个值,则终止页面执行
return EVAL_PAGE; //继续显示自定义后的标签
}
}
2.建一个.tld文件(放在WEB-INF下).
<tag>
<name>myhr</name>
<tag-class>com.zh.custag.MyHr</tag-class>
<body-content>JSP</body-content>
</tag>
<tag>
3..jsp文件内容:
<body>
<math:myhr>标签中间的内容</math:myhr>
<b>标签后的内容</b> <!-- 字体加粗-->
</body>
三、 BodyTagSupport
1.建一个java类TestBodyTag.java,继承BodyTagSupport,重写doStartTag()、doAfterBody( )和doEndTag()方法:
public class TestBodyTag extends BodyTagSupport {
private int loop;
public void setLoop(int loop) {
System.out.println("setLoop()"); //从控制台可以看出:赋值一次
this.loop = loop;
}
@Override
public int doStartTag()throws JspException { //执行一次
// TODO Auto-generated method stub
System.out.println("doStartTag()");
if(loop>0){
return EVAL_BODY_INCLUDE;
}else{
return SKIP_BODY;
}
}
@Override
public int doAfterBody()throws JspException { //执行了四次
System.out.println("doAfterBody()");
if(loop>0){
loop--;
return EVAL_BODY_AGAIN;
}else{
return SKIP_BODY;
}
}
@Override
public int doEndTag()throws JspException {
// TODO Auto-generated method stub
System.out.println("doEndTag()"); //在控制台打出,看该方法执行几次
return EVAL_PAGE;
}
}
2.建一个.tld文件(放在WEB-INF下)
<tag>
<name>mybody</name>
<tag-class>com.zh.custag.TestBodyTag</tag-class>
<body-content>tagdependent</body-content>
<attribute>
<description>循环的次数</description>
<name>loop</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Integer</type>
</attribute>
</tag>
3..jsp文件内容:
<body>
<math:mybody loop="3">标签中间的内容<br></math:mybody>
标签后的内容
</body>