Linking Non-JSF Resources

You’ve already seen an example of a link to a different resource than the JSF view that rendered the screen containing the view, namely the link to the first preferences view in the menu area view. The logout link is another example, and in links to a regular JSP page instead of a JSF view:

[<h:outputLink value="../../logout.jsp">
  <h:outputText value="Logout" />
</h:outputLink>]

The logout.jsp page is located in the top directory for the application, so that accessing this page doesn’t trigger the authentication constraint declared for all resources in the expense directory; it wouldn’t make sense to have to login if the session has timed out just to be able to logout. I therefore specify the path as a relative path to the file located two directories up from the file with the link. An alternative is to use a JSF EL expression that adds the context path for the application to a context-relative path so that it makes sense to the browser:

[<h:outputLink 
  value="#{facesContext.externalContext.request.contextPath}/logout.jsp">
  <h:outputText value="Logout" />
</h:outputLink>]

The JSF EL expression evaluates to the application context path, e.g., /jsfbook if you use the default for the sample application, so the HTML link’s href attribute is rendered with the value /jsfbook/logout.jsp. It would, of course, be better if the link renderer added the context-path automatically for a context-relative path, but it slipped through the cracks for the JSF 1.0 specification. ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.