سؤال

some of my viewParams are mandatory and a FacesMessage is shown when they are missing. However the system event preRenderView is excecuted with empty search parameters and thus there is a default data result returned. Is there anyway to prevent preRenderView registered methods to be executed when the required attribute is violated?

هل كانت مفيدة؟

المحلول

The <f:event type="preRenderView"> is in this construct in essence a hack/workaround. It's as its name says indeed always invoked before render response phase regardless of the type request (postback or not) and the validation result (fail or not). This is also in detail elaborated in this related question: What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for? This contains the following snippet which should help you further:

public void init() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    if (!facesContext.isPostback() && !facesContext.isValidationFailed()) {
        // ...
    }
}

Put the desired logic there in place of // ....

In JSF 2.2, a standard solution is available in flavor of <f:viewAction>, also demonstrated in the aforementioned related question.

نصائح أخرى

I haven't looked at the code, but from the docs the preRenderView starts before anything from the view is constructed. So the application wouldn't even know there's a view param to be validated.

You probably need to check for your required arguments in the event method or use JSF-2.2's new f:viewAction which seems to be what you actually want.

If you can add/are already using Omnifaces, there's the the InvokeActionEventListener, which runs after <f:viewParam>s are assigned:

<f:event listener="#{bean.myListener()}" type="postInvokeAction" />
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top