How to use && in EL boolean expressions in Facelets? - jsf

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.

Facelets is a XML based view technology. The & is a special character in XML representing the start of an entity like & which ends with the ; character. You'd need to either escape it, which is ugly:
rendered="#{beanA.prompt == true && 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:
Java EE 6 tutorial - Operators in EL
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}"

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.

Related

What is the difference between these Peewee query filter forms

In the description of filtering records in Peewee, there are examples of two alternative syntaxes: using commas to separate multiple conditions, such as the following example,
Tweet.select().where(Tweet.user == user, Tweet.is_published == True)
and using bitwise operators. I cannot figure out (and cannot find a description of) the difference between using the comma syntax and using bitwise operators. What does the comma syntax actually do? From the (single) documented example of using a comma, it seems like it might be equivalent to using &, as in
Tweet.select().where( (Tweet.user == user) & (Tweet.is_published == True) )
Is that the case?
Yes, they are equivalent, as per the code:
def where(self, *expressions):
if self._where is not None:
expressions = (self._where,) + expressions
self._where = reduce(operator.and_, expressions)

Ternary condition in El expression

When I use ternary condition in El Expression I get the eclipse warning message "cannot be result as a member" in the false expression.
#{sessionController.originalURI != null ?
sessionController.originalURI : request.contextPath}
In this case I got the message "contextPath cannot be resolved as a member of originalURI"
Try with this:
#{sessionController.originalURI ne null ?
sessionController.originalURI : request.contextPath}
I think it also could be an answer.
I don't know why but reversing the ternary solved the problem
#{sessionController.originalURI == null ? request.contextPath : sessionController.originalURI}
Use empty
http://docs.oracle.com/javaee/6/tutorial/doc/bnaik.html
#{not empty sessionController.originalURI ?
sessionController.originalURI : request.contextPath}
Valid too:
#{!empty sessionController.originalURI...

dustjs OR condition over multiple parameters

I have an object with multiple properties viz propA, propB propC and propD. I want to write a condition with OR checking more than one parameter like below.
{#if cond="'{obj.propA}'.length > 0
|| '{obj.propB}'.length > 0 || '{obj.propC}'.length> 0}
...
{/if}
Now since #if is deprecated in dust, how do i write an equivalent of this with eq or select. Or is there a new helper i can utilize for such scenarios.
I'm assuming that the props you're testing are strings.
This example requires dustjs-helpers >= 1.6.
You can use the {#any} and {#none} helpers mentioned by #rragan like this:
{#select}
{#ne key=obj.propA value="" /}
{#ne key=obj.propB value="" /}
{#ne key=obj.propC value="" /}
{#any}At least one of the above tests was true. At least one prop is not an empty string.{/any}
{#none}None of the tests above passed. All the props are empty{/none}
{/select}
select was recently extended with #any and #none that let you do multiple OR logic. Note that the .length only works because the deprecated #if uses eval. Dust tests use existence/non-existence so I think you can avoid using .length.
If you still prefer #if, see https://github.com/rragan/dust-motes/tree/master/src/helpers/control/if for an interpretive version of it that does not use eval.

how to handle conditionally existing components in action code?

This is another problem I am facing while migrating from antlr3 to antlr4. This problem is with the java action code for handling conditional components of rules. One example is shown below.
The following grammar+code worked in antlr3. Here, if the unary operator is not present, then a value of '0' is returned, and the java code checks for this value and takes appropriate action.
exprUnary returns [Expr e]
: (unaryOp)? e1=exprAtom
{if($unaryOp.i==0) $e = $e1.e;
else $e = new ExprUnary($unaryOp.i, $e1.e);
}
;
unaryOp returns [int i]
: '-' {$i = 1;}
| '~' {$i = 2;}
;
In antlr4, this code results in a null pointer exception during a run, because 'unaryOp' is 'null' if it is not present. But if I change the code like below, then antlr generation itself reports an error:
if($unaryOp==null) ...
java org.antlr.v4.Tool try.g4
error(67): missing attribute access on rule reference 'unaryOp' in '$unaryOp'
How should the action be coded for antlr4?
Another example of this situation is in if-then-[else] - here $s2 is null in antlr4:
ifStmt returns [Stmt s]
: 'if' '(' e=cond ')' s1=stmt ('else' s2=stmt)?
{$s = new StmtIf($e.e, $s1.s, $s2.s);}
;
NOTE: question 16392152 provides a solution to this question with listeners, but I am not using listeners, my requirement is for this to be handled in the action code.
There are at least two potential ways to correct this:
The "ANTLR 4" way to do it is to create a listener or visitor instead of placing the Java code inside of actions embedded in the grammar itself. This is the only way I would even consider solving the problem in my own grammars.
If you still use an embedded action, the most efficient way to check if the item exists or not is to access the ctx property, e.g. $unaryOp.ctx. This property resolves to the UnaryOpContext you were assuming would be accessible by $unaryOp by itself.
ANTLR expects you access an attribute. Try its text attribute instead: $unaryOp.text==null

Using greater than logical expression in rendered attribute

I have an outputText field for which I write a condition in the rendered attribute. The condition is for comparing the length of the string with some numeric value.
<h:outputText id="emailaddress"
value ="#{subsAlertsHelper.personEmail.substring(0,20)}"
rendered="#{subsAlertsHelper.personEmail.length() >20}" />
If I use == or != in rendered it is working fine. But for greaterthan and lessthan it is not giving the output. What could be the reason for that?
You have to use gt and lt operators.
Check out JavaServer Faces Expression Language Intro from Sun/Oracle. Precisely the Operators section.
rendered only accepts EL expression.
subsAlertsHelper.personEmail.length() is incorrect.
On the personEmail object, add a method getLength() witch returns the length
public int getLength(){ return this. length();}
Modify :
rendered="#{subsAlertsHelper.personEmail.length >20}"

Resources