JSP标签自定义(1)---useBean

来源:互联网 发布:vscode调试安卓 编辑:程序博客网 时间:2024/06/04 17:43
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

最近一直在学习自定义标签,于是就想试着去实现一些JSP中自带的标签库功能,顺便也把反射机制复习一下。(如果你对这两种技术不很熟悉,这篇blog的最下边有一些关于反射和自定义标签的example和PPT文档下载)

今天要实现的功能是useBean标签。下表是它的一些属性和用途。(我只选了个比较重要的属性,并没有实现所有属性)

属性 用途 id 给将要应用bean的变量一个名字,如果发现有相同id和scope的bean对象,则应用此对象而不会产生一个新的例示。 class 指明了bean的整个包名。 scope 表明了此bean的作用范围,共有四个值:page, request, session, 和 application,缺省的是page属性,表明此bean只能应用于当前页(保存在当前页的PageContext 中);request属性表明此bean只能应用于当前的用户请求中(保存在ServletRequest对象中);session属性表明此bean能应用于当前HttpSession生命周期内的所有页面;application属性值则表明此bean能应用于共享ServletContext的所有页面。需要注意的是,当没有具有相同的id和scope对象时,一个JSP:useBean 实体只能作用于一个新的例示中,反之,则作用于以前的对象,这时,在JSP:useBean标签之间的任何JSP:setParameter和其它实体都将被忽略。 type 说明将要索引对象的变量类型,它必须与类名及父类名相匹配。记住,这个变量的名字是由id属性值代替的。 beanName 给定此bean的名字,可以将其提供给bean的例示方法,只提供beanName和type而忽略class属性的情况是允许的。

 下面是标签处理方法类:useBean.java:

import javax.servlet.JSP.*;

import javax.servlet.JSP.tagext.*;

import java.lang.reflect.*;

//

public class useBean extends TagSupport{  //继承自TagSupport类

       private String scope;

       private String type;

       public useBean(){super();}

       /**

        *设置属性存取方法,这个方法由容器自动调用。setId()和getId()由系统自动实现

        */

       public void setScope(String s) {

           this.scope = s;

      }    

       public String getScope(){return this.scope;}

       public void setType(String type){

              this.type=type;

       }

       public String getType(){return this.type;}

       /**

        *覆盖doStartTag方法

        */

        public int doStartTag() throws JSPTagException 

        {  

            Object o = null;

           // find the bean in the specified scope

           if (scope.equals("page")) {

                     o = pageContext.getAttribute(getId(),PageContext.PAGE_SCOPE);

              } else if (scope.equals("request")) {

                     o = pageContext.getAttribute(getId(), PageContext.REQUEST_SCOPE);

              } else if (scope.equals("session")) {

                     o = pageContext.getAttribute(getId(), PageContext.SESSION_SCOPE);

              } else if (scope.equals("application")) {

                     o = pageContext.getAttribute(getId(), PageContext.APPLICATION_SCOPE);

           }

              if(o==null)

              {

                     System.out.println("o is null!");

                     try{

                            Class u=Class.forName(type);

                            o=u.newInstance();//无参构造方法

                            System.out.println("create success!");

                     }

                     catch(Exception e){

                            throw new JSPTagException("error to created a "+getId()+" object!");

                     }

              }

              pageContext.setAttribute(getId(), o); //保存实例对象到上下文对象中

           return EVAL_BODY_INCLUDE; //返回类型

       }

}

现在我们已经把对象实例放到pageContext里了,是不是这样就可以在JSP页面中直接引用了?当然不是了,直接将JAVA对象放进pageContext中与在脚本中直接引用是不同的。差别在于JSP容器要负责取得这个对象,以脚本变量的形式提供给页面。即JSP容器负责来维护脚本变量与pageContext中相应对象的状态。有两种方法可以为自定义标签来声明脚本变量。

一种是声明variable,一种是通过TagExtraInfo类声明变量。前者是JDK1.2后的方法,优点是比较方便。后者因为要再写一个类文件,所以显得麻烦些,但更灵活,出于兼容性与功能上的考虑,建议还是采用后者。(关于此类的详细说明,请参考PPT文档)

 import javax.servlet.JSP.*;

import javax.servlet.JSP.tagext.*;

 public class useBeanTag extends TagExtraInfo {

    public VariableInfo[] getVariableInfo(TagData data) {

        return new VariableInfo[] {

      new VariableInfo(

            data.getId(),

           data.getAttributeString("type"),

            true,

            VariableInfo.AT_BEGIN)

        };

  }

}

 

现在,定义一个useBean标签的工作已进行大半,下面该定义标签库描述(TLD)文件了,该文件是一个XML文档,主要定义标签的属性、处理类和扩展信息类的声明。主要声明部分如下:(tag.tld)

……………………(省去标头部分)

<!-- useBean 标签-->

 <tag>

<name>useBean</name>

<!—声明标签处理类-->

    <tag-class>cn.dever.tag.useBean</tag-class>

<!—声明标签扩展信息类-->

    <tei-class>cn.dever.taginfo.useBeanTag</tei-class>

    <!—主体内容类型-->

    <body-content>JSP</body-content>

<!—属性设置-->

   <attribute>

        <name>scope</name>

<!—是否必选-->

        <required>true</required>

<!—是否可以使用JSP表达式-->

        <rtexprvalue>true</rtexprvalue>

      </attribute>

   

      <attribute>

        <name>id</name>

        <required>true</required>

        <rtexprvalue>true</rtexprvalue>

      </attribute>

   

      <attribute>

        <name>type</name>

        <required>true</required>

        <rtexprvalue>true</rtexprvalue>

      </attribute>

 </tag>

其实这个标签库描述文件应该是最先建立的,因为我们主要是为了说明实现的方法,所以标签描述放在后边了。接下就是将刚才做的这些东西部署到我们的应用中去。在目标JSP页面中引用一下就OK了。

<%@ taglib uri="/WEB-INF/tag.tld" prefix="dever" %>

<dever:useBean id="test" type="cn.dever.common.User" scope="page" />

OK,到此为止,我们自定义的useBean标签已经可以开始工作了,是不是觉得很容易,其实本来就不难,只要大家掌握了定义标签的流程和配置方法,剩下的就只是写标签处理类了,在以后的几篇blog中,我就不再详述这些部署和配置,直接给出标签处理类的代码。

JSP /><JSP:useBean>

<JSP:useBean><JSP:useBean>自定义标签PPT

反射机制example

JSP:useBean>JSP:useBean>JSP:useBean><script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>