JavaBean

JavaBean.

JavaBean :軟件組件,主要用於事務處理

       規範要求(反射):是一個公共(public)類

                                   不帶參數的構造方法

                                   通過setXXX方法設置屬性,getXXX方法獲取屬性

    1.   JSP訪問javaBean 的語法             

                     導入JavaBean

                     聲明JavaBean對象

                     訪問JavaBean屬性

              例:<%@ import = “mypack.CounterBean”%>

                     <jsp:useBeanid=”myBean” class=”mypack.CounterBean” scope=”session”/>

                     <jsp:getPropertyname=”myBean” property=”count”/>

                     <jsp:setPropertyname=”myBean” property=”count” value=”0”/>

     2.  可以在<jsp:setProperty>中通過param參數來爲bean的屬性動態賦值

              <jspsetProperty property=”age” name=”person”value=”helloworld”/>

              對應的Servlet代碼是:

   org.apache.jasper.runtime.JspRuntimeLibrary.introspecthelper(_jspx_page_context.findAttribute("person"),"age", request.getParameter("helloworld"), request,"helloworld", false);

   3.   JavaBean 的存活範圍
              ---page  (默認值)

              ---request

              ---session

              ---application

 

   4 . javaBean放在session範圍內時,放在application也不一樣

       <jsp:useBeanid=”student” scope=”session” class=”javaBean.Stdent”/>

       生成的servlet源代碼是:

                 javaBean.Student student = null;

                 synchronized (session) {

                           student = (javaBean.Student)_jspx_page_context.getAttribute("student",javax.servlet.jsp.PageContext.SESSION_SCOPE);

                         if (student == null){

                                       student = new javaBean.Student();

                                      _jspx_page_context.setAttribute("student", student, javax.servlet.jsp.PageContext.SESSION_SCOPE);

                               }

                         }

發佈了43 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章