myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonathan Harley <...@parkplatz.net>
Subject Re: Variable resolution syntax
Date Thu, 26 Apr 2007 14:56:13 GMT
Mike Kienenberger wrote:
> Well, the easiest thing to do would be to use
> 
> <managed-bean>
>    <managed-bean-name>commandsController</managed-bean-name>
>    
> <managed-bean-class>com.csfb.fao.clr.maintenance.controller.SearchCommandsController</managed-bean-class>

> 
>    <managed-bean-scope>none</managed-bean-scope>
>    <managed-property>
>        <property-name>categorySelector</property-name>
>        <value>#{categorySelector}</value>
>    </managed-property>
> </managed-bean>
> 
> However, I must admit that I've never understood the use of scope=none.

I think it's there for encapsulation, since it means you can change
the type of a managed bean with scope 'none' in the JSF bean configs,
and the new type will get injected into all the places that use that
bean. Obviously you'd need a lot of recoding if you just let all
the users of that class create it with 'new'.

Apart from that, of course it lets you combine a bean where you
want a new copy every time, with each copy having other manged beans
injected into it. But I've never come across a case where I wanted
that.

> But if you want to hard-code references to your beans using java code,
> it can be done like this:
> 
>        ValueBinding binding =
> FacesContext.getCurrentInstance().getApplication().createValueBinding("#{categorySelector}");

> 
>        Object categorySelector =
> binding.getValue(FacesContext.getCurrentInstance());
> 
> Or like this:
> 
>        Object categorySelector =
> FacesContext.getCurrentInstance().getApplication().getVariableResolver().resolveVariable(FacesContext.getCurrentInstance(),

> 
> "categorySelector");

It's worth noting that:
(1) Application.createValueBinding() and .getVariableResolver() are
both deprecated in JSF 1.2 in favour of methods that use the new
unified EL; and
(2) dependency injection (the first way, using the managed bean
XML file) is widely considered best practice since it avoids
coupling your class to external APIs such as the JSF API, and
for the reason why that's such a bad idea, see (1)!


Jonathan.
-- 
.....................................................................
           Dr Jonathan Harley   .
                                .   Email: jon@parkplatz.net
            Zac Parkplatz Ltd   .   Office Telephone: 024 7633 1375
            www.parkplatz.net   .   Mobile: 079 4116 0423

Mime
View raw message