auto -instantiate a session bean?

2019-02-25 14:06发布

问题:

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?



标签: jsf jsf-1.2