[英]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.