users@javaserverfaces.java.net

Re: At which Managed Bean Lifecycle Phase does Injection happen?

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Tue, 22 May 2007 18:02:24 -0700

Todd Patrick wrote:
> I really just confused the heck out of myself with this one.
>
> I was looking at the Core JavaServer Faces Second Edition book and the
> example had the afterPhase attribute set to a method in a backing bean
> (with the correct signature)?
>
> The backing bean doesn't implement PhaseListener???
>
No, the bean provides methods to match the signatures outlined in the
TLD docs.
> Also, since I'm in my managed bean - how do I look up the managed bean
> method I want to call in my afterPhase method?
>
> For example (leaving out getter and setters):
>
> public Class MyBean {
>
> @EJB private MySessionLocal mySession;
> private ArrayList<SelectItem> listValues = new ArrayList<SelectItem>();
> public String inputFieldArg;
>
> public void afterPhase(PhaseEvent event) {
> this.buildListValues(this.inputFieldArg);
> }
>
> public ArrayList buildListValues(String arg){
> listValues = mySession.getServerSideListBuilder(arg);
> }
> }
>
> Can I call my "buildListValues" method from my afterPhase method as
> above, with just a "this." syntax?
>
I'd probably have a separate bean for the phase listener methods.

As to how, use the Application object to create a ValueExpression that
refers to the bean
you want to manipulate. Calling getValue() on said ValueExpression will
cause the bean
to be created and put into scope (if there is any).

