You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by James Flynn <ja...@yahoo.com> on 2005/10/10 01:59:00 UTC

accessing bound data in MyFaces

Hi All,

This seems like a simple problem, but we have been
unable to find a solution in the documentation. We are
new to JSF, so please excuse us if the answer is
obvious to someone with more experience:

Basically we have a form whose fields are all bound to
a data bean, which is a managed bean. We do not want
to put the action code right in this bean, so we have
created another managed bean for that, call it the
"controller". The problem is thus: how do we gain
access to the "data bean" from the controller?

We were originally using the following code to do
this:

DataBean dataBean =
(DataBean)FacesUtils.getManagedBean("DataBean");

But when we switched from the JSF reference
implementation to MyFaces, this did not work anymore
(it gave us back a new unbound bean instead, with all
properties set to null). I am assuming we are doing
something wrong, or is this a bug in MyFaces? What is
the recommended way to do this?

Thanks in advance for any and all help!

Cheers,
Ossie

Here is the complete example:

------------------------------------- JSP:

<%@taglib uri="http://java.sun.com/jsf/core"
prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html"
prefix="h"%>
<html>
<body>
<f:view>
	<h:form id="dataForm">
	<h:inputText value="#{DataBean.field1}"/>
	<h:inputText value="#{DataBean.field2}"/>
	<h:commandButton id="saveButton" value="Save"
action="#{Controller.saveDataBean}"/>
	</h:form>
</f:view>
</body>
</html>

------------------------------------- Faces Config:

<managed-bean>
<managed-bean-name>DataBean</managed-bean-name>	
<managed-bean-class>our.data.package.DataBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

<managed-bean>
<managed-bean-name>Controller</managed-bean-name>
<managed-bean-class>our.controller.package.DataBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>

------------------------------------- Bean:

public class DataBean {
	private String field1;
	private String field2;
	// (getters and setters snipped)
}

------------------------------------- Controller:

public class Controller {

public void saveDataBean() {
	// is this not the right way to access the data bean?
	DataBean dataBean  =
(DataBean)FacesUtils.getManagedBean("DataBean");	
	// view the bean data - it's all null in MyFaces!
	System.out.println("data bean properties:");
	System.out.println(dataBean.getField1());
	System.out.println(dataBean.getField2());
}
}

[end]




		
__________________________________ 
Yahoo! Music Unlimited 
Access over 1 million songs. Try it free.
http://music.yahoo.com/unlimited/

Re: accessing bound data in MyFaces

Posted by Mike Kienenberger <mk...@gmail.com>.
When you say you're using the jsf-spring integration, are you using
the jsf-spring sourceforge project?   If not, I'd recommend switching
over to that as this should allow you to have two-way sharing of
registered beans.

On 10/9/05, James Flynn <ja...@yahoo.com> wrote:
>
> Hi Simon,
>
> Thanks so much for your help! I tried your solution outlined below, but got an
> exception:
>
> Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No
> bean named '#{dataBean}' is defined:
> org.springframework.beans.factory.support.DefaultListableBeanFactory defining
> beans
>
> We are using the spring/jsf integration, so it seems to me that Spring is
> "injecting" itself into this managed bean facility. So it seems we must
> register our DataBean with Spring for this to work. But I'm guessing we need to
> continue to register it in faces-config as well, as the DataBean is a "backing
> bean" in the JSF side of things. Does this sound right to you?
>
> Kind regards,
> James
>
> --- Simon Kitching <sk...@obsidium.com> wrote:
>
> > James Flynn wrote:
> > > Hi All,
> > >
> > > This seems like a simple problem, but we have been
> > > unable to find a solution in the documentation. We are
> > > new to JSF, so please excuse us if the answer is
> > > obvious to someone with more experience:
> > >
> > > Basically we have a form whose fields are all bound to
> > > a data bean, which is a managed bean. We do not want
> > > to put the action code right in this bean, so we have
> > > created another managed bean for that, call it the
> > > "controller". The problem is thus: how do we gain
> > > access to the "data bean" from the controller?
> > >
> > > We were originally using the following code to do
> > > this:
> > >
> > > DataBean dataBean =
> > > (DataBean)FacesUtils.getManagedBean("DataBean");
> > >
> > > But when we switched from the JSF reference
> > > implementation to MyFaces, this did not work anymore
> > > (it gave us back a new unbound bean instead, with all
> > > properties set to null). I am assuming we are doing
> > > something wrong, or is this a bug in MyFaces? What is
> > > the recommended way to do this?
> >
> > You can use "injection" defined in the managed bean section to connect
> > the beans:
> >
> > <managed-bean>
> > <managed-bean-name>Controller</managed-bean-name>
> > ...
> > <managed-property>
> >    <description>databean object</description>
> >    <property-name>dataBean</property-name>
> >    <property-class>our.data.package.DataBean</property-class>
> >    <value>#{dataBean></value>
> > </managed-property>
> >
> >
> > Method Controller.setDataBean will then be called with the databean as a
> > parameter before any ActionListener callbacks occur.
> >
> > Regards,
> >
> > Simon
> >
>
>
>
>
>
>
>
> __________________________________
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> http://mail.yahoo.com
>

Re: accessing bound data in MyFaces

Posted by James Flynn <ja...@yahoo.com>.
> Well, there is really no such thing as a "backing bean" in JSF. There 
> are just beans that are stored into the standard servlet 
> request/session/application scope hashmaps. EL expressions can then 
> refer to them.
> 
> The managed bean stuff is really very simple. Whenever the EL expression 
> resolved is asked to evaluate an expression like "#{objname}" it looks 
> in the request/session/application. If no such object is found then it 
> asks the managed bean manager to create the object. And whenever the 
> managed bean manager finds a <managed-property> tag while trying to 
> create an object, it passes it to the EL resolver to compute the value 
> before calling the appropriate setter. [any circular references cause an 
> error to be reported].

