wood burning stoves*
The moose likes JSF and the fly likes Problem with Navigation in subview Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Problem with Navigation in subview" Watch "Problem with Navigation in subview" New topic
Author

Problem with Navigation in subview

matthew simcox
Greenhorn

Joined: Aug 20, 2008
Posts: 7
I've a banner jsf subview used on all my pages. In this banner I have a logout link that invalidates the user session then redirects to the initial login page. when the logout link was in the main page (not in a subview) it worked a treat however inside the subview the session is invalidated but no redirection occurs. The setup is as follows:

Entry page:

</ui:head>
<f:subview id="entryviewbanner">
<jsp:include page="/BannerView.jsp"/>
<jsp:include page="/EntryView.jsp"/> ***contains the link


EntryView page:

<ui:form id="form_entryview">
<h:panelGrid columns="2" id="welcomepanelgrid" style="left: 24px; top: 144px; position: absolute">
<ui:label id="welcomelabel" text="#{bundle.entryview_welcome_label}"/>
<ui:staticText id="welcometext" text="#{SessionBean.firstName} #{SessionBean.surName}"/>
<ui:label id="rolelabel" text="#{bundle.entryview_role_label}"/>
<ui:staticText id="roletext" text="#{SessionBean.role}"/>
</h:panelGrid>
<ui:hyperlink action="#{EntryView.logout_action}" id="logout" style="left: 1150px; top: 144px; position: absolute" text="#{bundle.entry_logout}"/>
</ui:form>
</f:subview>

Can anyone suggest why this wouldn't work. I really don't want to have to include the logout button in every page separately. Is there a better way of doing this?
matthew simcox
Greenhorn

Joined: Aug 20, 2008
Posts: 7
I forgot to include the action code in the backing bean:

public String logout_action() {

log("Invalidating Session for user: " + getSessionBean().getProvidedUserName());

/*Invalidate the session and return user to login page*/
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpSession session = (HttpSession) externalContext.getSession(false);
session.invalidate();

return "logout";
}
Darryl Nortje
Ranch Hand

Joined: Jun 11, 2002
Posts: 140
Howzit Matthew,

Obvious question, have you got the navigation rule setup correctly in your faces-config xml file?

cheers
Darryl
matthew simcox
Greenhorn

Joined: Aug 20, 2008
Posts: 7
Yeah I believe so:

<navigation-rule>
<from-view-id>/Entry.jsp</from-view-id>
...
<navigation-case>
<from-outcome>logout</from-outcome>
<to-view-id>/login.jsp</to-view-id>
<redirect/>
</navigation-case>
...

I've had a hunt around and found this variation does work:

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpSession session = (HttpSession) externalContext.getSession(false);
session.invalidate();

Not sure what the difference is between the two though.
matthew simcox
Greenhorn

Joined: Aug 20, 2008
Posts: 7
sorry pasted the code the wrong way around:

This doesn't work:

FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest request =
(HttpServletRequest)fc.getExternalContext().getRequest();
request.getSession().invalidate();

this does work:

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpSession session = externalContext.getSession();
session.invalidate();

The first appears to get the session off the back of the request and invalidate, the second gets the session from the external context. What is the difference here and why would it prevent naviagtion. The navigation occurs after the invalidate() is called.
Jerwin Louise Uy
Ranch Hand

Joined: Oct 27, 2007
Posts: 75
I think there are some issues when redirecting using the navigation handler when the session is already invalidated.

Therefore, my suggestion is that perform a redirect when the session is already invalidated.



Regards.


Uy Jerwin Louise Vergara
Junior Developer / Research and Development at Incuventure Partners Corporation
email: jerwin.uy@incuventure.net
matthew simcox
Greenhorn

Joined: Aug 20, 2008
Posts: 7
thanks for the help. In the end I've changed it so that the navigation sends the user back to the login page and then in the constructor of the backingbean it checks to see if some login variables are null or not. if they're not null then the session gets invalidated. Any better ideas would be gratefully accepted:

public login() {



/*If user already logged in then log them out when redireted back here*/
if (getSessionBean().getRetrievedUserName() != null) {

log("About to invalidate login");

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpSession session = (HttpSession) externalContext.getSession(false);
session.invalidate();
}
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with Navigation in subview