Search code examples
jsfjstlunset

How to make JSF handle unset values?


Before I was using JSF, the following code was working in JSP files:

<c:if test="${not (empty request.error)}">
    Error: ${request.getAttribute("error")}
</c:if>

Now, when I am using JSF, I am getting this error:

/login.xhtml @24,51 test="${not (empty request.error)}" /login.xhtml @24,51 test="${not (empty request.error)}": Property 'error' not found on type org.apache.catalina.connector.RequestFacade

What is the problem/solution here? The editor does not underline anything.

Whole login.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./template.xhtml"
                xmlns:c="http://java.sun.com/jsp/jstl/core">

    <ui:define name="userBar">
        <form method="POST" action="Login">
            <table>
                <tr>
                    <td>Login</td>
                    <td><input type="text" name="login" /></td>
                </tr>
                <tr>
                    <td>Password</td>
                    <td><input type="password" name="password" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Login" /></td>
                </tr>
            </table>            
        </form>

        <c:if test="${not (empty request.error)}">
            Error: ${request.getAttribute("error")}
        </c:if>
    </ui:define>

    <ui:define name="content">
        content
    </ui:define>
</ui:composition>

Solution

  • Since JSF2/Facelets, the #{request} is an implicit EL variable referring the currently involved HttpServletRequest object. See also Communication in JSF 2.0 - Implicit EL objects.

    You need to either rename your managed bean (or whatever it is which appears with exactly the same attribute name in the EL scope), or to explicitly refer it through the EL scope map. Assuming that it's request scoped, you should be using #{requestScope.request} instead.

    <c:if test="#{not empty requestScope.request.error}">