views:

187

answers:

3

Hi,

What the best way to get custom post-processing after binding to a backing bean, similar to onBind event when using a Spring MVC controller ?

I suppose i need a PhaseListener (after phase Update Model Values) as follows

new PhaseListener() {

    public void beforePhase(PhaseEvent event) {}

    public void afterPhase(PhaseEvent event) {
        // custom post-processing
    }

    public PhaseId getPhaseId() {
        return PhaseId.UPDATE_MODEL_VALUES;
    }

}

Is it a best practice or not ? If not, what should i use ?

+2  A: 

Well, this functionality is there, so you are free to use it.

If you want per-page (per-view actually) phase listener method, use:

<f:view afterPhase="#{bean.afterPhase}">

If you are using Facelets, the attribute is called afterPhaseListener

Bozho
+2  A: 

In a JEE5 server, JSF 1.2 can take advantage of @PostConstruct:

JSF implementations running in a Java EE 5 compliant container must support attaching the @PostConstruct and @PreDestroy annotations to aid in awareness of the managedbean lifecycle.

Methods on managed beans declared to be in request, session, or application scope, annotated with @PostConstruct, must be called by the JSF implementation after resource injection is performed (if any) but before the bean is placed into scope.

McDowell
Or if jsf-spring integration is used (+1)
Bozho
Good to know (+1)
Arthur Ronald F D Garcia
+1  A: 

Why don't you just hook on the INVOKE_ACTION phase? You normally use the bean's action method for this. You can use an UICommand component to trigger the event, such as h:commandLink or h:commandButton:

<h:form>
    <h:inputText value="#{bean.input}" />
    <h:commandButton value="submit" action="#{bean.submit}" />
</h:form>

Bean would look like:

public String submit() {
    // Do your thing here. The `this.input` is already set.
    return "someNavigationCaseOutcome"; // Can even be null or void for a postback to same page.
}

If you just want to cover all JSF POST requests, then a PhaseListener is indeed the best solution. I would rather hook on the beforePhase of the INVOKE_ACTION which would make more sense.

BalusC
Hi BalusC, You said: why don't you just hook on the INVOKE_ACTION phase ?. Because i use Seam. I have seen your article about JSF life-cycle (+1). Really good.
Arthur Ronald F D Garcia
I don't do Seam, but I didn't knew that it let the JSF lifecycle behave differently?
BalusC