Using && in EL results in error: The entity name must immediately follow the '&' in the entity reference
Asked Answered
A

2

13

I'm trying to use a conditional expression in an el expression used in jsf, but it does not work.

<h:outputText value="#{(sel.description !=null) && (sel.description !='') ? sel.description : 'Empty description'} - "/>

but it does not work, the compiler says:

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

Do you have any suggestions? Thank you!

Arsenault answered 13/11, 2012 at 11:23 Comment(0)
B
30

You seem to be using Facelets (which is perfectly fine). It's however a XML based view technology. Everything which you write in Facelets has to be syntactically valid XML. The & is in XML a special character denoting the start of an entity like &amp;, &lt;, &gt;, &#160;, etc.

If you would like to represent the & as-is in XML, then you'd have to replace it by &amp;.

<h:outputText value="#{(sel.description !=null) &amp;&amp; (sel.description !='') ? sel.description : 'Empty description'} - "/>

However, that's not pretty readable, you would rather like to use the alternative EL operator and for this (see also operators in EL for an overview of all available operators in EL):

<h:outputText value="#{(sel.description !=null) and (sel.description !='') ? sel.description : 'Empty description'} - "/>

All with all, this is in turn pretty clumsy as there's a simpler empty keyword for the purpose of testing both nullness and emptiness. This can in your particular case be used as:

<h:outputText value="#{not empty sel.description ? sel.description : 'Empty description'} - "/>

or

<h:outputText value="#{!empty sel.description ? sel.description : 'Empty description'} - "/>

or

<h:outputText value="#{empty sel.description ? 'Empty description' : sel.description} - "/>
Boldt answered 13/11, 2012 at 11:35 Comment(1)
Wow, great explanation. &amp;&amp; worked for me, but I decided to go with 'and' instead, for readability. Thanks for the tip.Rouse
M
5

Use and instead of &&. You simply have XML syntax error.

Milne answered 13/11, 2012 at 11:28 Comment(1)
@Arsenault it's not just about making it work, instead learning how the technology works.Countermarch

© 2022 - 2024 — McMap. All rights reserved.