JSF 2.0: outputText does not print date - jsf

I have the following code:
<h:outputText value="#{java.util.Calendar.getInstance().getTime()}">
<f:convertDateTime pattern="MM/dd/yyyy" type="date" />
</h:outputText>
which prints nothing.
When I use the following code:
<h:outputText value="#{group.effectiveDate}">
<f:convertDateTime pattern="MM/dd/yyyy" type="date" />
</h:outputText>
it works. Does anyone know why invoking java.util.Calendar.getInstance().getTime() does not work? I'm using Mojarra.

java.util.Calendar.getInstance().getTime() isn't a valid EL expression. Remember that you can only access beans through EL.
This would either be ones you explicitly create, or ones that are exposed by the container.
when the EL parser sees #{java.util}, it looks for a managed bean named java which has a getUtil() method.
Otherwise, it will probably say something along the lines of "Unable to find managed bean: 'java'".

Related

<pe:keyFilter > not working in <ui:repeat>. Throwing Exception for id not found

<pe:keyFilter> is working with single <p:inputText> but when i go for inputText in repeat it is not working and throwing an exception.
Like
<p:inputText value="text2" id="text2" />
<pe:keyFilter for="text2" regEx="/[ABC]/i"/>
It is working fine but below code is not working and throwing exception for id text-0 not found
<ui:repeat var="answer" value="#{answerBean.answerList}" varStatus="answerStatus" >
<p:inputText value="#{answer.answerText}" id="text-#{answerStatus.index}" />
<pe:keyFilter for="text-#{answerStatus.index}" regEx="/[ABC]/i" />
</ui:repeat>
You don't need to worry about forcing unique IDs inside JSF iteration components. JSF already does that all by itself. Simply get rid of varStatus and all unnecessity related to that and let JSF do its job.
<ui:repeat value="#{answerBean.answerList}" var="answer">
<p:inputText id="text" ... />
<pe:keyFilter for="text" ... />
</ui:repeat>
It's only necessary if you were using <c:forEach> taghandler instead of <ui:repeat> component. See also JSTL in JSF2 Facelets... makes sense?

Converter can't get rich:dataGrid rows as custom parameters

I need a converter with custom parameters, I've made it and it works well, except when I use it with row elements and I can't understand why, ideas?
Examples
This works well:
<h:outputText value="#{bean.value}">
<cc:converter param="#{bean.attribute}" />
</h:outputText>
also this one:
<h:outputText value="#{bean.value}">
<cc:converter param="fixedValue" />
</h:outputText>
this one doesn't work at all, param is null inside the converter:
<rich:dataGrid value="#{bean.list}" var="row">
<h:outputText value="#{row.value}">
<cc:converter param="#{row.attribute}" />
</h:outputText>
</rich:dataGrid>
See BalusC link. It describes why your converter doesn't work. Converter without parameters works fine inside datatable in JSF 1.2. For simulate conversion you can use getter method in object which represents row like getConvertedValue and move your code from converter into this method. After that in datatable you can call #{row.convertedValue} for displaying the converted value.

Format a JSF converted value

Today I noticed ocpsoft has a nice time library adapted to use in JSF 2 as a converter. The strong point of that is you can use the converter directly in the date displayed in the view and it converts it into a string telling the user something like 6 hours ago or 17 hours from now. I think my best is to combine both, the JSF converted date and this one to display something like 26-03-2013 17:00 (4 hours from now). I can achieve something similar with the following code:
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>
My problem comes when I want to put the second value into parenthesis. The PrettyTimeConverter accepts only a date as a value and I can't write the parenthesis there directly. Also JSF is not accepting the following:
<h:outputFormat value="({0})">
<f:param value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</f:param>
</h:outputFormat>
With that I have the following error:
<f:converter> Parent not an instance of ValueHolder:
javax.faces.component.UIParameter#1492636
Any idea about how to achieve that avoiding writing both parentheses using specific h:outputText tags?
You can just put those parentheses directly in template text without the need for another <h:outputText>s.
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
(<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>)
See also:
Is it suggested to use h:outputText for everything?

JSF2 Building Dynamic EL Expressions

I am working on creating a dynamic table using JSTL forEach and a h:dataTable and have all of the controls and potential error message showing nicely, but am now stuck on getting the value set for each of the control. I will need to create (I think) a dynamic EL expression to set the value, but have not been able to get any of my versions to work. I was hoping to build out the expression using c:out, but found out that that tag is not available in JSF2.
So, is it possible to build a dynamic expression in the page?
How can I set the expression in the backing bean if the control hasn't been built yet ?
<h:dataTable id="dtDetails" styleClass="slate_table" value="#{remediationDetail.eventList}" var="dataItem">
<c:forEach items="#{remediationDetail.eventHeaders}" var="key">
<h:column>
<f:facet name="header">#{key.fieldDefinition.fieldConfiguration.customLabel}</f:facet>
<h:inputText value="" id="txtNumber" styleClass="remediation_textbox error_marker" title="#{remediationDetail.errorMessages(dataItem.id, key.fieldDefinition.id)}">
<f:convertNumber maxFractionDigits="0" maxIntegerDigits="19"/>
</h:column>
</c:forEach>
</h:dataTable>
As always, any help or direction is appreciated.
Regards,
Mike
I was not being able to create a Dynamic EL Expression. So, I ended using the index of the c:forEach to help define what values I am looking for. The only catch for this result is that I would be expecting the data that I am about to display to have the same number of positions in the array.
<c:forEach items="#{remediationDetail.eventHeaders}" var="key" varStatus="looper">
<h:column>
<f:facet name="header">#{key.fieldDefinition.fieldConfiguration.customLabel}</f:facet>
<h:inputText id="txtNumber" value="#{dataItem.entityList[looper.index].val}">
<f:convertNumber maxFractionDigits="0" maxIntegerDigits="19"/>
</h:inputText>
</h:column>
</c:forEach>
In my case the following helped to build dynamic EL.
<ui:param name="beanName" value="myBackedBean"/>
<ui:param name="propertyName" value="field1"/>
<ui:param name="nestedPropertyName" value="field1"/>
<p:inputTextarea value="#{sessionScope[beanName][propertyName][nestedPropertyName]}"/>
Inspired of this topic

combining rich:inplaceInput with rich:suggestionbox

We want to pre-fill our richfaces suggestion-box with a inplace text.
Are there any recommendations on how to do this? I think, simple replacement of h:inputText to rich:inplaceInput will not work.
<h:inputText id="field" value="#{form.field}" required="true" /><rich:in
<rich:suggestionbox id="suggestionBoxId" for="field" suggestionAction="#{suggestion.autocomplete}" var="result" fetchValue="#{result.zip} #{result.cityName}" minChars="3">
<h:column>
<h:outputText value="#{result.text}" />
</h:column>
</rich:suggestionbox>
If I understand the question correctly, you could simply define form.field as whatever you want to pre-fill with in the #PostConstruct method of the form bean. Or even the constructor, really.

Resources