Re-execute f: viewAction when the ViewScoped bean is recreated after a POST request

Environment: JSF 2.2 with Mojarra 2.2.12 and CDI ViewScoped beans and javax.faces.STATE_SAVING_METHODinstalled on client.

To properly initialize my bean thanks <f:viewParam ... />, I would like to (re) execute <f:viewAction action="#{bean.onLoad}" />when the ViewScopedbean is recreated (view was popped from the LRU, see com.sun.faces.numberOfLogicalViews ) after the POST request.

<f:metadata>
    <f:viewParam maxlength="100" name="name" value="#{bean.file}" />
    <f:viewAction action="#{bean.onLoad}"  />
</f:metadata>

<o:form includeRequestParams="true">
     <!-- action can only work if onLoad has been called -->
     <p:commandButton action="#{bean.action}" />
</o:form>

Any ideas?

Notes:

  • I know postBack="true", but not suitable, because it bean.onLoad()will be called in every POST request.
  • onLoad() @PostConstruct, viewParam (. f: viewAction PostConstruct?).
+4
2

postBack="true", , bean.onLoad() POST.

EL onPostback, , / .

, , :

<f:metadata>
    <f:viewParam maxlength="100" name="name" value="#{bean.file}" required="true" />
    <f:viewAction action="#{bean.onLoad}" onPostback="#{empty bean.file}" />
</f:metadata>

, :

<f:metadata>
    <f:viewParam maxlength="100" name="name" value="#{bean.file}" />
    <f:viewAction action="#{bean.onLoad}" onPostback="#{empty bean.file and not empty param.name}" />
</f:metadata>

onLoad() @PostConstruct, viewParam.

<o:form> , , OmniFaces. CDI @Param , HTTP- @PostConstruct.

, <f:viewParam><f:viewAction> :

@Inject @Param(name="name", validators="javax.faces.Length", validatorAttributes=@Attribute(name="maximum", value="100"))
private String file;

@PostConstruct
public void onLoad() {
    if (!Faces.isValidationFailed()) {
        // ...
    }
}

, Bean Validation (aka JSR303) :

@Inject @Param(name="name") @Size(max=100)
private String file;

@PostConstruct
public void onLoad() {
    if (!Faces.isValidationFailed()) {
        // ...
    }
}
+3

, havent numberOfLogicalView, beans . , usecase - () , - . initialized -flag,

@SomeLongLivingScope
public class MyBean {

    private boolean initialized = false;

    public void preRenderView() {
        if ( initialized ) return;
        try {
             do_init();
        } finally {
            initialized = true;
        }
    }

    public void someDataChangedObserver( @Observes someData ) {
        ...
        initialized = false;
    }
}

, viewAction postBack="true".

0

Source: https://habr.com/ru/post/1611263/


All Articles