webtier@glassfish.java.net

Re: [webtier] JSF2 parses EL expressions that are commented

From: Jason Lee <jasondlee_at_sun.com>
Date: Thu, 25 Feb 2010 10:32:06 -0600

On 2/25/10 10:20 AM, webtier_at_javadesktop.org wrote:
> Hi,
>
> I have a page that looks roughly like the following:
>
> [code]
> <?xml version='1.0' encoding='UTF-8' ?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml"
> xmlns:h="http://java.sun.com/jsf/html"
> xmlns:f="http://java.sun.com/jsf/core">
> <h:head>
> <title>Main</title>
> </h:head>
> <h:body>
> <h:form prependId="false">
> <!--h:commandLink action="#{someBackingBean.logout}" value="logout" /-->
> </h:form>
> </h:body>
> </html>
> [/code]
>
> I know the page is useless but I intentionally created it like this, so as not to clutter the example.
>
> Please, pay attention to the fact that the EL expression is in a section that's commented out. So, it should not be evaluated.
>
> When I try to display the page, however, I get an exception:
>
I think that's because there's a misunderstanding of how Facelets
works. To comment something out in Facelets, you would wrap it in
<ui:remove>. When Facelets finds the HTML comment, it assumes that you
have a comment that you want rendered to the client, which in this case,
includes parsing EL. If I recall, JSP behaved the same way (but it's
been YEARS since I've used JSP, thankfully :).

So, short answer, use <ui:remove> instead of <!-- / -- >.

> So, why does JSF attempt to evaluate EL expressions that are in comments?
>
> Furthermore, the backing bean DOES have a logout method. And if I uncomment the code, it works. No exceptions. The property exists.
>
The problem here is the difference between ValueExpressions and
MethodExpressions. Components like h:commandLink take a method
expression, so #{someBackingBean.logout} is treated correctly, as that
points to a method called logout on someBackingBean. Since this is
inside a comment, El is treating it as a ValueExpression (the text,
comment markers included, are wrapped up in a UIInstruction component so
that the "static" text can be added to the component tree and rendered),
so EL is looking for someBackingBean.getLogout(), which, apparently
doesn't exist.

Hope that helps.

-- 
Jason Lee
Senior Java Developer
GlassFish Administration Console
Sun Microsystems, Inc.
Phone x31197/+1 405-343-1964
Email jasondlee_at_sun.com
Blog http://blogs.sun.com/jasondlee
Blog http://blogs.steeplesoft.com