auto -instantiate a session bean?

断了今生、忘了曾经 提交于 2019-12-02 06:24:51

问题


I have a session bean

<managed-bean>
  <managed-bean-name>vdcAddBean</managed-bean-name>
  <managed-bean-class>com.cloud.appsportfolio.jsf.vdc.beans.VDCAddBean</managed-bean-class>
  <managed-bean-scope>session</managed-bean-scope>
</managed-bean>

Now, I am injecting this bean into a request one:

<managed-bean>
      <managed-bean-name>providerSelectionBean</managed-bean-name>
      <managed-bean-class>com.cloud.appsportfolio.jsf.sourcing.ProviderSelectionBean</managed-bean-class>
      <managed-bean-scope>request</managed-bean-scope>
      <managed-property>
        <property-name>vdcAddBean</property-name>
        <property-class>com.cloud.appsportfolio.jsf.vdc.beans.VDCAddBean</property-class>
        <value>#{sessionScope.vdcAddBean}</value> 
      </managed-property>
</managed-bean>

Well, when I'm accessing vdcAddBean in providerSelectionBean java code, I receive a NPE because vdcAddBean is not yet initialized. If I'm going first in my menu, in a page which has vdcAddBean in the back-end and comes back to providerSelectionBean all works great because it seems that vdcAddBean was already initialized.

The question is: how I can force vdcAddBean to be initialized (if it's null) when accessing providerSelectionBean bean?

Thanks.


回答1:


Replace

<value>#{sessionScope.vdcAddBean}</value> 

by

<value>#{vdcAddBean}</value> 

to get JSF to autocreate the bean.




回答2:


JSF managed session beans are stored within the ExternalContext, you can retrieve a map with all of them using the following method, getSessionMap.

The key to this map should be the managed-bean-name, so perhaps you can check for null and if so then try instantiating your bean and putting it directly into the sessionMap?



来源:https://stackoverflow.com/questions/6043259/auto-instantiate-a-session-bean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!