简体   繁体   中英

Navigation with JSF2.0 and Primefaces 3.4

I'm new to JSF and Primefaces and have just started working on logging in and basic navigation and I have run into a problem already. I've gone through about 10 similar questions here on SO and none of the solutions has worked for me yet so I figured I would post up my specific problem so that someone who really knows can point me in the right direction.

  1. Logging in: seems to work just fine as does logging out but I'm concerned because the url in the browser still says that I'm at the login screen after logging in and I used the login example straight from the Oracle EE6 docs. Login method is provided below.

     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"; 

    }

  2. After logging in I'm taken to the correct page in the correct directory and everything is being displayed corectly but when you hover over the links the status bar at the bottom of the browser displays the same url for all three links. Code for the page provided below.

      <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> 
  3. The NavigationBean

    @Named(value = "navigationBean") @RequestScoped public class NavigationBean implements Serializable {

    public NavigationBean() { }

    public String getPageToDisplay() { return pageToDisplay; }

    public void setPageToDisplay(String pageToDisplay) { this.pageToDisplay = pageToDisplay; }

    private String pageToDisplay = "welcome.xhtml"; }

When the page loads after logging in the default page set in the navigation bean is displayed but clicking on any link other than the log out link causes the default page to disappear from the center layout unit and a blank page is displayed/ Clicking on the log out link does log you out like intended though. ANy help would be greatly appreciated.

1. Logging in: seems to work just fine as does logging out but I'm concerned because the url in the browser still says that I'm at the login screen after logging in.

Send a redirect (this instructs the browser to send a new GET request on the given URL, which get reflected in browser's address bar).

return "/faces/system/index?faces-redirect=true";

2. After logging in I'm taken to the correct page in the correct directory and everything is being displayed corectly but when you hover over the links the status bar at the bottom of the browser displays the same url for all three links.

The <h:form> indeed submits to the very same page. Use <h:outputLink> or <h:link> instead of <h:commandLink> for page-to-page navigation. See also When should I use h:outputLink instead of h:commandLink?


3. When the page loads after logging in the default page set in the navigation bean is displayed but clicking on any link other than the log out link causes the default page to disappear from the center layout unit and a blank page is displayed

This is solved by using GET instead of ajax postback for page-to-page navigation. So, it's inherently solved when solving #2. You might only want to redesign your NavigationBean to be a filter or phase listener which also intercepts on GET requests. You shouldn't be navigating by POST at all. It defeats bookmarkability, user experience and SEO, exactly as you're encountering now.

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