Ternary condition in El expression - jsf

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

Related

Expected string literal in condition express in Jenkins pipeline

I am using ?: to determine the build agent of Jenkins shared library groovy script like this:
def call(String type, Map map) {
if (type == "gradle") {
pipeline {
agent "${map.agent == null}" ? "any" : "${map.agent}"
}
}
}
but it gives me the following error:
org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException: startup failed:
/Users/dabaidabai/.jenkins/jobs/soa-robot/builds/154/libs/pipeline-shared-library/vars/ci.groovy: 6: Expected string literal # line 6, column 42.
agent "${map.agent == null}" ? "any" :
^
/Users/dabaidabai/.jenkins/jobs/soa-robot/builds/154/libs/pipeline-shared-library/vars/ci.groovy: 6: Only "agent none", "agent any" or "agent {...}" are allowed. # line 6, column 13.
agent "${map.agent == null}" ? "any" : "${map.agent}"
^
/Users/dabaidabai/.jenkins/jobs/soa-robot/builds/154/libs/pipeline-shared-library/vars/ci.groovy: 6: No agent type specified. Must be one of [any, docker, dockerfile, label, none] # line 6, column 13.
agent "${map.agent == null}" ? "any" : "${map.agent}"
What am I doing wrong?
This error is thrown by the pipeline syntax validator that runs before your pipeline code gets executed. The reason you see this error is the following:
Only "agent none", "agent any" or "agent {...}" are allowed. # line 6, column 13.
This is the constraint for the label section. It means that the following values are valid:
agent any
agent none
agent "constant string value here"
agent { ... }
When you pass something like:
agent "${map.agent ?: 'any'}
agent(map.agent ?: 'any')
you are getting Expected string literal because any form of an expression is not allowed in this place, including interpolated GStrings of any form.
Solution
There is a way to define pipeline agent dynamically however. All you have to do is to use a closure block with the label set to either expression or empty string (an equivalent of agent any in this case.)
pipeline {
agent {
label map.agent ?: ''
}
stages {
...
}
}
The label section allows you to use any expression, so map.agent is a valid construction here. Just remember to use an empty string instead of "any" - otherwise Jenkins will search for a node labeled as "any".
Don't use string replacments everyhwhere:
agent(map.agent==null ? "any" : map.agent)
Or get groovy:
agent(map.agent?:"any")
The actual problem is most likely the "ternary operator" battling against the "parens are maybe optional"-rule.
It seems to me that agent is a method taking a string argument and the way your code is written is ambiguous. Try surrounding the argument expression with parens:
agent(map.agent == null ? "any" : "${map.agent}")
I removed the quotes around map.agent == null as they seemed extraneous.
Also this could probably be rewritten using the groovy "elvis operator" (:?) as:
agent(map.agent ?: "any")
Which essentially means "use map.agent if it has a value, otherwise use 'any'". "If it has a value" is in this context being defined using groovy truth where both empty string and null represent "no value".

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' : ''}

JSF: ExpressionLanguage if else expression

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}

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.

What do Groovy assertions look like with parentheses?

The example on this page only shows Groovy assertions without parentheses.
assert a != null, 'First parameter must not be null'
What would this look like if I wanted to include the parentheses? I presume that this is the closest equivalent to Perl's die() function (print error message and exit in one statement)?
The answer is:
assert(a != null), "First parameter must not be null"

Resources