简体   繁体   中英

JSF ViewScope and Bean creation

I have a problem that i don't understand: I request a new site. A site has a link that opens a dialog. The link is inside a form. The dialog is not inside the form.

A reduced code example:

    <p:outputPanel id="layout-center" >
      <h:form>
        <p:commandLink id="option_field_user_profile" actionListener="#{controllerBean.getBean('userProfileBean', component).init}" oncomplete="#{controllerBean.getBean('userProfileBean', component).show}" >
        <h:outputText value="#{msg.mProfile}"/>
        </p:commandLink>
      </h:form>
    </p:outputPanel>

    <p:dialog header="#{userPreferencesBean.header}" widgetVar="#{userPreferencesBean.widgetVar}" appendToBody="#{userPreferencesBean.appendToBody}" resizable="#{userPreferencesBean.resizable}" id="#{userPreferencesBean.xhtmlId}" dynamic="#{userPreferencesBean.dynamic}" modal="#{userPreferencesBean.modal}" closable="#{userPreferenceBean.closable}">
      <ui:include src="/WEB-INF/templates/modification/userPreferences.xhtml" />
    </p:dialog>

UserPreferencesBean is in ViewScope. My problem is now that the @PostConstruct method from the UserPreferencesBean is called twice with the non-postback request ie the Bean is constructed twice although it should be the same view. If i move the dialog inside the form for testing purposes it is called once, like expected. But since the dialog has its own form this is not a solution, for sure. When the site is loaded and I hit F5, the PostConstruct method is executed once.

Has somebody an idea?

This is caused because you referenced a view scoped bean property in the view build time attribute id of the <p:dialog> . If you fix the id to be static, or to reference a request or application scoped bean property instead, then your view scoped bean will behave as expected.

See also:

  • JSTL in JSF2 Facelets... makes sense? - for some background explanation on view build time and view render time; the id and binding attributes of UI components are evaluated during view build time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM