jsf 2 - How to start special init event in a backing bean before JSF page loads? -


pf 3.5.10, mojarra 2.1.21, omnifaces 1.5

how call special init()-method of (cdi)sessionscoped bean before load .xhtml jsf page ? call init() if user select page site menu (with p:menutitem). if user use browser address line type url directly?

edit: my.xhtml:

<ui:define template="/mytemp.xhtml">    <f:event type="prerenderview" listener="#{mybean.init()}" />    <h:form>      <p:commandbutton update="@form" ... />    </h:form> </ui:define> 

if way init() called on every update (i.e. on every postback server),in example on every click of commandbutton. can not use proposal.

edit 2: thank luiggi mendoza, , balusc! in addtion solution luiggi mendoza, in comments stated omnifaces 1.6 have viewscope also.

the problem @postconstruct public void init() method called after managed bean created , fields injected. since bean @sessionscoped, live until user session expires.

a way solve use <f:event type="prerenderview" listener="{bean.init}" /> explained here: what can <f:metadata>, <f:viewparam> , <f:viewaction> used for? (no need use <f:metadata> explained balusc here: does matter whether place f:event inside f:metadata or not?).

per question update, problem handled in first link. i'll post relevant code handle situation (taken balusc answer):

public void init() {     facescontext facescontext = facescontext.getcurrentinstance();     if (!facescontext.ispostback() && !facescontext.isvalidationfailed()) {         // ...     } } 

if migrate jsf 2.2, there's a @viewscoped annotation cdi beans , reduce scope of @sessionscoped beans accordingly.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -