小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

bean scope in jsf

 chanvy 2009-01-08
再來解釋JSF中的MBean,其實MBean就是一個JavaBean,因此它有著和JavaBean相同的要求。
        最后是MBean的配置,JSF的MBean需要在faces-config文件中做配置,配置方法如下:
    <managed-bean>
       
<description>demo of config</description>
       
<display-name>userInfo</display-name>
       
<managed-bean-name>user</managed-bean-name>
       
<managed-bean-class>net.moon.beans.UserInfo</managed-bean-class>
       
<managed-bean-scope>session</managed-bean-scope>
    
</managed-bean>
 
對其中的managed-bean-name,managed-bean-class和managed-bean-scope做一下解釋。
        managed-bean-name是這一MBean的名字,用于其它位置的調用。
        managed-bean-class是這一MBean的完整路徑,用于指明該MBean的類文件位置。
        managed-bean-scope是這一MBean的有效范圍。
下面再對manage-bean-scope進行一下詳細的解釋,其有效取值為:application, session, request, none。很容易理解,它們的存活周期分別如下:
Name
Scope
Application
整個應用
Session
整個對話
Request
整個請求
None
需要時,臨時
大家知道,JSF是以JSP為基礎的,那么,對于JSP的九大對象來說,這四種scope的信息到底是怎么存儲的呢?
經過測試證明,scope為applicatoin的MBean的實例保存在ServletContext中,也就是JSP中的application中,因此我們可以用如下的方法得到某個類的引用:
FacesContext fc = FacesContext.getCurrentInstance();
UserInfo ui 
= (serInfo)fc.getExternalContext().getApplicationMap().get("user");
 
 
對session級別的MBean,我們可以用如下方法得到其引用:
FacesContext fc = FacesContext.getCurrentInstance();
UserInfo ub 
= (UserInfo)fc.getExternalContext().getSessionMap().get("userInfo");
 
 
當然,我們也可以用其它的方法得到session對象后,從session中取出實例。
 
對request級別的MBean,我們可從request對象中取得,代碼如下:
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest request 
= (HttpServletRequest)fc.getExternalContext().getRequest();
UserInfo ui 
= (UserInfo)request.getAttribute("user");
 
 
至于none類型的MBean,應該只能得到新的實例了。
 
當然,JSF提供了另外的訪問MBean的方法,我們可以用如下的代碼得到MBean的實例:
FacesContext context = FacesContext.getCurrentInstance();
ValueBinding binding 
=  context.getApplication().createValueBinding("#{user}"); 
UserBean user 
= (UserBean) binding.getValue(context); 
 
也可用如下的代碼直接得到MBean的一個屬性:
FacesContext context = FacesContext.getCurrentInstance(); 
ValueBinding binding 
=  context.getApplication().createValueBinding("#{user.name}"); 
String name 
= (String) binding.getValue(context); 

 

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多