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

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.

Related

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.

javax.el.PropertyNotWritableException when using EL conditional operator in value of UIInput

I get this error message:
javax.el.PropertyNotWritableException: /u/editProfile.xhtml #64,140 value="#{empty userProfile.cfg.gpu or userProfile.cfg.gpu == '' ? usernavmsg.EditMe: userProfile.cfg.gpu}": null
The problem is that when the value of a bean property is null the value of an inputText field switches from my ManagedBean property, to a resource string. So I cannot persist that value.
I request the profile of the user from my database in my managed bean. If a property of the said profile is null, the inplace text holder value is "Edit Me". If it's not null it's the value of that property. Which is working ! However when I submit the form and try to persist a new value or no value at all the error appears. The error only appears for the field that are null to begin with (when I requested them from the db). So the problem is that when the value is null it switch from my ManagedBean property, to a resource string.
I just have a form with this:
<h:outputLabel value="#{usernavmsg.GPU}: "/>
<p:inplace styleClass="lessDark">
<p:inputText value="#{empty userProfile.cfg.gpu ? usernavmsg.EditMe: userProfile.cfg.gpu}" />
</p:inplace>
when I submit the form I want to persist userProfile.cfg in the database which doesn't happen.
Can I work around this ? Is it even possible ?
Conditional statements in EL are indeed not writable. They are read-only. End of story. The same applies to method references in EL as in #{bean.property()}. It must really be a true value expression as in #{bean.property}.
Your approach of using a (HTML5) "placeholder" is actually wrong. For that you should be using the placeholder attribute, not the value attribute. In older versions of PrimeFaces that do not have a placeholder attribute you'll need passthrough.
<html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...
<p:inputText value="#{userProfile.cfg.gpu}" a:placeholder="#{usernavmsg.EditMe}" ... />
Nowadays PrimeFaces offers a placeholder attribute.
<p:inputText value="#{userProfile.cfg.gpu}" placeholder="#{usernavmsg.EditMe}" ... />

Rendered attribute for Boolean type is not working in primefaces

I am using rendered attribute for p:graphicImage. In that i am checking a field which is boolean type . Rendered is not working for this boolean type. In DB this field is BIT type.
In DB, value for this field is 1. Default value in DB is 0. When i give as != 0 in rendered, it is showing as true and for == 0 in rendered, it is showing true itself.
What is the syntax for checking boolean type in rendered attribute in primefaces?
Below is my code:
<p:graphicImage value="/Images/abc.png"
rendered="#{!(MedicineList.goal eq '0')}">
</p:graphicImage>
In above code, MedicineList is dataTable's variable and goal is the field which is BIT type in mysql.
Thanks. Any help would be greatly appreciated.
If you are using BIT type in MySQL then 0 is for false and 1 is for true. So try using it like.
<p:graphicImage value="/Images/abc.png"
rendered="#{not MedicineList.goal}">
</p:graphicImage>
Also the return type of goal function should boolean.

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.

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>

Resources