You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Georg Füchsle <gi...@googlemail.com> on 2009/04/27 11:22:45 UTC

may I use session scoped beans in an application scoped bean's method?

Hallo,

I found such things in my code:

In an static method of an application scoped bean I fetch a session
scoped bean and read some data from it.
It seems to work. But is it really allowed?

Georg

Re: may I use session scoped beans in an application scoped bean's method?

Posted by Anton Gavazuk <an...@gmail.com>.
Hi Matthias, Catagay,

please describe your approach with DI, especially interested in
Master-Detail scenario - how would you do that with DI

for example:
SearchBeanA returns the list of BeanA - and showing them in h:dataTable ()
now user chooses an item in table and a detail page must be shown -
properties of BeanA are linked to the detail page.
How would you load through the DI the BeanA from SearchBeanA?

Thanks,
Anton


2009/4/27 Matthias Wessendorf <ma...@apache.org>

> On Mon, Apr 27, 2009 at 12:09 PM, Georg Füchsle <gi...@googlemail.com>
> wrote:
> > Thanks Matthias,
> >
> > I do not use EL API, and I don't understand what you mean by
> DI-container.
>
> in fact, you are using the API:
>
> javax.faces.el.ValueBinding vb =app....
>
> cleaner solution is using some DI container
> (e.g. spring)
>
> >
> >
> > But I can show you the following example:
> >
> > I have an application scoped bean and a session scoped bean
> (MbSessionBean)
> > In my application scoped bean I found code like that:
> >
> >         public String getImageUrl()
> >         {
> >                 int layout =
> (MbSessionBean)getManagedBean("mbsessionbean").getLayout();
> >
> >                 if(layout == 1)
> >                         return "/pages/imageone.jpg";
> >                 else
> >                         return "/pages/imagetwo.jpg";
> >         }
> >
> >
> >
> >        protected static final Object getManagedBean(String beanName)
> >        {
> >
> >                Object ob;
> >                FacesContext facesContext =
> FacesContext.getCurrentInstance();
> >                Application app = facesContext.getApplication();
> >                javax.faces.el.ValueBinding vb
> =app.createValueBinding("#{"+beanName+"}");
> >                ob = vb.getValue(facesContext);
> >                return ob;
> >        }
> >
> >
> >
> > Of course this is not nice code, because I ask user dependent data
> > from a method of an application scoped bean.
> > But I wonder how it can work.
> >
> > How does the application bean know which session scoped bean it should
> ask?
>
> it uses the one from the current user. Using the API like you do is a
> giant hack.
> I'd not do it, even it works... This code will cause errors and "fun"
> in the future
> of the application.
>
> >
> > Obviously it works. But does it only work by chance, ( for example
> > because there has never been 2 or more session beans with different
> > data at the same time...) or does it really work?
> >
> > If it works (even the code is ugly) I could let it this way now and
> > clean it another time....
> >
> >
> > Cheers Georg
> >
> >
> > 2009/4/27 Matthias Wessendorf <ma...@apache.org>:
> >> I'd not do that...
> >>
> >> generally fetching beans by using the EL API is not that good.
> >> Use a real DI container for that
> >>
> >> -M
> >>
> >> On Mon, Apr 27, 2009 at 11:22 AM, Georg Füchsle <
> giofyxle@googlemail.com> wrote:
> >>> Hallo,
> >>>
> >>> I found such things in my code:
> >>>
> >>> In an static method of an application scoped bean I fetch a session
> >>> scoped bean and read some data from it.
> >>> It seems to work. But is it really allowed?
> >>>
> >>> Georg
> >>>
> >>
> >>
> >>
> >> --
> >> Matthias Wessendorf
> >>
> >> blog: http://matthiaswessendorf.wordpress.com/
> >> sessions: http://www.slideshare.net/mwessendorf
> >> twitter: http://twitter.com/mwessendorf
> >>
> >
>
>
>
> --
> Matthias Wessendorf
>
> blog: http://matthiaswessendorf.wordpress.com/
> sessions: http://www.slideshare.net/mwessendorf
> twitter: http://twitter.com/mwessendorf
>

Re: may I use session scoped beans in an application scoped bean's method?

