Adrian Adamczyk
Adrian Adamczyk

Reputation: 3070

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>

Upvotes: 0

Views: 668

Answers (3)

BalusC
BalusC

Reputation: 1108912

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}">

Upvotes: 3

user207421
user207421

Reputation: 310957

It's not an 'unset value', it's a non-existent property. HttpRequest does not have an 'error' property. The request.getAttribute("error") syntax is what you should be using.

Upvotes: 0

RicardoS
RicardoS

Reputation: 2118

The error is clear.
Your bean named request, of type "org.apache.catalina.connector.RequestFacade" has no attribute named "error". Or if it exists, possibly there is no getter/setter.

If your intention is getting a value from servlet request, you could do like this:

<h:outputText value="#{param['error']}" />

More information here: Get Request and Session Parameters and Attributes from JSF pages

But why do you need it? If you want to handle errors in JSF, you could catch the Exceptions and display it as you want with:

h:message tag
FacesMessage class

Some sites:
http://www.jsftoolbox.com/documentation/help/12-TagReference/html/h_message.html

http://docs.oracle.com/cd/E17824_01/dsc_docs/docs/jscreator/apis/jsf/javax/faces/application/FacesMessage.html

Upvotes: 0

Related Questions