myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simon <simon.kitch...@chello.at>
Subject Re: help me decide when to populate my datatable?
Date Wed, 05 Mar 2008 20:58:52 GMT

On Wed, 2008-03-05 at 12:20 -0800, h b wrote:
> Hello, I'm new to jsf.  
> 
> Background:
> I have a page that shows a table of items using the datatable
> component.  The datatable is bound to my backing beans items field.
> <h:datatable values="#{myBean.items}"...></h:datatable>.
> 
> myBean has a request scope.
> 
> myBean.getItems() is implemented as follows:
> if (items == null) items = createItems();
> return items;
> 
> I put in a few log statements to see how everything is working.
> 
> What I'm observing:
> * The first time the page is rendered myBean.getItems is invoked
> during the RENDER_RESPONSE phase.  That's great, it make sense.
> * On post back getItems is called during the: APPLY_REQUEST_VALUES &
> RENDER_RESPONSE phase.
> 
> Questions & Concerns:
> 1. On postback why is getItems getting invoked during
> APPLY_REQUEST_VALUES phase?

Because tables can have editable fields in them. It's perfectly allowed
for an h:column to include an h:inputText or similar. So on postback the
processing does:
  get the table rows (ie fetch the model)
  for each row in the table
    ask each column component to do its postback processing

It might be possible for a table to look into all of its child
components to see whether any are input components, and if not then skip
this processing. However myfaces doesn't currently do that. 

> 2. On postback since myBean has request scope, it is constructed
> again, which means items is null until getItems gets called.  This
> means that (on postback) during the APPLY_REQUEST_VALUES phase, I'm
> doing a db fetch to build my items.  This is not good for me,
> specially since, I want to respond to an event (such as new filter
> criteria) and fetch items based on that event.  So I'll end up doing 2
> fetches.
> 
> IOne way to resolve this issue is by changing myBean scope to session
> scope.  But I want to stay stateless...and I'm hoping jsf can support
> a stateless model.
> 
> I'm sure I'm missing something since this is a very simple use-case.

Yes, it's a common issue.

In JSF 1.2 it is possible to detect whether a postback is in progress.
In your case, if you know that there are no editable fields then you
could then just return null, or an empty list. Of course you then need
to go back to really fetching data in the render phase, but the setters
methods for your search criteria should be able to trigger that.

I agree it's ugly, and there should really be a nicer solution. I don't
know of one though.

See:
  FacesContext.renderKit.responseStateManager.isPostback

Regards,
Simon


Mime
View raw message