簡體   English   中英

條件導航在JSF中無法正常工作

[英]Conditional navigation not working properly in JSF

我正在使用JSF 2.0,hibernate和primefaces開發一個網站。 實際上,我正在實現一個管理面板,在該面板中,網站管理員可以將整個應用程序置於“維護模式”。 這是通過更改應用程序范圍的bean(故意稱為“ AppConfigurationBean”)上的標志來完成的。 如果該標志處於活動狀態,則必須通過在faces-config.xml中聲明的條件導航規則將用戶返回到稱為“ maintenance.xhtml”的頁面。 用戶可以通過插入一個特殊的旁路密鑰來繞過維護頁面,該密鑰很少由管理員提供。 如果他這樣做,則在特殊會話作用域的Bean上激活一個標志,該標志保存有關用戶的信息,稱為“ CurrentClientSessionBean”。 如果從CurrentClientSessionBean上的特殊方法返回的值起作用,則導航規則將起作用,該方法接受應用程序Bean上的維護模式標志以及其自身上的“具有旁路鍵”標志。

這是faces-config.xml上的導航規則:

<navigation-rule>
        <description>System</description>
        <from-view-id>*</from-view-id>
        <navigation-case>
            <display-name>Maintenance Mode</display-name>
            <from-outcome>*</from-outcome>
            <if>#{currentClientSessionBean.toReturnToMaintenance}</if>
            <to-view-id>/maintenance.xhtml</to-view-id>
            <redirect />
        </navigation-case>
</navigation-rule>

這是maintenance.xhtml頁面

<ui:composition id="maintenance" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:plm="http://java.sun.com/jsf/composite/plmCustomComponents">

    <f:verbatim>&lt;!DOCTYPE html&gt;</f:verbatim>

    <html>

<h:head>

    <title>#{msg.applicationTitle}</title>

    <meta charset="UTF-8"></meta>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"></meta>

    <h:outputStylesheet library="default" name="css/normalize.css" />
    <h:outputStylesheet library="default" name="css/icons.css" />
    <h:outputStylesheet library="default" name="css/main.css" />

</h:head>

<f:metadata>
    <ui:include src="metadata/browserViewParams.xhtml" />
</f:metadata>

<h:body>

    <div id="legalPart">
        <ui:insert name="legalPart">
            <ui:include src="/structure/legalPart.xhtml" />
        </ui:insert>
    </div>

    <p:panel>

        <h1>
            <h:outputText value="#{msg.maintenancePageTitle}" />
        </h1>
        <p>
            <h:outputText value="#{msg.maintenancePageBodyMsg}" />
        </p>
        <h:form>
            <p:outputLabel for="bypassKeyInput"
                value="#{msg.maintenancePageInsertBypassKeyLabel}" />
            <br />
            <p:inputText id="bypassKeyInput"
                value="#{maintenanceBacking.keyBuffer}" required="true" />
            <br />
            <p:message for="bypassKeyInput" />

            <p:commandButton value="#{msg.checkPassword}"
                action="#{maintenanceBacking.checkBypassKey}" ajax="false" />
        </h:form>

    </p:panel>

    <ui:debug />

</h:body>

    </html>

</ui:composition>

維護支持非常簡單,它僅檢查旁路密鑰是否匹配真實密鑰,如果是,則簡單導航到主頁。

現在,用於條件導航的CurrentClientSessionBean方法就是這樣的:

/**
     * Special getter that determines if user should return to maintenance
     * 
     * @return
     */
    public boolean isToReturnToMaintenance(){
        try{
            AppConfigurationBean appConfigurationBean = JSFAppUtils.findApplicationBean("appConfigurationBean");
            boolean result = ((!this.hasBypassKey) && appConfigurationBean.isMaintenanceModeOn());
            return result;
        }catch(NotFoundException e){
            logger.error("NotFoundException thrown in isToReturnToMaintenance method", e);
            throw new RuntimeException("NotFoundException thrown in isToReturnToMaintenance method", e);
        }
    }

這是主要的問題:由於某種奇怪的原因,當維護模式處於活動狀態時,即使用戶在輸入中輸入了正確的旁路鍵,也會被迫返回維護頁面。

此外:由於我試圖注釋條件導航規則,因此我的應用程序中肯定存在某些錯誤,這意味着維護頁面應該無法訪問,維護模式的更改對應用程序沒有影響,但維護模式系統仍然沒有影響。以我描述的相同態度繼續工作。

請幫助我,對不起,如果我不夠清楚。 向我詢問您對代碼不了解的任何內容。

好的,我終於找到了解決方案:似乎我在主頁上添加了一個重定向,該重定向已連接到maintenanceModeOn布爾標志,並且沒有考慮hasBypassKey標志。

該規則深深地嵌套在我很久以前編寫的代碼的某些部分中,我忘記了。

<h:panelGroup rendered="#{appConfigurationBean.maintenanceModeOn}"> <script> window.location = "maintenance.faces"; </script>
</h:panelGroup>

請關閉此問題。 謝謝大家度過的空閑時間查看此問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM