Avoid Date field showing as null in XHTML - jsf

I am rendering a date field in XHTML using JSF. Since the value in DB is saved as null the date field values is shown as "null" in the UI. How to avoid this null?
My code is given below:
<h:outputFormat value="{0, date, MM/dd/yyyy}"
rendered="#{sysbean.xx.isfeed == '1' or (sysbean.yy== false and sysbean.details.endate!= null)}">
<f:param value="#{sysbean.details.endate}" /></h:outputFormat>
The output I am getting is
Project Name : null
How can I avoid the value coming as "null"?

<f:param value="#{value to check!=null?value when condition is true:
value when condition is false}" />
Checking the null at EL expression,
or in the bean also you can put the inline check for null values.

Related

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>

JSF: Ternary operator used to assign a value to an <f:param> fails

I am using JSF tags within an XHTML file. My intention is to enable or disable a <rich:MenuItem> context-menu item by setting the "disabled" attribute to "true" or "false" appropriately. To do this, I am using a backing bean variable in a ternary operator and setting an <f:param> value to either "true" or "false" based on the bean variable, as below:
<rich:componentControl event="oncontextmenu" for="network-tree-menu"
operation="show">
<f:param id="nestlevel" value="#{item.nestLevel > 10 ? 'true' : 'false'}"
name="nestlevel" />
</rich:componentControl>
where item is the backing bean and item.nestLevel is an integer.
I am using this <f:param> value later in the XHTML file as below:
<rich:contextMenu ...
<rich:menuItem id="abc" ajaxSingle="true" disabled="{nestlevel}"
onclick="doSomething();" value="Do something...">
</rich:contextMenu>
This is not working !! The menu item is always enabled, (I guess this is the default behaviour) even though the result of the ternary operation is "true". Is there something I am missing here w.r.t the syntax, or is there some other way I can do this conditional enabling of context-menu items within the XHTML file?
Thanks in advance.
Regards,
Nagendra U M
How about this:
<f:param id="nestlevel" value="#{item.nestLevel > 10}" name="nestlevel" />

Resources