Using a variable property inside msg - jsf

So I am working on an app and I have it set up so the following line
<h:outputText value = "#{msg['properties.help.keys.example.text']}" />
Will print some output value from a properties file. What I want is to make part of that string it uses to find the properties variable.
I.E.
<h:outputText value = "#{msg['properties.help.keys.' + cc.attrs.key + '.text']}" />
Where cc.attrs.key is a value I pass into the xhtml. Is this possible if so how do you do it?

You can't concatenate strings in EL expressions like that. The + is in EL exclusively a sum operator for numbers. You need to use <c:set> to concatenate strings with EL expressions before nesting it in another EL expression. Concatenating the string is then solely be done by just inlining the expression.
<c:set var="key" value="properties.help.keys.#{cc.attrs.key}.text" />
<h:outputText value="#{msg[key]}" />
In the upcoming EL 3.0, there will be a new EL operator & for concatenating strings in EL expressions. The use of the & character as operator is however discutable in XML based view technologies as it's a reserved XML character, I've been in discussion with EL guys about that. It should be possible with an alternative operator like ct which is in line with gt, lt, etc.
Update: in EL 3.0, there is the new EL operator += for concatenating strings in EL expressions. Your use case can then be solved as follows:
<h:outputText value="#{msg['properties.help.keys' += cc.attrs.key += '.text']}" />

Related

JSF render contents of attribute dynamically [duplicate]

I need to create a callback for <h:commandButton> while as a parameter I need to pass an argument that is string-concatenated with an external parameter id:
I tried nesting an EL expression something like this:
<h:commandButton ... action="#{someController.doSomething('#{id}SomeTableId')}" />
However this failed with an EL exception. What is a right syntax/approach to do this?
If you're already on EL 3.0 (Java EE 7; WildFly, Tomcat 8, GlassFish 4, etc), then you could use the new += operator for this:
<h:commandButton ... action="#{someController.doSomething(id += 'SomeTableId')}" />
If you're however not on EL 3.0 yet, and the left hand is a genuine java.lang.String instance (and thus not e.g. java.lang.Long), then use EL 2.2 capability of invoking direct methods with arguments, which you then apply on String#concat():
<h:commandButton ... action="#{someController.doSomething(id.concat('SomeTableId'))}" />
Or if you're not on EL 2.2 yet, then use JSTL <c:set> to create a new EL variable with the concatenated values just inlined in value:
<c:set var="tableId" value="#{id}SomeTableId" />
<h:commandButton ... action="#{someController.doSomething(tableId)}" />
See also:
String concatenation in EL for dynamic ResourceBundle key

JSF EL expression compare string value

I am trying to evaluate EL expression (method expression?) that returns a value (String) if that value is equal to "bar" then I would like to render the tag.
<p:tab rendered="#{bean.getAnswer('foo').answer == "bar"}" />
However I get following error message.
Invalid location of text ("}") in tag.
What would be the right syntax to use?
Edit: Remove the double quotes arround bar, it's also such a String. You can use == also to compare Strings. I preffer eq for more readable.
<p:tab rendered="#{bar eq bean.getAnswer('foo')}" />
<p:tab rendered="#{bar == bean.getAnswer('foo')}" />
All operators can you found here. http://docs.oracle.com/javaee/6/tutorial/doc/bnaik.html
THX #Jasper de Vries
The problem I see here is the use of "" for the value bar, as it is conflicting with the outer "".
Have it wrapped inside single quotes, like so 'bar'.
Hope this helps.
UPDATE:
Using 'eq' with String makes it more readable. However, == works too.

Appending Strings in global resource bundle [duplicate]

I need to create a callback for <h:commandButton> while as a parameter I need to pass an argument that is string-concatenated with an external parameter id:
I tried nesting an EL expression something like this:
<h:commandButton ... action="#{someController.doSomething('#{id}SomeTableId')}" />
However this failed with an EL exception. What is a right syntax/approach to do this?
If you're already on EL 3.0 (Java EE 7; WildFly, Tomcat 8, GlassFish 4, etc), then you could use the new += operator for this:
<h:commandButton ... action="#{someController.doSomething(id += 'SomeTableId')}" />
If you're however not on EL 3.0 yet, and the left hand is a genuine java.lang.String instance (and thus not e.g. java.lang.Long), then use EL 2.2 capability of invoking direct methods with arguments, which you then apply on String#concat():
<h:commandButton ... action="#{someController.doSomething(id.concat('SomeTableId'))}" />
Or if you're not on EL 2.2 yet, then use JSTL <c:set> to create a new EL variable with the concatenated values just inlined in value:
<c:set var="tableId" value="#{id}SomeTableId" />
<h:commandButton ... action="#{someController.doSomething(tableId)}" />
See also:
String concatenation in EL for dynamic ResourceBundle key

JSF2 variable element of bundle [duplicate]

I need to create a callback for <h:commandButton> while as a parameter I need to pass an argument that is string-concatenated with an external parameter id:
I tried nesting an EL expression something like this:
<h:commandButton ... action="#{someController.doSomething('#{id}SomeTableId')}" />
However this failed with an EL exception. What is a right syntax/approach to do this?
If you're already on EL 3.0 (Java EE 7; WildFly, Tomcat 8, GlassFish 4, etc), then you could use the new += operator for this:
<h:commandButton ... action="#{someController.doSomething(id += 'SomeTableId')}" />
If you're however not on EL 3.0 yet, and the left hand is a genuine java.lang.String instance (and thus not e.g. java.lang.Long), then use EL 2.2 capability of invoking direct methods with arguments, which you then apply on String#concat():
<h:commandButton ... action="#{someController.doSomething(id.concat('SomeTableId'))}" />
Or if you're not on EL 2.2 yet, then use JSTL <c:set> to create a new EL variable with the concatenated values just inlined in value:
<c:set var="tableId" value="#{id}SomeTableId" />
<h:commandButton ... action="#{someController.doSomething(tableId)}" />
See also:
String concatenation in EL for dynamic ResourceBundle key

JSF Hide Negative Mark when using f:convertNumber

I want to hide the minus value from the print during formatting in JSF. How can I achieve that.
For Example,
The Value : -340
Need to Display as : 340.00
Not like -340.00 or (340.00)
Is that possible? The current code is like that, but can not hide minus value.
<h:outputText value="#{paymentBill.amount}" >
<f:convertNumber pattern="#0.00" />
</h:outputText>
If it's <h:outputText> then you can use arbitrary EL expression as its value, if you're so dissatisfied with getting absolute value while preparing data in your managed bean, as it's rightly proposed by perissf and fareed, like:
value="#{(paymentBill.amount lt 0) ? -paymentBill.amount : paymentBill.amount}"
You can return the absolute value of the amount In the backing bean.
Return Math.abs(amount) instead of amount.

Resources