JSF - Problem with @ViewScope

2019-01-29 10:08发布

问题:

I have a page that load (in a panelGroup) two different page, due to the login status (logged, not logged). This is the main switch :

template.xhtml P.S. selector.page is "homepage" as default

<h:panelGroup layout="block" styleClass="contenitore">
    <h:panelGroup layout="block">
        <h:panelGroup layout="block" styleClass="menu_table" id="login">
            <ui:insert name="login_homepage" />
        </h:panelGroup>

        <h:panelGroup layout="block" id="content">
            <c:catch>
                <ui:include src="/#{selector.page}/#{selector.page}.xhtml" />
            </c:catch>
        </h:panelGroup>
    </h:panelGroup>
</h:panelGroup>        

homepage.xhtml

<h:panelGroup rendered="#{!user.loggedIn}">
    <h:panelGroup layout="block" styleClass="content_title">
        Homepage
    </h:panelGroup>
</h:panelGroup>
<h:panelGroup rendered="#{user.loggedIn}">
    <ui:include src="/profile/profile.xhtml" />
</h:panelGroup>

login

<ui:define name="login_homepage">
    <h:form id="formLogin" prependId="false">
        <h:inputHidden value="#{selector.page}" id="page" />

        <h:panelGroup rendered="#{!user.loggedIn}">
            <h:outputScript name="jsf.js" library="javax.faces" target="head" />

            <h:outputLabel styleClass="menu_span" value="Username" />
            <h:inputText value="#{user.nickname}" id="nickname" />
            <h:outputLabel styleClass="menu_span" value="Password" />
            <h:inputSecret value="#{user.password}" id="password" />

            <h:outputLabel styleClass="menu_span">
                <h:commandButton value="Login" action="#{user.checkLogin}">
                    <f:ajax event="action" execute="nickname password page" render=":login :content err"/>
                </h:commandButton>
            </h:outputLabel>
        </h:panelGroup>

        <h:panelGroup rendered="#{user.loggedIn}">
            <h:outputLabel styleClass="menu_title" value="LOGGED" />
        </h:panelGroup>
    </h:form>
</ui:define>

profile.xhtml

<h:panelGroup layout="block" id="profileContent">
    <h:form id="formProfile">
        <h:outputScript name="jsf.js" library="javax.faces" target="head" />

        <h:panelGroup rendered="#{profileSelector.profilePage=='main'}">
            <ui:include src="/profile/profile_main.xhtml" />
        </h:panelGroup>

        <h:panelGroup rendered="#{profileSelector.profilePage=='edit' || profileSelector.profilePage=='editConfirm'}">
            <ui:include src="/profile/profile_edit.xhtml" />
        </h:panelGroup>
    </h:form>
</h:panelGroup>

ProfileSelector is @ViewScope scoped. When i load the page and im logged, it initialize the ProfileSelector bean. Else, if i'm not logged and i do the login, it change the Content from homepage to profile, but the Bean is not initalized. So, how can i inizialize this bean when im not logged?

I think the code above is sufficent to understand the problem.

Cheers

回答1:

  <c:catch>
                <ui:include src="/#{selector.page}/#{selector.page}.xhtml" />
            </c:catch>

The problem is that you are using JSTL elements which break @ViewScope use alternative.



回答2:

A similar bug

http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-928