Gotcha - that makes sense. I think we were making out to be more difficult than
it actually is. 

> 
> So I'm not sure why the spring stuff is involved here at all.
> 
> Unfortunately I'm using JSF on J2EE, not on Spring so I can't shed any 
> more light on your spring-related problem.

Fair enough - I will harass the Spring folks about that one ;)

> 
> Regards,
> 
> Simon
> 
Thanks so much for your help!

Kind regards,
James


	
		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com

Re: accessing bound data in MyFaces

Posted by Simon Kitching <sk...@obsidium.com>.
James Flynn wrote:
> Hi Simon,
> 
> Thanks so much for your help! I tried your solution outlined below, but got an
> exception:
> 
> Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No
> bean named '#{dataBean}' is defined:
> org.springframework.beans.factory.support.DefaultListableBeanFactory defining
> beans 
> 
> We are using the spring/jsf integration, so it seems to me that Spring is
> "injecting" itself into this managed bean facility. So it seems we must
> register our DataBean with Spring for this to work. But I'm guessing we need to
> continue to register it in faces-config as well, as the DataBean is a "backing
> bean" in the JSF side of things. Does this sound right to you?

Well, there is really no such thing as a "backing bean" in JSF. There 
are just beans that are stored into the standard servlet 
request/session/application scope hashmaps. EL expressions can then 
refer to them.

The managed bean stuff is really very simple. Whenever the EL expression 
resolved is asked to evaluate an expression like "#{objname}" it looks 
in the request/session/application. If no such object is found then it 
asks the managed bean manager to create the object. And whenever the 
managed bean manager finds a <managed-property> tag while trying to 
create an object, it passes it to the EL resolver to compute the value 
before calling the appropriate setter. [any circular references cause an 
error to be reported].

So I'm not sure why the spring stuff is involved here at all.

Unfortunately I'm using JSF on J2EE, not on Spring so I can't shed any 
more light on your spring-related problem.

Regards,

Simon

Re: accessing bound data in MyFaces

Posted by James Flynn <ja...@yahoo.com>.
Hi Simon,

Thanks so much for your help! I tried your solution outlined below, but got an
exception:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named '#{dataBean}' is defined:
org.springframework.beans.factory.support.DefaultListableBeanFactory defining
beans 

We are using the spring/jsf integration, so it seems to me that Spring is
"injecting" itself into this managed bean facility. So it seems we must
register our DataBean with Spring for this to work. But I'm guessing we need to
continue to register it in faces-config as well, as the DataBean is a "backing
bean" in the JSF side of things. Does this sound right to you?

Kind regards,
James

--- Simon Kitching <sk...@obsidium.com> wrote:

> James Flynn wrote:
> > Hi All,
> > 
> > This seems like a simple problem, but we have been
> > unable to find a solution in the documentation. We are
> > new to JSF, so please excuse us if the answer is
> > obvious to someone with more experience:
> > 
> > Basically we have a form whose fields are all bound to
> > a data bean, which is a managed bean. We do not want
> > to put the action code right in this bean, so we have
> > created another managed bean for that, call it the
> > "controller". The problem is thus: how do we gain
> > access to the "data bean" from the controller?
> > 
> > We were originally using the following code to do
> > this:
> > 
> > DataBean dataBean =
> > (DataBean)FacesUtils.getManagedBean("DataBean");
> > 
> > But when we switched from the JSF reference
> > implementation to MyFaces, this did not work anymore
> > (it gave us back a new unbound bean instead, with all
> > properties set to null). I am assuming we are doing
> > something wrong, or is this a bug in MyFaces? What is
> > the recommended way to do this?
> 
> You can use "injection" defined in the managed bean section to connect 
> the beans:
> 
> <managed-bean>
> <managed-bean-name>Controller</managed-bean-name>
> ...
> <managed-property>			
>    <description>databean object</description>
>    <property-name>dataBean</property-name>
>    <property-class>our.data.package.DataBean</property-class>
>    <value>#{dataBean></value>
> </managed-property>			
> 
> 
> Method Controller.setDataBean will then be called with the databean as a 
> parameter before any ActionListener callbacks occur.
> 
> Regards,
> 
> Simon
> 





	
		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com

Re: accessing bound data in MyFaces

Posted by Simon Kitching <sk...@obsidium.com>.
James Flynn wrote:
> Hi All,
> 
> This seems like a simple problem, but we have been
> unable to find a solution in the documentation. We are
> new to JSF, so please excuse us if the answer is
> obvious to someone with more experience:
> 
> Basically we have a form whose fields are all bound to
> a data bean, which is a managed bean. We do not want
> to put the action code right in this bean, so we have
> created another managed bean for that, call it the
> "controller". The problem is thus: how do we gain
> access to the "data bean" from the controller?
> 
> We were originally using the following code to do
> this:
> 
> DataBean dataBean =
> (DataBean)FacesUtils.getManagedBean("DataBean");
> 
> But when we switched from the JSF reference
> implementation to MyFaces, this did not work anymore
> (it gave us back a new unbound bean instead, with all
> properties set to null). I am assuming we are doing
> something wrong, or is this a bug in MyFaces? What is
> the recommended way to do this?

You can use "injection" defined in the managed bean section to connect 
the beans:

<managed-bean>
<managed-bean-name>Controller</managed-bean-name>
...
<managed-property>			
   <description>databean object</description>
   <property-name>dataBean</property-name>
   <property-class>our.data.package.DataBean</property-class>
   <value>#{dataBean></value>
</managed-property>			


Method Controller.setDataBean will then be called with the databean as a 
parameter before any ActionListener callbacks occur.

Regards,

Simon