6 Replies Latest reply on Aug 17, 2009 7:47 PM by israel.bgf

    Last Page Visited

    israel.bgf

      How to know the last page that the user visited?


      Example:


      I'm, in the view /view1.xhtml and go to the /view2.xhtml, now in the view 2 i want some method to return the /view1.xhtml, is there any built-in function to achieve this?

        • 1. Re: Last Page Visited
          israel.bgf

          And btw, is there any Seam/JSF event that can detect if the user changed the current page? Maybe something with a phase listener to achieve this..

          • 2. Re: Last Page Visited
            ben_utzer

            Hi,


            you could try registering a listener to org.jboss.seam.afterPhase or org.jboss.seam.beforePhase events I guess.


            Cheer,


            Ben

            • 3. Re: Last Page Visited
              israel.bgf
              But how could i know if the view id is changing with these events? Anyway, id did a component to solve this needing:



              `
              package br.com.techpeople.nexxcard.utility;

              import java.util.LinkedList;

              import javax.faces.context.FacesContext;

              import org.jboss.seam.Component;
              import org.jboss.seam.ScopeType;
              import org.jboss.seam.annotations.AutoCreate;
              import org.jboss.seam.annotations.Create;
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.annotations.Scope;
              import org.jboss.seam.annotations.intercept.BypassInterceptors;

              @Name("viewHistory")
              @AutoCreate
              @BypassInterceptors
              @Scope(ScopeType.SESSION)
              public class ViewHistory {

                      private LinkedList<String> history;
                     
                      @Create
                      public void init(){
                              history = new LinkedList<String>();
                      }
                     
                      public void addCurrentViewId(){
                             
                              String currentViewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
                             
                              if(history.size() != 0 && history.getLast().equals(currentViewId)){
                                      return;
                              }
                              if(history.size() == 2){
                                      history.removeFirst();
                              }
                              history.add(currentViewId);
                      }
                     
                     
                      public String getLastViewId(){
                              if(history.size() == 0){
                                      return FacesContext.getCurrentInstance().getViewRoot().getViewId();
                              }
                              return history.getFirst();
                      }
                     
                     
                      public static ViewHistory instance(){
                              return (ViewHistory) Component.getInstance("viewHistory", ScopeType.SESSION);
                      }
              }

              `

              Just have to put it in action in the pages.xml to the view id matching /*. Example:

              <page view-id="/pages/*" login-required="true">
                   <action execute="#{viewHistory.addCurentViewId}" on-postback="false"/>
              </page>

              the problem is... it is doing something strange, it's causing an endless redirect in my browser. Don't know why, maybe I did a mistake. Could someone just put this component, and test it for me?

              Thanks in advance,

              Israel

              (Seam 2.1.1)
              • 4. Re: Last Page Visited
                ben_utzer

                Hi Israel,


                your code works for me if I correct the spelling error in #{viewHistory.addCurentViewId}.


                My suggestion was to use something like:




                 @Observer("org.jboss.seam.beforePhase")
                 public void before(PhaseEvent event) {
                    if (event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {
                        //do your history thing
                    }
                 }
                
                




                • 5. Re: Last Page Visited
                  ralf

                  Hi,


                  there is a component, which name is redirect. There is a functionality which is named captureCurrentView and returnToCapturedView.


                  https://www.redhat.com/docs/manuals/jboss/jboss-eap-4.3/doc/seam/api/org/jboss/seam/core/Redirect.html


                  You can track method-calls and their behaviour in the pages.xml


                  I dont have an example, but i am sure, you will find something.

                  • 6. Re: Last Page Visited
                    israel.bgf

                    Thanks for the replies, i will give a look in both tips. :)