PrimeFaces Ajax Navigation with Browser History/Hashtag

前端 未结 2 787
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 07:25

I have implemented a web application which is a one-page-design. Which basically loads a single page then updates with AJAX the central content. The code is the following:

相关标签:
2条回答
  • 2021-01-01 07:55

    If I understand well your question, you want to navigate with back/forward button. You can do this with LinkedList in your backing bean:

    private LinkedList<String> historyForBackPage= new LinkedList<String>();
    
    public void setLastBackPage(String navigationCase) {
        historyForBackPage.push(navigationCase);
        if (historyForBackPage.size() > yourMaxSize) {
            historyForBackPage.pollLast();
        }
    }
    
    public String getLastBackPage() {
        return historyForBackPage.pop();
    }
    

    and always add last page when you call layout.setAll method. The simple commandButton call getLastBackPage() method. Before try it, please configure navigation case in faces-config.xml.

    If your case cannot work navigation case, because you work only one XHTML, than you could add back/foward page name for your bean and render your page. May be simple JavaScript call onclick="window.history.go(-1); return false;" it could be usefull in your case. I don't know. Please try it!

    In my answer I focused only to back button, but I think you can adapt foward button in same way.

    By the way, the Breadcrumb is nice PrimeFaces feature.


    If you want to catch browser back clicking action, you can use one JavaScript or Ajax script.

    For Ajax script please check this answer https://stackoverflow.com/a/10050826/1047582.

    0 讨论(0)
  • 2021-01-01 08:01

    I have created a blog post explaining how to get this to work using jQuery BBQ, based on your question.

    With jQuery BBQ you can keep track of state, history and allow bookmarking while dynamically modifying the page via AJAX and/or DHTML.. just click the links, use your browser's back and next buttons, reload the page..

    First we should include jQuery BBQ.

    <h:outputScript library="js" name="jquery.ba-bbq.min.js" /> 
    

    Now consider we have the menu (with all our nav rules)

      <p:submenu label="Meat">
         <p:menuitem outcome="/meat/steak.xhtml" value="Steak" />
         <p:menuitem outcome="/meat/burger.xhtml" value="Burger" />
         <p:menuitem outcome="/meat/chicken.xhtml" value="Chicken" /> 
         <p:menuitem outcome="/meat/kebab.xhtml" value="Kebab" /> 
      </p:submenu>
    

    Then the centered content

     <pe:layoutPane id="content" position="center">
    
         <h:panelGroup id="centerpanel" layout="block">
            <ui:include id="include" src="#{mainBean.currentNav}" />
         </h:panelGroup>
    
      </pe:layoutPane>
    

    the include reflects the currentNav clicked.

    now define a remoteCommand inside the form

    <p:remoteCommand name="updateNav" 
                     actionListener="#{mainBean.updateNav()}"  
                     update=":centerpanel"/>
    

    This remoteCommand will update our currentNav based on the hashtag.

    Create your JS file or include the following code into the document ready

    $(document).ready(function() {
    
       $('.ui-menuitem-link').click(function(event) {
           event.preventDefault();
           var currentNav = $(this).attr('href').
                            substr($(this).attr('href').indexOf("/faces") + 6)
           window.location.hash = '#' + currentNav;
    
       })
    
       $(window).bind('hashchange', function(e) {
    
        var url = $.param.fragment();
        updateNav([{name: 'currentNav', value: url}]); //remoteCommand Call
    
       })
    
       $(window).trigger('hashchange');
    
     });
    

    Basically first we handle our clicks on the menu items, setting the hash of the window.

    then we define an event of the hashchange of the window, using the help of jQuery BBQ.

    ManagedBean

    public void updateNav() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map map = context.getExternalContext().getRequestParameterMap();
        currentNav = (String) map.get("currentNav");
    }
    

    For a complete code, please see my newly post created for the question.

    Primefaces Hash Navigation Using jQuery BBQ

    And also the example is available on github.

    Hope it helps.

    0 讨论(0)
提交回复
热议问题