You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by "Airton (Terra)" <li...@gmail.com> on 2005/11/13 15:22:05 UTC

value through beans

Hi,

how could I get a value from a bean class to another?

Ex.:

I have a class LoginBean with setName and getName. I need get "name"
on ListBean to query on a database. I've done:

public class ListBean {
...
LoginBean lb = new LoginBean();
String name = lb.getName();
...
}

but "name" comes "null".

Thanks.

Re: value through beans

Posted by Sean Schofield <se...@gmail.com>.
On 11/15/05, Airton (Terra) <li...@gmail.com> wrote:
> Thank you all !
>
> I've got success this way too:
>
> FacesContext fc = FacesContext.getCurrentInstance();
> ValueBinding vb = fc.getApplication().createValueBinding("#{loginBean.name}");
> name = (String) vb.getValue(fc);
>
> Pros? Cons?

I use this approach.  If you are using Shale View Controller there are
some handy methods that allow you to reduce this to just one line:
String name = (String)getValue("#{loginBean.name}");

If you are not using Shale ... you should check it out.  The next
thing you will want is a way to automatically initialize the beans
before your components use them and that's one of many ways where
Shale can help you.

> Regards,
> Airton

sean

Re: value through beans

Posted by Volker Weber <us...@weber-oldenburg.de>.
see:

http://www.mail-archive.com/users%40myfaces.apache.org/msg11973.html

Tim Davies wrote:
> Sorry, I missed the beginning of this. What were you trying to do?
> 
> 
> Airton (Terra) wrote:
> 
>> Thank you all !
>>
>> I've got success this way too:
>>
>> FacesContext fc = FacesContext.getCurrentInstance();
>> ValueBinding vb =
>> fc.getApplication().createValueBinding("#{loginBean.name}");
>> name = (String) vb.getValue(fc);
>>
>> Pros? Cons?
>>
>> Regards,
>> Airton
>>
>>
>> 2005/11/13, Simon Kitching <sk...@obsidium.com>:
>>  
>>
>>> Just FYI, I've added a section to the FAQ on the wiki using (slightly
>>> modified) copies of the solutions described by Volker and Mario.
>>>
>>> Regards,
>>>
>>> Simon
>>>
>>>
>>>   
> 
> 

-- 
Don't answer to From: address!
Mail to this account are droped if not recieved via mailinglist.
To contact me direct create the mail address by
concatenating my forename to my senders domain.

Re: value through beans

Posted by Tim Davies <ti...@ktsplc.com>.
Sorry, I missed the beginning of this. What were you trying to do?


Airton (Terra) wrote:

>Thank you all !
>
>I've got success this way too:
>
>FacesContext fc = FacesContext.getCurrentInstance();
>ValueBinding vb = fc.getApplication().createValueBinding("#{loginBean.name}");
>name = (String) vb.getValue(fc);
>
>Pros? Cons?
>
>Regards,
>Airton
>
>
>2005/11/13, Simon Kitching <sk...@obsidium.com>:
>  
>
>>Just FYI, I've added a section to the FAQ on the wiki using (slightly
>>modified) copies of the solutions described by Volker and Mario.
>>
>>Regards,
>>
>>Simon
>>
>>
>>    
>>

-- 
Tim Davies
Analyst Developer

KTS PLC: Service you can bank on
8th Floor, Finsbury Tower,
103-105 Bunhill Row,
London  EC1Y 8TY
tel: +44 (0)20 7256 2300
fax: +44 (0)20 7256 2301

email: tim.davies@ktsplc.com
web: http://www.ktsplc.com 


Re: value through beans

Posted by "Airton (Terra)" <li...@gmail.com>.
Thank you all !

I've got success this way too:

FacesContext fc = FacesContext.getCurrentInstance();
ValueBinding vb = fc.getApplication().createValueBinding("#{loginBean.name}");
name = (String) vb.getValue(fc);

Pros? Cons?

Regards,
Airton


2005/11/13, Simon Kitching <sk...@obsidium.com>:
> Just FYI, I've added a section to the FAQ on the wiki using (slightly
> modified) copies of the solutions described by Volker and Mario.
>
> Regards,
>
> Simon
>
>

Re: value through beans

Posted by Simon Kitching <sk...@obsidium.com>.
Just FYI, I've added a section to the FAQ on the wiki using (slightly 
modified) copies of the solutions described by Volker and Mario.

Regards,

Simon


Re: value through beans

Posted by Mario Ivankovits <ma...@ops.co.at>.
Airton (Terra) wrote
> LoginBean lb = new LoginBean();
> String name = lb.getName();
>
> but "name" comes "null".
>   

In your case LoginBean is "session" scoped, isnt it.

Then you can do something like this:

        <managed-bean>
                <managed-bean-name>LoginBean</managed-bean-name>
                <managed-bean-class>fqn.to.LoginBean</managed-bean-class>
                <managed-bean-scope>session</managed-bean-scope>
        </managed-bean>

        <managed-bean>
                <managed-bean-name>ListBean</managed-bean-name>
                <managed-bean-class>fqn.to.ListBean</managed-bean-class>
                <managed-bean-scope>request</managed-bean-scope>
                <managed-property>
                        <property-name>loginBean</property-name>
                        <value>#{LoginBean}</value>
                </managed-property>
        </managed-bean>

having a get/setLoginBean in your list bean allows faces to provide your 
ListBean automatically with the LoginBean session instance.
No need to have faces specific code in your beans for this!

---
Mario


Re: value through beans

Posted by Volker Weber <us...@weber-oldenburg.de>.
Hi,

Airton (Terra) wrote:
> Hi,
> 
> how could I get a value from a bean class to another?
> 
> Ex.:
> 
> I have a class LoginBean with setName and getName. I need get "name"
> on ListBean to query on a database. I've done:
> 
> public class ListBean {
> ...
> LoginBean lb = new LoginBean();
Here you are instanciating a NEW instance of your class LoginBean. If
you don't initialzing the field name in constructor you can't expect to
get a value.

Try this (assuming the name of your LoginBean is "LoginBean"):

FacesContext facesContext = FacesContext.getCurrentInstance();
LoginBean lb
    = (LoginBean)facesContext.getApplication().getVariableResolver()
      .resolveVariable(facesContext, "LoginBean");

> String name = lb.getName();
> ...
> }
> 
> but "name" comes "null".
> 
> Thanks.
> 

Regards
  Volker
-- 
Don't answer to From: address!
Mail to this account are droped if not recieved via mailinglist.
To contact me direct create the mail address by
concatenating my forename to my senders domain.