Zack Macomber
Zack Macomber

Reputation: 6905

Why is my SessionScoped ManagedBean showing up fine in one place and null in another?

The following facelet code, which renders a message when the employeeId inputText component loses focus, is executing successfully...

<h:inputText id="employeeId" value="#{questionAnswerAction.questionAnswerActionForm.employeeId}" 
 required="true" requiredMessage="#{app:requiredFieldMessage(bundle, 'label.peoplesoftId')}"
 binding="#{questionAnswerAction.questionAnswerActionForm.employeeIdInputText}">
    <f:ajax event="blur" render="employeeIdMessage" />
</h:inputText>
<h:message id="employeeIdMessage" errorClass="deg-msg-error" infoClass="deg-msg-info" for="employeeId"></h:message>

For some reason though, when I attempt to apply just about the same code on another facelet (the difference being that it is for a managerId instead of employeeId), when the setter occurs that gets triggered in the ajax call (setEmployeeId in the above call), my SessionScoped ManagedProperty is being flagged as null in the method. The ManagedProperty is actually a SessionScoped ManagedBean itself.

Can't figure out why this code is working fine in one page but then in another facelet when the 'set' occurs the ManagedProperty is showing up as null...

Upvotes: 0

Views: 153

Answers (1)

BalusC
BalusC

Reputation: 1108852

The binding attribute causes problems when bound to a property of a bean in a scope broader than the request scope, such as the session scope, because you're then basically sharing physically the same JSF component state across multiple page includes, page views and browser tabs/windows throughout the entire session. Get rid of it or rebind it to a request scoped bean.

The UIInput field is then passed to a method that adds a FacesContext message to the specified UIComponent (in this case it's the UIInput field

Just use a normal Validator or Converter depending on the functional requirement and register it on the component. If you throw a ValidatorException or ConverterException, then the wrapped FacesMessage will automagically end up in the right place. This way you don't need the component binding any more.

setting the manager bean to be RequestScoped fixed it...this is strange because when I set my employee managed bean to be SessionScoped, the ajax functionality started working

This is answered by points 4 and 5 in commandButton/commandLink/ajax action/listener method not invoked or input value not updated.

Upvotes: 2

Related Questions