How to use && in EL boolean expressions in Facelets?
Asked Answered
M

2

89

I am having a little trouble figuring out how to do and's on EL expressions in Facelets. So basically I have:

<h:outputText id="Prompt"
    value="Fobar" 
    rendered="#{beanA.prompt == true && beanB.currentBase !=null}" />

But I keep getting:

Error Traced[line: 69] The entity name must immediately follow the '&' in the entity reference.

Michikomichon answered 18/12, 2011 at 15:59 Comment(0)
R
173

Facelets is a XML based view technology. The & is a special character in XML representing the start of an entity like &amp; which ends with the ; character. You'd need to either escape it, which is ugly:

rendered="#{beanA.prompt == true &amp;&amp; beanB.currentBase != null}"

or to use the and keyword instead, which is preferred as to readability and maintainability:

rendered="#{beanA.prompt == true and beanB.currentBase != null}"

See also:


Unrelated to the concrete problem, comparing booleans with booleans makes little sense when the expression expects a boolean outcome already. I'd get rid of == true:

rendered="#{beanA.prompt and beanB.currentBase != null}"
Ribbentrop answered 18/12, 2011 at 16:3 Comment(1)
The JSF imlementation should really recognize this case and give a better diagnostic. It's a simple problem, but very confusing for beginners.Both
C
6

In addition to the answer of BalusC, use the following Java RegExp to replace && with and:

Search:  (#\{[^\}]*)(&&)([^\}]*\})
Replace: $1and$3

You have run this regular expression replacement multiple times to find all occurences in case you are using >2 literals in your EL expressions. Mind to replace the leading # by $ if your EL expression syntax differs.

Chic answered 14/3, 2013 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.