ppackage cn.tag;
import java.io.IOException;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class TestTag extends BodyTagSupport {
private static final long serialVersionUID = -5663050580910865400L;
/**
* 可以在这里进行一些初始化的工作
*/
public int doStartTag() throws JspException {
System.out.println("----doStartTag()----");
JspWriter out = this.pageContext.getOut();
try {
out.println("before");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.EVAL_BODY_INCLUDE;
}
/**
* 核心处理标签的地方,业务逻辑在此实现,如:分页,根据参数返回给前台的值等等
*/
public int doEndTag() throws JspException {
JspWriter out = this.pageContext.getOut();
System.out.println("----doEndTag()----");
/**
* 获取参数
*/
Map map=this.pageContext.getRequest().getParameterMap();
if(map==null){
System.out.println("map is null");
}else{
System.out.println("map not null");
System.out.println(map.size());
}
try {
out.println("end");
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return super.doEndTag();
}
}
定义testTag.tld,貌似有些项是必需的,如下所示:
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">
测试
1.1
tt
/testTag
test
cn.tag.TestTag
JSP
测试页面,关键是要引入testTag.tld文件:
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'test.jsp' starting page测试
显示结果:
控制台:
----doStartTag()----
----doEndTag()----
map not null
页面:
before 测试end
分享到:
2009-04-09 11:52
浏览 1126
评论