Posted by Matthias Wessendorf <ma...@apache.org>.
On Mon, Apr 27, 2009 at 12:09 PM, Georg Füchsle <gi...@googlemail.com> wrote:
> Thanks Matthias,
>
> I do not use EL API, and I don't understand what you mean by DI-container.

in fact, you are using the API:

javax.faces.el.ValueBinding vb =app....

cleaner solution is using some DI container
(e.g. spring)

>
>
> But I can show you the following example:
>
> I have an application scoped bean and a session scoped bean (MbSessionBean)
> In my application scoped bean I found code like that:
>
>         public String getImageUrl()
>         {
>                 int layout = (MbSessionBean)getManagedBean("mbsessionbean").getLayout();
>
>                 if(layout == 1)
>                         return "/pages/imageone.jpg";
>                 else
>                         return "/pages/imagetwo.jpg";
>         }
>
>
>
>        protected static final Object getManagedBean(String beanName)
>        {
>
>                Object ob;
>                FacesContext facesContext = FacesContext.getCurrentInstance();
>                Application app = facesContext.getApplication();
>                javax.faces.el.ValueBinding vb =app.createValueBinding("#{"+beanName+"}");
>                ob = vb.getValue(facesContext);
>                return ob;
>        }
>
>
>
> Of course this is not nice code, because I ask user dependent data
> from a method of an application scoped bean.
> But I wonder how it can work.
>
> How does the application bean know which session scoped bean it should ask?

it uses the one from the current user. Using the API like you do is a
giant hack.
I'd not do it, even it works... This code will cause errors and "fun"
in the future
of the application.

>
> Obviously it works. But does it only work by chance, ( for example
> because there has never been 2 or more session beans with different
> data at the same time...) or does it really work?
>
> If it works (even the code is ugly) I could let it this way now and
> clean it another time....
>
>
> Cheers Georg
>
>
> 2009/4/27 Matthias Wessendorf <ma...@apache.org>:
>> I'd not do that...
>>
>> generally fetching beans by using the EL API is not that good.
>> Use a real DI container for that
>>
>> -M
>>
>> On Mon, Apr 27, 2009 at 11:22 AM, Georg Füchsle <gi...@googlemail.com> wrote:
>>> Hallo,
>>>
>>> I found such things in my code:
>>>
>>> In an static method of an application scoped bean I fetch a session
>>> scoped bean and read some data from it.
>>> It seems to work. But is it really allowed?
>>>
>>> Georg
>>>
>>
>>
>>
>> --
>> Matthias Wessendorf
>>
>> blog: http://matthiaswessendorf.wordpress.com/
>> sessions: http://www.slideshare.net/mwessendorf
>> twitter: http://twitter.com/mwessendorf
>>
>



-- 
Matthias Wessendorf

blog: http://matthiaswessendorf.wordpress.com/
sessions: http://www.slideshare.net/mwessendorf
twitter: http://twitter.com/mwessendorf

Re: may I use session scoped beans in an application scoped bean's method?

Posted by Cagatay Civici <ca...@gmail.com>.
Instead of binding your application scoped bean code to access session, why
don't you just pass the layout param to the getImageUrl method.

Also in JSF you can't inject managed beans with short scopes to longer ones,
so you can't inject a request bean to a session scoped bean using jsf ioc,
or session bean to an application scoped bean. Given that although you can
hack it and make it work programatically in your case, it conflicts with the
jsf nature.

On Mon, Apr 27, 2009 at 11:09 AM, Georg Füchsle <gi...@googlemail.com>wrote:

