I want to access the current PhaseId when a method is called on a managed bean given that the bean has not implemented the PhaseListener interface. Means to know what is the current phase in which this method is being called. Is it possible? I have googled but could not find any working way.
If you want a backing bean to know what phase its methods are being called from, you're probably about to do something horrible. Backing beans are supposed to be essentially POJOs.
As far as it goes, the phases that invoke the various methods are quite predictable, but since the methods themselves shouldn't exhibit variant behavior depending on phase, there's not much point in knowing. Phase Listeners are not normally backing bean objects.
An IDE is no substitute for an Intelligent Developer.
Joined: Aug 28, 2009
well, not that much horrible..
Actually I am stuck with a problem in doing true pagination. Here is the code
Now here when the first time page is loaded, everything is ok. When I hit '2' to show the second page, the method "getTableModel" in 'Paginator' backing bean is called three times. first in "APPLY_REQUEST_VALUES" phase and then two time is in "RENDER_RESPONSE" phase. In real situation where data base will be involved it means three database hits which is surly not a good thing. First I dont want database hit in "APPLY_REQUEST_VALUES" phase as it will again query db. If I get to know that this method is being called in "APPLY_REQUEST_VALUES" phase then I can restrict it.and Secondly, I have no Idea that why is the "getTableModel" method called twice in "RENDER_RESPONSE" phase.
You are planning something horrible. Getter methods should not have side-effects, nor should they attempt to do extensive logic. It's not good practice even for POJOS, and JSF backing bean "get" methods can be invoked multiple times, depending on the context. Even within the same phase.
You should do your paging in the action processors, not in "get" methods. If you're trying to avoid using a session-scope object, you're not going to have an easy time of it. Depending on the version of JSF you're using, a request-scope object could set up the datamodel in a @PostConstruct method. Or in JSF2, you can use View scope. For earlier versions of JSF, it's better to just use session scope, but release the datamodel when you're done with it (in the action method that takes you away from that view).
In certain rare cases, I have been forced to setup something in a "get" method. The way I did it was like this: