Jsp 自定义tag编程

Jsp 自定义tag编程

不错的文章


I think the essence of wisdom is emancipation, as fat as possible, from the tyranny of the here and now. We cannot help the egoism of our senses. Sight and sound and touch are bound up with our own bodies and cannot be impersonal. Our emotions start similarly from ourselves. An infant feels hunger or discomfort, and is unaffected except by his own physical condition. Gradually with the years, his horizon widens, and, in proportion as his thoughts and feelings become less personal and less concerned with his own physical states, he achieves growing wisdom. This is of course a matter of degree. No one can view the world with complete impartiality; and if anyone could, he would hardly be able to remain alive. But it is possible to make a continual approach towards impartiality, on the one hand, by knowing things somewhat remote in time or space, and on the other hand, by giving to such things their due weight in our feelings. It is this approach towards impartiality that constitutes growth in wisdom.

今天学习带body的自定义标签,尝试写一个标签,这个标签可以迭代显示标签主体里面的内容,迭代的次数由标签的属性指定.
首先,要说明的是,其实标签也是一个java类,它是运行一个或两个接口的javabean,然后再配合标签描述文件(.tld为扩展名),两者结合就可以作出自己的自定义标签库了.呵呵,是不是觉得有点眉目了?
那么下面我们开始写一个自己的java标签类,作为有body的标签,这个类必须实现javax.servlet.jsp.tagext.BodyTag接口.
下面我们看一下BodyTag这个接口中定义了那些方法来支持tag

public void doInitBody();
public void setBodyContent(javax.servlet.jsp.tagext.BodyContent);
public int doAfterBody();
那么由于BodyTag继承了javax.servlet.jsp.tagext.Tag接口,所以我们再来看一下Tag中的方法:

public void release();
public javax.servlet.jsp.tagext.Tag getParent();
public void setParent(javax.servlet.jsp.tagext.Tag);
public int doEndTag();
public int doStartTag();
public void setPageContext(javax.servlet.jsp.PageContext);
说了这么多方法,是不是有点头晕了?呵呵,那么究竟BodyTag的处理过程是怎么样的呢?下面我们就来说一下它的处理流程:

1.
当容器创建一个新的标签实例后,通过setPageContext来设置标签的页面上下文.
2.
使用setParent方法设置这个标签的上一级标签,如果没有上一级嵌套,设置为null.
3.
设置标签的属性,这个属性在标签库描述文件中定义,如果没有定义属性,就不调用此类方法.
4.
调用doStartTag方法,这个方法可以返回EVAL_BODY_INCLUDESKIP_BODY,当返回EVAL_BODY_INCLUDE时,就计算标签的body,如果返回SKIP_BODY,就不再计算标签的body.
5.
调用setBodyContent设置当前的BodyContent.
6.
调用doInitBody,如果计算BodyContent时需要进行一些初始化,就在这个方法中进行.
7.
每次计算完Body后调用doAfterBody,如果返回EVAL_BODY_TAG,表示继续计算一次Body,知道返回SKIP_BODY才继续往下执行.
8.
调用doEndTag方法,这个方法可以返回EVAL_PAGE或者SKIP_PAGE,当返回EVAL_PAGE时,容器将在标签结束时继续计算JSP页面其他的部分;如果返回SKIP_PAGE,容器将在标签结束时停止计算JSP页面其他的部分.
9.
调用release()方法释放标签程序占用的任何资源。
知道了标签处理的流程了,那么下面我们就开始写一个自己的java类了
我的标签类的源码:

  1. package test.jsp.tags;
  2. import javax.servlet.jsp.tagext.BodyTagSupport;
  3. import javax.servlet.jsp.tagext.BodyContent;
  4. import javax.servlet.jsp.JspTagException;
  5. public class BodyTagExample
  6. extends BodyTagSupport {
  7. private int counts;
  8. public BodyTagExample() {
  9. super();
  10. }
  11. public void setCounts(int counts) {
  12. this.counts = counts;
  13. }
  14. public int doStartTag() throws JspTagException {
  15. System.out.println("doStartTag...");
  16. if (counts > 0) {
  17. return EVAL_BODY_TAG;
  18. }
  19. else {
  20. return SKIP_BODY;
  21. }
  22. }
  23. public void setBodyContent(BodyContent bodyContent) {
  24. System.out.println("setBodyContent...");
  25. this.bodyContent = bodyContent;
  26. }
  27. public void doInitBody() throws JspTagException {
  28. System.out.println("doInitBody....");
  29. }
  30. public int doAfterBody() throws JspTagException {
  31. System.out.println("do After body..." + counts);
  32. if (counts > 1) {
  33. counts--;
  34. return EVAL_BODY_TAG;
  35. }
  36. else {
  37. return SKIP_BODY;
  38. }
  39. }
  40. public int doEndTag() throws JspTagException {
  41. System.out.println("do end Tag...");
  42. try {
  43. if (bodyContent != null) {
  44. bodyContent.writeOut(bodyContent.getEnclosingWriter());
  45. }
  46. }
  47. catch (java.io.IOException e) {
  48. throw new JspTagException("IO Error: " + e.getMessage());
  49. }
  50. return EVAL_PAGE;
  51. }
  52. }


写完了java类,下面我们该用一个描述文件来描述我们写的这个类了:
我的tld描述文件源码:

<?xml version="1.0" encoding="ISO-8859-1" ?>
br />PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

1.0
1.1

Application Tag Library
http://jakarta.apache.org/taglibs/struts-example-1.0
tag loop


loop
test.jsp.tags.BodyTagExample
JSP

counts
true
true



我们对这个文件进行一个小小的说明:
最重要的是上面的黑体字部分,其他的部分我们可以拷贝粘贴就可以了。

tag
中的name属性:定义了我们的tag名称,在后面会用到。
tag
中的tagclass属性:指定了我们这个tag的实现类。
tage
中的bodycontent属性:指定我们的页面内容是什么性质的。(注意:在jsp开发中这里必须写JSP)
tage
中的attribute属性:定义了我们的这个tag可能有的属性。

attribute
中的name属性:指定了属性的名称。它和我们类中定义的“int counts;”必须一样,并且在类中还必须包含一个setCounts(int counts)方法,否则这个属性就不能设置。
attribute
中的required属性:表示这个属性是否是必须的。
attribute
中的rtexprvalue属性:表示这个属性是否可以用JSP的程序段的结果输出。
现在我们的tag就写完了,下面就是我们怎么在页面中用了。
首先,我们要在web.xml中声明这个tag
我的web.xml源码:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. /span>"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
  3. demotag
  4. /WEB-INFtldmytag.tld


其次,我们要在页面中引用这个tag
我的jsp源码:

  1. "text/html; charset=GBK" language="java"%>
  2. "demotag" prefix="demo" %>

  3. "5">
  4. 现在时间是:new java.util.Date().toString()%>


好了,一个tag就开发完了,下面是页面的显示结果:现在时间是:Thu Sep 22 12:33:31 CST 2005现在时间是:Thu Sep 22 12:33:31 CST 2005现在时间是:Thu Sep 22 12:33:31 CST 2005现在时间是:Thu Sep 22 12:33:31 CST 2005现在时间是:Thu Sep 22 12:33:31 CST 2005
这里是后台打出来的信息:

doStartTag...
setBodyContent...
doInitBody....
do After body...5
do After body...4
do After body...3
do After body...2
do After body...1
do end Tag...
一切OK了!
程序很简单,但是希望以此来向大家展示一个tag的基本开发流程,如果有不对的地方,希望大家批评指正!谢谢!

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7911270/viewspace-914065/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7911270/viewspace-914065/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值