On 10/2/06, Baker,Jonathan <[EMAIL PROTECTED]> wrote:
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.
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.
Craig
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