> I'd appreciate any insight to properly use the f:view afterPhase
> attribute.
>
> Thanks,
>
> --Todd
>
>
>
> -----Original Message-----
> From: Ryan.Lubke_at_Sun.COM [mailto:Ryan.Lubke_at_Sun.COM]
> Sent: Monday, May 21, 2007 10:07 AM
> To: users_at_javaserverfaces.dev.java.net
> Subject: Re: At which Managed Bean Lifecycle Phase does Injection
> happen?
>
> Todd Patrick wrote:
>
>> Ryan:
>>
>> OK. I see that I can do either:
>>
>> <f:view beforePhase="#{}">
>>
>> or
>>
>> <f:view afterPhase="#{}">
>>
>> Is this what you meant by attaching a PhaseListener to the view?
>>
>> I couldn't find anything else.
>>
>> Thanks,
>>
>>
> Yes, that is what I was referring to.
>
>> --Todd
>>
>> -----Original Message-----
>> From: Ryan.Lubke_at_Sun.COM [mailto:Ryan.Lubke_at_Sun.COM]
>> Sent: Saturday, May 19, 2007 10:59 AM
>> To: users_at_javaserverfaces.dev.java.net
>> Subject: Re: At which Managed Bean Lifecycle Phase does Injection
>> happen?
>>
>> Todd Patrick wrote:
>>
>>
>>> Ryan, Jacob:
>>>
>>> Thank you for the information, but I'm stuck with an logic issue.
>>>
>>> Once I inject my EJB with:
>>>
>>> @EJB private CustomerSessionLocal customerSessionRemote;
>>>
>>> Do I assign "customerSessionRemote" to a managed property? Such as:
>>>
>>> <managed-property>
>>> <property-name>currentCustomer</property-name>
>>>
>>>
>>>
> <property-class>com.dtn.bean.customer.CustomerSessionLocal</property-c
>
>>> la
>>> ss>
>>> <null-value/>
>>> </managed-property>
>>>
>>> So I can access a method in my "customerSessionRemote" EJB reference?
>>>
>>> Bottom line is, I need:
>>>
>>> 1.) Inject or Lookup an EJB
>>> 2.) Each time the page is loaded, call a method - pass a parameter to
>>>
>
>
>>> the method - in the EJB to return a List. The list is used to
>>>
> populate
>
>>>
>>>
>>
>>
>>> a h:selectManyListbox.
>>>
>>>
>>>
>> I'm assuming the parameters you wish to provide are coming from the
>> request.
>> If you want to do this each time the page is loaded, then you'll want
>>
> to
>
>> provide a PhaseListener and attach it to the view (this is a new
>>
> feature
>
>> added in 1.2 - check the tlddocs [1] for details).
>>
>> In your custom PL, you can lookup the ManagedBean, and then you can
>>
> call
>
>> some public method on the bean that passes the parameters to the EJB.
>>
>>
>>> I can't used a method with the @PostConstruct annotation since that
>>>
> is
>
>>>
>>>
>>
>>
>>> only called once and I can't call that method again.
>>>
>>> The 'pass a parameter to the method' to an EJB method each time the
>>> page is loaded is really throwing me for a loop.
>>>
>>> Once I have the EJB reference, I put the business logic in a void
>>> return type method and made a reference to the method in the bean
>>> constructor, but it throws a NullPointerException the first time the
>>> page is loaded because the constructor runs before the EJB
>>>
>>>
>> injection...
>>
>>
>>> Any thoughts on this?
>>>
>>> Thanks,
>>>
>>> --Todd
>>>
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: jacob_at_hookom.net [mailto:jacob_at_hookom.net]
>>> Sent: Friday, May 18, 2007 1:25 PM
>>> To: users_at_javaserverfaces.dev.java.net
>>> Subject: Re: At which Managed Bean Lifecycle Phase does Injection
>>> happen?
>>>
>>>
>>>
> http://weblogs.java.net/blog/jhook/archive/2007/05/jsf_12_ri_backi.htm
>
>>> l
>>>
>>>
>>> Todd Patrick wrote:
>>>
>>>
>>>
>>>> JSF 1.2_04
>>>>
>>>> At which Managed Bean Lifecycle Phase does Injection happen or does
>>>> Injection happen after all of the Lifecycle Phases have completed?
>>>>
>>>>
>>>>
>>>>
>>> Injection isn't associated with a phase, it will occur when the bean
>>> is first referenced and not already in scope.
>>>
>>>
>>>
>>>> If I inject an EJB with:
>>>>
>>>> @EJB private CustomerSessionLocal customerSessionRemote;
>>>>
>>>> I can do set-up with a method annotated with @PostConstruct, however
>>>>
>
>
>>>> I
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>> cannot use any parameters.
>>>>
>>>> I'd like to call a EJB method AND pass parameters each time the
>>>> managed bean is loaded for the JSF page being called.
>>>>
>>>> What is happening is the annotated method is called once - thus any
>>>> call to the page again still has the first call values, I need to
>>>> pass
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>> different parameters based on the user's selections on the JSF page.
>>>>
>>>> JSF --> [parameters] -->
>>>> Managed Bean --> [parameters] -->
>>>> Injected EJB --> [parameters] --> EJB method
>>>>
>>>> Is this scenario possible and how?
>>>>
>>>>
>>>>
>>>>
>>> If you use managed-properties, these would be initialized before the
>>> @PostConstruct callback was invoked and thus be available for use.
>>>
>>>
>>>
>>>> Thanks,
>>>>
>>>> --Todd
>>>>
>>>> -----------------------------------------
>>>> NOTICE: This email message is for the sole use of the intended
>>>> recipient(s) and may contain confidential and privileged
>>>>
> information.
>
>>>>
>>>>
>>
>>
>>>> Any unauthorized use, disclosure or distribution is prohibited. If
>>>> you
>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>> are not the intended recipient, please contact the sender by reply
>>>> email and destroy all copies of the original message.
>>>>
>>>>
>>>>
> ---------------------------------------------------------------------
>
>>>> To unsubscribe, e-mail:
>>>> users-unsubscribe_at_javaserverfaces.dev.java.net
>>>> For additional commands, e-mail:
>>>> users-help_at_javaserverfaces.dev.java.net
>>>>
>>>>
>>>>
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail:
>>>
> users-unsubscribe_at_javaserverfaces.dev.java.net
>
>>> For additional commands, e-mail:
>>> users-help_at_javaserverfaces.dev.java.net
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail:
>>>
> users-unsubscribe_at_javaserverfaces.dev.java.net
>
>>> For additional commands, e-mail:
>>> users-help_at_javaserverfaces.dev.java.net
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail:
>>>
> users-unsubscribe_at_javaserverfaces.dev.java.net
>
>>> For additional commands, e-mail:
>>> users-help_at_javaserverfaces.dev.java.net
>>>
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_javaserverfaces.dev.java.net
>> For additional commands, e-mail:
>>
> users-help_at_javaserverfaces.dev.java.net
>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_javaserverfaces.dev.java.net
>> For additional commands, e-mail:
>>
> users-help_at_javaserverfaces.dev.java.net
>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_javaserverfaces.dev.java.net
> For additional commands, e-mail: users-help_at_javaserverfaces.dev.java.net
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_javaserverfaces.dev.java.net
> For additional commands, e-mail: users-help_at_javaserverfaces.dev.java.net
>
>