> Thanks Matthias,
>
> I do not use EL API, and I don't understand what you mean by DI-container.
>
>
> But I can show you the following example:
>
> I have an application scoped bean and a session scoped bean (MbSessionBean)
> In my application scoped bean I found code like that:
>
>         public String getImageUrl()
>         {
>                 int layout =
> (MbSessionBean)getManagedBean("mbsessionbean").getLayout();
>
>                 if(layout == 1)
>                         return "/pages/imageone.jpg";
>                 else
>                         return "/pages/imagetwo.jpg";
>         }
>
>
>
>        protected static final Object getManagedBean(String beanName)
>        {
>
>                Object ob;
>                FacesContext facesContext =
> FacesContext.getCurrentInstance();
>                Application app = facesContext.getApplication();
>                javax.faces.el.ValueBinding vb
> =app.createValueBinding("#{"+beanName+"}");
>                ob = vb.getValue(facesContext);
>                return ob;
>        }
>
>
>
> Of course this is not nice code, because I ask user dependent data
> from a method of an application scoped bean.
> But I wonder how it can work.
>
> How does the application bean know which session scoped bean it should ask?
>
> Obviously it works. But does it only work by chance, ( for example
> because there has never been 2 or more session beans with different
> data at the same time...) or does it really work?
>
> If it works (even the code is ugly) I could let it this way now and
> clean it another time....
>
>
> Cheers Georg
>
>
> 2009/4/27 Matthias Wessendorf <ma...@apache.org>:
> > I'd not do that...
> >
> > generally fetching beans by using the EL API is not that good.
> > Use a real DI container for that
> >
> > -M
> >
> > On Mon, Apr 27, 2009 at 11:22 AM, Georg Füchsle <gi...@googlemail.com>
> wrote:
> >> Hallo,
> >>
> >> I found such things in my code:
> >>
> >> In an static method of an application scoped bean I fetch a session
> >> scoped bean and read some data from it.
> >> It seems to work. But is it really allowed?
> >>
> >> Georg
> >>
> >
> >
> >
> > --
> > Matthias Wessendorf
> >
> > blog: http://matthiaswessendorf.wordpress.com/
> > sessions: http://www.slideshare.net/mwessendorf
> > twitter: http://twitter.com/mwessendorf
> >
>

Re: may I use session scoped beans in an application scoped bean's method?

Posted by Georg Füchsle <gi...@googlemail.com>.
Thanks Matthias,

I do not use EL API, and I don't understand what you mean by DI-container.


But I can show you the following example:

I have an application scoped bean and a session scoped bean (MbSessionBean)
In my application scoped bean I found code like that:

	 public String getImageUrl()
	 {
		 int layout = (MbSessionBean)getManagedBean("mbsessionbean").getLayout();

		 if(layout == 1)
			 return "/pages/imageone.jpg";
		 else
			 return "/pages/imagetwo.jpg";
	 }



	protected static final Object getManagedBean(String beanName)
	{
		
		Object ob;
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Application app = facesContext.getApplication();
		javax.faces.el.ValueBinding vb =app.createValueBinding("#{"+beanName+"}");
		ob = vb.getValue(facesContext);
		return ob;
	}



Of course this is not nice code, because I ask user dependent data
from a method of an application scoped bean.
But I wonder how it can work.

How does the application bean know which session scoped bean it should ask?

Obviously it works. But does it only work by chance, ( for example
because there has never been 2 or more session beans with different
data at the same time...) or does it really work?

If it works (even the code is ugly) I could let it this way now and
clean it another time....


Cheers Georg


2009/4/27 Matthias Wessendorf <ma...@apache.org>:
> I'd not do that...
>
> generally fetching beans by using the EL API is not that good.
> Use a real DI container for that
>
> -M
>
> On Mon, Apr 27, 2009 at 11:22 AM, Georg Füchsle <gi...@googlemail.com> wrote:
>> Hallo,
>>
>> I found such things in my code:
>>
>> In an static method of an application scoped bean I fetch a session
>> scoped bean and read some data from it.
>> It seems to work. But is it really allowed?
>>
>> Georg
>>
>
>
>
> --
> Matthias Wessendorf
>
> blog: http://matthiaswessendorf.wordpress.com/
> sessions: http://www.slideshare.net/mwessendorf
> twitter: http://twitter.com/mwessendorf
>

Re: may I use session scoped beans in an application scoped bean's method?

Posted by Matthias Wessendorf <ma...@apache.org>.
I'd not do that...

generally fetching beans by using the EL API is not that good.
Use a real DI container for that

-M

On Mon, Apr 27, 2009 at 11:22 AM, Georg Füchsle <gi...@googlemail.com> wrote:
> Hallo,
>
> I found such things in my code:
>
> In an static method of an application scoped bean I fetch a session
> scoped bean and read some data from it.
> It seems to work. But is it really allowed?
>
> Georg
>



-- 
Matthias Wessendorf

blog: http://matthiaswessendorf.wordpress.com/
sessions: http://www.slideshare.net/mwessendorf
twitter: http://twitter.com/mwessendorf