You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Troy Bull <tr...@gmail.com> on 2006/10/02 20:54:41 UTC

Backing Bean Constructor

Greetings

I have a faces page that allows the user to edit a table in the 
database.  What I want to do is in the constructor of the backing bean 
load up some data from the database so that it can be displayed and 
edited on the faces page.  Is this considered bad form?  Also just off 
the top of my head is this a place I can also load collections that will 
be used for drop down list boxes?

Please help, I don't want to do things the wrong way...

Troy

Re: Backing Bean Constructor

Posted by Craig McClanahan <cr...@apache.org>.
On 10/2/06, Baker,Jonathan <Ba...@oclc.org> wrote:
>
> Troy,
>
> You may be interested in looking at Shale. (http://shale.apache.org/)
> The shale view controller is an interface that gives you some extra
> plug-in points in the JSF lifecycle.  One of these points is an init
> call, which is guaranteed to run after your managed bean has been
> constructed and had all of its dependencies injected.


In addition to the advantage you cite above, there's actually another ... if
your code is buggy and throws an exception, it tends to be much easier to
understand the stack trace when you use the init() method.

For your list boxes you could also use the init method, or if you are
> using the <f:selectItems> tag to populate your pulldowns, you could use
> any method you wanted to populate the pulldowns as long as that method
> was bound to the selectItems tag.  This method will get called everytime
> you render though, so you would probably only want to do that if your
> lists were changing every request.


If you are using Shale for the init() method on backing beans, it's also
feasible to use the same concept on session scoped and application scoped
managed beans, if they extend AbstractSessionBean/AbstractApplicationBean.
I tend to build my dropdown lists in the init() methods of one of these
beans, in application scope (if the options are the same for all users) or
in session scope (if they are user specific).

Example ... assume you have a list of product types that is to fill a
dropdown, and the list is the same for everyone.  You could have an
application scoped managed bean like this (under managed bean name "lists"):

    public class SelectItemLists extends AbstractApplicationBean {

        public void init() {
            ... populate the productTypes property ...
        }

        private SelectItem[] productTypes;

        public SelectItem[] productTypes() {
            return productTypes;
        }

        ...

    }

Now, you can reference this in your component:

    <h:selectOneMenu ...>
        <f:selectItems value="#{lists.productTypes}"/>
    </h:selectOneMenu>

and, as a side effect of evaluating the binding expression, the first time
this bean is accessed it will populate the lists and make them available.


JB


Craig

RE: Backing Bean Constructor

Posted by "Baker,Jonathan" <Ba...@oclc.org>.
Troy,

You may be interested in looking at Shale. (http://shale.apache.org/)
The shale view controller is an interface that gives you some extra
plug-in points in the JSF lifecycle.  One of these points is an init
call, which is guaranteed to run after your managed bean has been
constructed and had all of its dependencies injected.

For your list boxes you could also use the init method, or if you are
using the <f:selectItems> tag to populate your pulldowns, you could use
any method you wanted to populate the pulldowns as long as that method
was bound to the selectItems tag.  This method will get called everytime
you render though, so you would probably only want to do that if your
lists were changing every request.


JB

-----Original Message-----
From: Jeff Bischoff [mailto:jbischoff@klkurz.com] 
Sent: Monday, October 02, 2006 3:18 PM
To: MyFaces Discussion
Subject: Re: Backing Bean Constructor

Troy,

Yes, you can use the constructor for these tasks, if it suits you. I
would be cautious though, what I put in a managed-bean constructor. Keep
in mind that the class will be automatically instantiated by the
framework any time it is looked up or otherwise referenced. I've gotten
myself into trouble by over-using the constructors.

In our project, we do still use the contstructors to load any drop-down
lists that are rather static. For loading live data or dynamic
drop-downs, however, we steer clear and go in favor of a load() method
which is explicitly called as a result of the business logic set in
motion by the command action.

Our pattern is far from perfect - we're just adapting as we go. I'd
suggest you play around with it a bit, and see what works best. Don't
rule out the use of the constructor, but don't abuse it either! ;)

Regards,

Jeff Bischoff
Kenneth L Kurz & Associates, Inc.

Troy Bull wrote:
> Greetings
> 
> I have a faces page that allows the user to edit a table in the 
> database.  What I want to do is in the constructor of the backing bean

> load up some data from the database so that it can be displayed and 
> edited on the faces page.  Is this considered bad form?  Also just off

> the top of my head is this a place I can also load collections that 
> will be used for drop down list boxes?
> 
> Please help, I don't want to do things the wrong way...
> 
> Troy
> 
> 
> 



Re: Backing Bean Constructor

Posted by Jeff Bischoff <jb...@klkurz.com>.
Troy,

Yes, you can use the constructor for these tasks, if it suits you. I 
would be cautious though, what I put in a managed-bean constructor. Keep 
in mind that the class will be automatically instantiated by the 
framework any time it is looked up or otherwise referenced. I've gotten 
myself into trouble by over-using the constructors.

In our project, we do still use the contstructors to load any drop-down 
lists that are rather static. For loading live data or dynamic 
drop-downs, however, we steer clear and go in favor of a load() method 
which is explicitly called as a result of the business logic set in 
motion by the command action.

Our pattern is far from perfect - we're just adapting as we go. I'd 
suggest you play around with it a bit, and see what works best. Don't 
rule out the use of the constructor, but don't abuse it either! ;)

Regards,

Jeff Bischoff
Kenneth L Kurz & Associates, Inc.

Troy Bull wrote:
> Greetings
> 
> I have a faces page that allows the user to edit a table in the 
> database.  What I want to do is in the constructor of the backing bean 
> load up some data from the database so that it can be displayed and 
> edited on the faces page.  Is this considered bad form?  Also just off 
> the top of my head is this a place I can also load collections that will 
> be used for drop down list boxes?
> 
> Please help, I don't want to do things the wrong way...
> 
> Troy
> 
> 
>