File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes What phase is it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "What phase is it?" Watch "What phase is it?" New topic
Author

What phase is it?

Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
How can you find out the current phase, from within a managed bean method (for instance)? Why? I'm trying to hack something because JSF has got me


There is no emoticon for what I am feeling!
g madhava
Ranch Hand

Joined: Sep 14, 2001
Posts: 85
The below link helps you in order to set up a phase listener for your JSF application.

http://www.jroller.com/page/cschalk?entry=getting_familiar_with_the_jsf

Put some debug (System.out.println or (SOP in short))statements in your managed bean method and then in your console you should be able to see at what phase are your methods being called.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Thanks for the tip. I know about PhaseListeners, but I was hoping that I wouldn't have to write one just to find out the current phase -- I was hoping this was already exposed more directly in the API. In the end, I realized that all my bean method cared about was whether or not it was being called when rendering a response, and the following is sufficient for that:

FacesContext context = FacesContext.getCurrentInstance();
boolean renderResponse = context.getRenderResponse();

Perhaps my problem could be solved in a cleaner way, so allow me to describe it:

I have an ordinary web page: the top half lets you insert, edit or delete records and the bottom half has a MyFace's DataTable that shows the results of selecting certain summary information from the database. The bean backing this table is request-scoped and its data method used to go to the database the first time it was called:

The trouble with this is that when you submit from this page, before the insert/update/delete action is done, the view is restored which means that my getResults method is called! So my results are a snapshot of the db's state *before* the database action. My solution was to make this method even lazier -- don't try to find any data until the response phase:

This works but I wish it were cleaner. I'm not sure what I want, but since my table has no input fields in it, I wish the backing bean was not created until the response phase. Hmmm.... could this be because my results table is inside the HTML FORM tags? I'll have to play with this a bit more...
[ May 04, 2006: Message edited by: Jeff Albertson ]
g madhava
Ranch Hand

Joined: Sep 14, 2001
Posts: 85
Assuming the backing bean is the same, for both the top and the bottom half of the page, probably this might work.
Tie the action listener event of the top half-page in order to initialize an instance variable that can be checked in your getResults()method. Since action listeners happen at phase 5, you should be pretty much lazily loaded.

Please let us know if this worked for you.
Sample code is as below.


[ May 04, 2006: Message edited by: g madhava ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I don't know if that approach is any better than what I suggested. The trouble with it is that the table needs to be rendered whenever this page is displayed, not just after carrying out a insert/save/delete. For example, there are lots of other pages that are linked to this page, and I don't know if its a good idea to require those pages to know about setting that flag. I'd like my backing bean to look after itself...

(BTW, moving this table out of the scope of the FORM tags didn't change anything.)
g madhava
Ranch Hand

Joined: Sep 14, 2001
Posts: 85
>>> This works but I wish it were cleaner.

I was just giving an alternate solution that doesn't deal with the FacesContext. and is much cleaner.(atleast in my opinion).



I didn't get your point overhere,

table needs to be rendered whenever this page is displayed, not just after carrying out a insert/save/delete.

The table will always be rendered, but the data will be loaded pretty lazily. I thought that was the requirement.
May be I was missing something ?

For example, there are lots of other pages that are linked to this page, and I don't know if its a good idea to require those pages to know about setting that flag. I'd like my backing bean to look after itself...


Yes, if all the pages need this particular lazy loading of the data, then you should be fine with the above solution. But in case if you want it to be customized for each different page, then you might need to have some more code for conditional checking using the viewId of the request page.


YMMV...
Brod Wayne
Greenhorn

Joined: Dec 07, 2012
Posts: 1
If you are using JSF 2.0 or later just use:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What phase is it?