[英]Navigation with JSF2.0 and Primefaces 3.4
我是JSF和Primefaces的新手,剛剛開始進行登錄和基本導航工作,現在已經遇到了問題。 我在SO上經歷了大約10個類似的問題,但沒有一個解決方案對我有用,所以我認為我會提出我的特定問題,以便真正認識的人可以向我指出正確的方向。
登錄:似乎和注銷一樣正常,但是我擔心,因為瀏覽器中的url仍然顯示登錄后處於登錄屏幕,並且直接使用了Oracle EE6文檔中的登錄示例。 登錄方法如下。
public String login(){ FacesContext context = FacesContext.getCurrentInstance(); HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest(); try{ logger.log(Level.FINE, "User credentials: name: {0}, password: {1}", new Object[] {this.username, this.password}); request.login(this.username, encrypt(this.password)); logger.log(Level.FINE, "User: {0} logged in", this.username); }catch(ServletException e){ logger.log(Level.SEVERE, "User: {0} login failed, password: {1}", new Object[]{this.username, encrypt(this.password)}); context.addMessage(null, new FacesMessage("Login Failed!")); return "error"; } return "/faces/system/index";
}
登錄后,我被帶到正確目錄中的正確頁面,並且所有內容都會正確顯示,但是當您將鼠標懸停在鏈接上時,瀏覽器底部的狀態欄會為所有三個鏈接顯示相同的URL。 下面提供的頁面代碼。
<h:body> <p:layout fullPage="true"> <f:facet name="last"> <h:outputStylesheet library="css" name="discovery.css"></h:outputStylesheet> </f:facet> <p:layoutUnit styleClass="headerDiv" position="north" size="100"> <h:graphicImage library="images" name="header.jpg"></h:graphicImage> </p:layoutUnit> <p:layoutUnit styleClass="navDiv" position="west" size="200" id="navPanel"> <h:form> <h:outputText value="Navigation Menu"></h:outputText> <br/> <p:commandLink value="First Time Users" update=":main"> <f:setPropertyActionListener target="#{navigationBean.pageToDisplay}" value="tutorial.xhtml"></f:setPropertyActionListener> </p:commandLink> <br/> <p:commandLink value="Help" update=":main"> <f:setPropertyActionListener target="#{navigationBean.pageToDisplay}" value="help.xhtml"></f:setPropertyActionListener> </p:commandLink> <br/> <h:commandLink action="#{loginBean.logout()}" value="Log Out"></h:commandLink> </h:form> </p:layoutUnit> <p:layoutUnit position="center" id="main"> <ui:include src="#{navigationBean.pageToDisplay}"></ui:include> </p:layoutUnit> </p:layout> </h:body>
NavigationBean
@Named(value =“ navigationBean”)@RequestScoped公共類NavigationBean實現了Serializable {
公共NavigationBean(){}
public String getPageToDisplay(){return pageToDisplay; }
public void setPageToDisplay(String pageToDisplay){this.pageToDisplay = pageToDisplay; }
private String pageToDisplay =“ welcome.xhtml”; }
在登錄后顯示了導航Bean中設置的默認頁面后加載頁面時,但是單擊除注銷鏈接以外的任何鏈接都會導致默認頁面從中央布局單元中消失,並顯示空白頁面/單擊日志輸出鏈接確實會按預期將您注銷。 任何幫助將不勝感激。
1.登錄:似乎和注銷一樣正常,但是我擔心,因為瀏覽器中的URL仍然顯示我登錄后處於登錄屏幕。
發送重定向(指示瀏覽器在給定的URL上發送新的GET請求,該請求會反映在瀏覽器的地址欄中)。
return "/faces/system/index?faces-redirect=true";
2.登錄后,我將帶到正確目錄中的正確頁面,並且所有內容都會正確顯示,但是當您將鼠標懸停在鏈接上時,瀏覽器底部的狀態欄會為所有三個鏈接顯示相同的URL。
<h:form>
確實提交到同一頁面。 使用<h:outputLink>
或<h:link>
代替<h:commandLink>
進行頁面間導航。 另請參見何時應使用h:outputLink代替h:commandLink?
3.登錄后,在加載導航頁中設置的默認頁面后加載頁面時,但單擊注銷鏈接以外的任何鏈接都會導致默認頁面從中央布局單元中消失,並顯示空白頁面
這可以通過使用GET代替ajax回發進行頁面間導航來解決。 因此,在解決#2時會固有地解決。 您可能只想將NavigationBean
重新設計為過濾器或階段偵聽器,也可以攔截GET請求。 您根本不應該通過POST進行導航。 就像您現在遇到的那樣,它擊敗了可書簽性,用戶體驗和SEO。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.