JSF EL expression compare string value - jsf

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.

Related

<h:outputText> is rendered only based on boolean check and its not rendering for String equals check

I have a code
<h:outputText id="idTxt" value="#{systemBean.checkSize}"
rendered="#{systemBean.vpmoEditFlag == true}">
The above code renders correctly in the UI.
but, when I add a condition
systemBean.checkSize eq 'Y' to the above code which will be
<h:outputText id="idTxt" value="#{systemBean.checkSize}"
rendered="#{systemBean.vpmoEditFlag == true and systemBean.checkflag eq 'Y'}">
is not redering in the UI.
Both the variables
vpmoEditFlag & checkflag
are declared globally in the backing bean which is systemBean.
I tried "==" for instead of "eq" for the above condition but its does not work.
What might be the problem ? Any help will be really helpful to resolve this issue.
Please make sure checkflag is String datatype. Both eq and == are valid.
If checkflag is String then 'Y' should work.
Check this out http://docs.oracle.com/javaee/6/tutorial/doc/bnaik.html for more info.

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.

Convert double to int in inline-text

<o:importFunctions type="java.lang.Math" />//omnifaces, see http://showcase.omnifaces.org/taghandlers/importFunctions
<c:set var="ordersToShow" value="${Math:min(5, processedOrders.size())}" /> // processedOrders is a List
<p:fieldset legend="Last Stuff (${ordersToShow})">[...]</p:fieldset>
${ordersToShow} is of type java.lang.Double (output of "${ordersToShow.class}") and prints sth. like "Last Stuff 2.0".
I want to have the output like "Last Stuff 2", how can I do that?
f:convertNumber will throw error ( Parent not an instance of ValueHolder) for p:fieldSet if its used inside.
Instead you can keep an string in managed bean and in its getter you can use String.format("%.0f", ordersToShow);
Else keep an binding variable for fieldSet and set the above formatted value in bean.
You can use f:convertNumber. Keeping minFractionDigits="0" should do the trick.
See here http://www.tutorialspoint.com/jsf/jsf_convertnumber_tag.htm
I have not used p:fieldset so now sure how the f:convertNumber can be used with the legend but seems not possible.
You can use this, replace c:set and p:fieldset lines with the below. The fmt tag explained here http://www.tutorialspoint.com/jsp/jstl_format_formatnumber_tag.htm
<fmt:formatNumber var="ordersToShow" type="number" maxFractionDigits="0" value="${...}" />
<p:fieldset legend="Last Stuff (${ordersToShow})">[...]</p:fieldset>

Conditionally render JSF h:panelGrid based on presence of query string (or no query string at all)

I'm trying to dynamically render some composition/templates based on the presence of a query string or the absence of a query string. For example:
localhost:9080/myWonderfulApp/test.xhtml?foo=bar
This works, A.xhtml gets pulled in.
localhost:9080/myWonderfulApp/test.xhtml
This doesn't work, B.xhtml does not get pulled in.
I'm having problems with the absence of a query string part. I can render A.xhtml when I pass ?foo=bar, but I can't seem to get B.xhtml rendered when I have no query string. I've tried some variations, I initially figured #{param['foo'] != 'bar' would work, but I think that's not working because I don't have a foo query param at all. I've tried checking if param == null but that didn't work either.
Is it possible to set my rendered attribute based on NO query string?
I can't just set another query string for B.xhtml, either as I'm working on a legacy app that gets 2 different skins, so retrofitting all the old apps that link in isn't an option. New apps will use a query string, old ones need to get an old skin linking in with no query string.
<!--This works-->
<h:panelGrid rendered="#{param['foo'] == 'bar' ? true : false}">
<ui:composition template="A.xhtml">...</ui:composition>
</h:panelGrid>
<!--This doesn't work-->
<h:panelGrid rendered="#{param == null ? true : false}">
<ui:composition template="B.xhtml">...</ui:composition>
</h:panelGrid>
This doesn't seem to work, either:
<h:panelGrid rendered="#{empty facesContext.externalContext.requestParameterMap.foo ? true : false}">
You just need the inverse condition: #{param.foo != 'bar'}. Those conditional operators returning booleans are unnecessary, the equation itself returns a boolean already. The #{param} is never null. It returns a Map<String, String> which is just empty (not null!) when it doesn't contain any parameters. The brace notation is only useful when the parameter name contains periods which would otherwise be interpreted as nested properties, but request parameter names shouldn't contain periods anyway.
So, this should do:
<h:panelGrid rendered="#{param.foo == 'bar'}">
<ui:composition template="A.xhtml">...</ui:composition>
</h:panelGrid>
<h:panelGrid rendered="#{param.foo != 'bar'}">
<ui:composition template="B.xhtml">...</ui:composition>
</h:panelGrid>
As a different alternative, if you want to use the conditional operators the right way, you can also do
<ui:composition template="#{param.foo == 'bar' ? 'A' : 'B'}.xhtml">...</ui:composition>

Using a variable property inside msg

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']}" />

Resources