All Things Techie With Huge, Unstructured, Intuitive Leaps

Java Server Faces Redirect

I have been created a JSP/JSF web application and a certain aspect of it was coded in XHTML using JSF and Facelets. It has been giving me no end of grief. On every page, I do a check of a session attribute to see if the person is logged in or not. It is a simple check. I have a page that is included with every JSP page that contains a little scriplet that queries the session attribute. If an attribute called "Access Granted" is true, then the person is logged in and gets to view the page. If not, they are re-directed to the index page which is the login page.

Here is the normal code that works great as a scriplet:

<%

//If a user bookmarks a page, then this check is performed to see
// if they are properly logged in. If not they get re-directed
// to the login page.

// The way that it is done, is that the login validation sets an
// Access Granted attribute.

String accessGranted = (String) session.getAttribute("AccessGranted");
if(accessGranted == null)
accessGranted = "false";
if(accessGranted.equalsIgnoreCase("false"))
response.sendRedirect("index.jsp?NotLoggedIn=True");
%>

Simple stuff. Then I had a page coded in XHTML and I used the ui:include tag:


The tag worked, but the page didn't. The scriptlet tag <% some code ... %> fails because it is not well formed html. It is not closed like a regular tag "/>".

So I rewrote the page in well-formed XML. I added the JSTL jar so that I would have access to the c:if conditional tag and the c:redirect tag. One can always get a session attribute with the following syntax:

"#{sessionScope['attribute_name']}"

OK, so I use the conditional tag in the jstl core to see if the session attribute was true, and if not, then re-direct to the index page. Piece of cake, right? Wrong.

After dicking around for a long time, I had an error message that said something like the tag was defined in "http://java.sun.com/jsp/jstl/core", but no class could be found. It was after a bit of hair pulling that I found out that the c:redirect tag was not supported by facelets.

A lot more dicking around ensued until I found this javascript work-around that enables a JSF redirect. Here is the code in its entirety:




Having a problem as this blog wants to parse the tags in the code. Replace '<' with < and '>' with >.




'<'ui:composition
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" '>'

'<'c:if test="#{!sessionScope['AccessGranted']}" '>'

'<'form jsfc="h:form" id="redirect"'>'
'<'a jsfc="h:outputLink" value="#{'index.jsp'}" id="path" '>''<'/a'>'
'<'script type="text/javascript"'>'
var link = document.getElementById("redirect:path");
location.replace(link);
'<'/script'>'
'<'/form'>'
'<'/c:if'>'

'<'/ui:composition'>'



It works perfectly. However, JSF is supposed to uncomplicate your coding life. It just did the opposite for me.

No comments:

Post a Comment