Using server faces its possible to store up the pages a user has visited in an order. This was useful for me because I needed one page to be called from a lot of other places and “javascript:history.back()” wasn’t working because when the user clicks buttons on the page, the back page is the last one!.
So first setting up the session bean.
In the session bean I added a public string.
public String[] sURL={"","","","",""};
And some functions to support it:-
public void StoreURL(String sURLtoSearch) {
// this routine keeps a history of 5 urls
// that the user has visited
// so that we can correctly link back to the last one
if (sURL[0].equals(sURLtoSearch)) {
return;
}
// its different lets do the shuffle
for (int i=4;i>0;--i) {
sURL[i]=sURL[i-1];
}
sURL[0]=sURLtoSearch;
}
public String getURLs() {
String result="";
for (int i=0;i<5;++i) {
result=result.concat(sURL[i]);
}
return result;
}
public String getLastPage() {
// returns the last page visited
return sURL[0];
}
Then on the the pagefragment I have on all the pages (header) I added the code:-
HttpServletRequest request= (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
try {
String sURL=request.getRequestURL().toString();
sURL=sURL.substring(sURL.lastIndexOf("/")+1);
getSessionBean1().StoreURL(sURL);
} catch (Exception e) {
}
I’ve left it pretty for you but its now down to one line.
Then lets say I have a Hyperlink I want to link back to the last page…
hypWHEREFROM.setUrl(getSessionBean1().getLastPage() );
And off we go.