·控制jsp页面某一部分内容是否执行
java代码:
package cn.itcast.web.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
public class TagDemo1 extends TagSupport {
@Override
public int doStartTag() throws JspException {
return Tag.SKIP_BODY; //不输出return Tag.SKIP_BODY 输出return Tag.EVAL_BODY_INCLUDE
}
}
tld中的代码:
A tag library exercising SimpleTag handlers.
1.0
itcast
http://www.itcast.cn
viewIP
cn.itcast.web.tag.ViewIpTag
empty
demo1
cn.itcast.web.tag.TagDemo1
JSP
aaaaa
·使用标签控制整个jsp是否输出:
做这个标签的顺序和上一个一样,但内部有些变了:
1.建一个java程序继承TagSupport 类,2.重写doEndTag()方法3.返回Tag.SKIP_PAGE (这个返回值代表,这个标签以下的代码不执行,执行的是Tag.EVAL_PAGE 返回值)4.调用这个标签:<itcast:demo2/>
·循环输出标签
java代码:
package cn.itcast.web.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.IterationTag;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;
public class TagDemo3 extends TagSupport {
int x = 5;
@Override
public int doAfterBody() throws JspException {
x--;
if(x > 0){
return IterationTag.EVAL_BODY_AGAIN; //符合条件执行
}else{
return IterationTag.SKIP_BODY; //(IterationTag接口可实现迭代)
}
}
@Override
public int doStartTag() throws JspException {
return Tag.EVAL_BODY_INCLUDE; //设置他为执行
}
}
demo3
cn.itcast.web.tag.TagDemo3
JSP
asdasdasd
调用结果:输出五次
asdasdasd
·用标签修改jsp页面内容
java实现类:
package cn.itcast.web.tag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class TagDemo4 extends BodyTagSupport {
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
return BodyTag.EVAL_BODY_BUFFERED;
}
@Override
public int doEndTag() throws JspException {
BodyContent bc = this.getBodyContent();
String content = bc.getString();
content = content.toUpperCase();
try {
this.pageContext.getOut().write(content);
} catch (IOException e) {
throw new RuntimeException(e);
}
return EVAL_PAGE;
}
}
demo4
cn.itcast.web.tag.TagDemo4
JSP
This is my JSP page.
输出结果:THIS IS MY JSP PAGE