1
votes

I'm new to Java so sorry if my terminology is not correct.

I have a managed bean setup (PolicyInformation) with a session scope that I instantiate in my Java code with:

thisPolicy = (PolicyInformation) Utils.getSessionMapValue("policyInformationBean");
if (thisPolicy == null) {
    thisPolicy = new PolicyInformation();
}

The code above checks if the bean already exists, and if it does, it uses the object in the session map. It seems to work great.

However, if I add a label on my XPage to display a value from my PolicyInformation managed bean with the following code (using expression language):

<xp:label value="#{policyInformationBean.name}"/>

the managed bean runs twice: once when I call it in my Java class, and again when I add the label above.

Is there a way I can prevent that second call when adding the label on my XPage? Can I somehow get the value from the Java session map in my XPage label?

faces-config:

<managed-bean>
<managed-bean-name>policyInformationBean</managed-bean-name>
<managed-bean-scope>session</managed-bean-scope>
<managed-bean-class>com.package.PolicyInformation</managed-bean-class>
</managed-bean>
2
Please also show your faces-config XML file. With a managed bean you shouldn't ever have to use the 'new' keyword. - Steve Zavocki
Now if you want to control the creation of your bean, which is very common and useful, then your bean would be a POJO. The term managed bean implies that the creation is done for you. - Steve Zavocki
also, look at JSF lifecycle (methods can be called multiple times) and bear in mind it is hard to make singleton in XPages (because of multiple class loaders) - Frantisek Kossuth
I added my faces-config bean. - Ryan Buening
Do you add your manually created bean to the session scope after instantiating it? - Sven Hasselbach

2 Answers

4
votes

Please consider these three things:

  1. Try adding an "id" property to your managed bean opening tag with the same name as your managed-bean-name. This is likely not your problem, but this is how we set it up.

Example:

<managed-bean id="dBar">
<managed-bean-name>dBar</managed-bean-name>
<managed-bean-class>eu.linqed.debugtoolbar.DebugToolbar</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope> 
</managed-bean>
  1. Remove all code that creates a new PolicyInformation object. To be managed, you have to let the framework manager it all the time. I think you might be confusing it, by creating another object.

  2. Alternatively, you could make this bean a POJO (Plain Old Java Object) and not a managed bean at all. This way you just create the object when you need it. You would not have an entry in faces-config anymore, and you would use the 'new' keyword when you need to create it. When the reference becomes null, then garbage collection will clean it up.

0
votes

Anyone at IBM, please have a look at the following Classes:

Thread [Thread-7] (Suspended (breakpoint at line 131 in ApplicationController))
ApplicationController.() line: 131
J9VMInternals.newInstanceImpl(Class) line: not available [native method]
Class.newInstance() line: 1688

Beans.instantiate(ClassLoader, String, BeanContext, AppletInitializer) line: 189
Beans.instantiate(ClassLoader, String) line: 80 ManagedBeanFactory$1.run() line: 222
AccessController.doPrivileged(PrivilegedExceptionAction) line: 413
ManagedBeanFactory.newInstance(FacesContext) line: 216
ApplicationAssociate.createAndMaybeStoreManagedBeans(FacesContext, String) line: 269

VariableResolverImpl.resolveVariable(FacesContext, String) line: 135 VariableResolverImpl.resolveVariable(FacesContext, String) line: 71

In my opinion IBM is not able to instanciate a application, view or session scoped variable synchronized, even thought they <sarcasm> allready have </sarcasm> JAVA 1.6

http://www-01.ibm.com/support/docview.wss?uid=swg21188789 allready wondering how long this link will work...

Please consider race conditions as a real thing, e.g. for instanciating.