JSF: ExpressionLanguage if else expression - jsf

How can I do this? I want to show a value from a entity, not a string, but how can I do this as a valid code?
itemLabel="#{mandatoryFriendship.receiver == loginBean.currentMandatory ? mandatoryFriendship.sender.surname : mandatoryFriendship.receiver.surname mandatoryFriendship.receiver.name}"
Thank you for you help

Assuming at least EL 2.2 and that the names are non-null Strings you could have an expression of the form:
#{condition ? str0 : str1.concat(str2)}
For an older version (or null Strings) you could use:
#{condition ? str0 : str1}#{condition ? str0 : str2}

Related

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...

OpenFL: Array has no field sortOn

I'm using OpenFL and I'm attempting to use this code I found...
pos.sortOn("cotangent", Array.NUMERIC | Array.DESCENDING);
...but I'm getting the error:
src/Main.hx:180: characters 2-12 : Array<{ yPoint : Float, xPoint : Float, cotangent : Float }> has no field sortOn
src/Main.hx:180: characters 26-39 : Class<Array> has no field NUMERIC
src/Main.hx:180: characters 42-58 : Class<Array> has no field DESCENDING
Now according to this 'sortOn' is an available method for AS3 arrays so what is the problem?
In Haxe you could use ArraySort.
Something like here
Haxe has a builtin standard Array class, which is what you're referencing, not AS3's Array. The Haxe Array is documented here: http://api.haxe.org/Array.html
If you don't care about a stable sort, you can use the sort method: http://api.haxe.org/Array.html#sort

Shorthand for groovy String

If I am using Groovy String Template:
http://docs.groovy-lang.org/latest/html/documentation/template-engines.html#markuptemplate-gotchas
I have a variable $name1,name2 and I want to says something like:
${name1? name1:''} ${name2? 'name2 was here, too': ''}
Is there a cleaner way to write expression like this.
Given I don't know if $name1, $name2 is null or not. If they are, printing empty or not printing anything is fine. I just don't want 'null' as the text.
There is such thing as an Elvis operator:
def foo = bar?:baz
Which is equal to calling:
def foo = bar ? bar : baz
In your case, you can use it like this:
${name1?:''}
${name1 ?: ''} ${name2 ? 'name2 was here, too' : ''}

Switch-case in haxe

I have a problem with my haxe code, So I have the following code :
var t : String = switch( id ) {
case 1 : Std.random( 2 ) == 0 ? Texts.list.0 : Texts.list.1;
case 2 : Std.random( 2 ) == 0 ? Texts.list.2 : Texts.list.3;
default: "";
}
For the default I get an error : This pattern is unused.
Can you help me please? Thx in advance
This is due that compiler detects patterns which will never match the input value http://haxe.org/manual/lf-pattern-matching-unused.html
In this case it might be bug https://github.com/HaxeFoundation/haxe/issues/4387
if You use haxe 3.2, try 3.1.3 to ensure.
In try.haxe your sample works well http://try.haxe.org/#9e54A

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

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.

Resources