Yeah,

Shale is indeed a nice enhancement for use cases like that.
init() is much *clearer* than defining stuff inside a constructor.

-Matthias

[EMAIL PROTECTED] wrote:
You might take a look at Shale.  It addresses this problem by defining a 
ViewController interfaces that defines extra lifecycle methods.
init()
destroy()
preprocess()
prerender()

The ViewController is a managed bean that implements this interface. There is also an abstract class that you can inherit from, AbstractViewController.

There is an assumed mapping between the managed beans name, the view id and the ViewController instance. It uses a filter and a phase listener to invoke these callback methods to a managed bean/ ViewController.

It’s pretty cool, http://wiki.apache.org/struts/StrutsShale

Gary




Sure. Lazy initialization is a good pattern to follow, but if you have a
lot of getters using the same data it can be ugly to put checks in a lot
of different places. If it's just the one property, then lazy
initialization is definitely the way to go.

It'd be nice if a bean could optionally implement some managed bean
interface and there'd be onInitialize() method that would get called
automatically if bean so wished (i.e. implements the interface). The
same approach could be used for a lot of other cases as well.

Kalle


-----Original Message-----
From: David Gadbois [mailto:[EMAIL PROTECTED] Sent: Friday, March 11, 2005 3:31 PM
To: MyFaces Discussion
Subject: Re: How to execute bean method before JSF page is loaded?


The pattern I have been using for this sort of thing it to have the value accessor in the bean check to see if the data has been retrieved and, if not, sets the value up. E.g.,

public class SomeBean {
  private Object value = null;

  public Object getValue() {
    if (value == null) {
      // Initialize value
    }
    return value;
  }

  public void setValue(Object value) {
    this.value = value;
  }
}

The data table then accesses the value through the getter:

<h:dataTable var="item" value="#{someBean.value}">

You could use a PhaseListener on the Render Response phase (and JSF 1.2 will have listeners that can be more conveniently attached to a particular view), but then you may run into problems if some other view logic needs to access the value in a prior phase. For this reason, I have found most recommendation for using a PhaseListener to be wrong-headed.

--David Gadbois


Jonathan Eric Miller wrote:

Does anyone know if there is a recommended way to have a

bean method


executed before a JSF page is loaded? For example, I have a

page with


a h:dataTable in it. What I want to do is make it so that

when a user


brings up the page directly (by entering the URL in the

browser text


box rather than as the result of submitting a form), a bean

method is


executed which does a query to a database. I want the bean to be populated before the page is rendered. Currently, I'm doing

this using


a servlet filter. However, one problem with this is that

Faces isn't


initialized at this point. This isn't a big issue, but, one

issue that


I ran into is that I was using a managed bean and that bean

might not


have been created by Faces when the filter executes. So, I put some code in to test if the bean is null or not and it creates it if necessary. I'm wondering if there is a more recommended way

to do this


within the context of Faces itself. i.e. maybe using a listener or something? Note, I only want the code to execute for a

specific page.


It seems like there should be some kind of onInit() method

that could


be overriden to perform initialization before a page is

renderered...


I don't know much about ASP.NET, but, I think it allows you to do something like this. I guess I could use a scriptlet in the

JSP to do it.

Jon




Reply via email to