JSF EL expressions to check empty string in propery file? - jsf-2

I have to check if my property file is empty for certain label based on that I have to render the element but even when the label is empty i still get the element displaying key:
<h:panelGroup rendered="#{not empty I18N['key_hint_message'] }">
<h:outputLabel id="hint_label" value="#{I18N['key_label_hint']}
"></h:outputLabel>
<h:outputText value="#{I18N['key_hint_message']}" ></h:outputText>
</h:panelGroup>

You can use ResourceBundle#containsKey() for this.
<h:panelGroup rendered="#{I18N.containsKey('key_hint_message')}">
<h:outputLabel value="#{I18N['key_label_hint']}" />
<h:outputText value="#{I18N['key_hint_message']}" />
</h:panelGroup>
You'd better not rely on default format of missing keys as this can be overriden by a custom resource bundle resolver.

JSF implementation displays by defaults missing resources as ???resource???, So you can use the fn:contains JSTL function in your rendered attribute like this:
<h:panelGroup rendered="${not fn:contains(I18N['key_hint_message'], '???')}">
<h:outputLabel id="hint_label" value="#{I18N['key_label_hint']}"/>
<h:outputText value="#{I18N['key_hint_message']}" />
</h:panelGroup>

Related

Display JSF element only if specific h:message is being displayed

I have a h:form with several inputs and each of them got its own h:message and I'm looking for a way to show (using render or assigning some styleClass) some other element only when specific h:message is being shown (and hide when that h:message is not being displayed).
Here a code snippet
<li>
<h:panelGroup id="current_password_wrapper">
<p:password value="#{myBean.myCurrPass}" id="current_password" required="true"
styleClass="required_field" />
</h:panelGroup>
<h:message for="current_password"/>
</li>
<li>
<h:panelGroup id="new_password_wrapper">
<p:password value="#{myBean.myNewPass}" id="new_password" required="true"/>
</h:panelGroup>
<h:message for="new_password"/>
<h:commandLink value="my value"/>
</li>
I want to make the h:commandLink visible only when the <h:message for="new_password"/> is being displayed
So far I couldn't find anything...
If your environment supports EL 2.2, then you could check if FacesContext#getMessageList() isn't empty for the particular client ID.
<p:password binding="#{new_password}" ... />
<h:commandLink ... rendered="#{not empty facesContext.getMessageList(new_password.clientId)}" />
If the message is being shown as result of a validation error, then you could also just check the UIInput#isValid() state of the component associated with the message.
<p:password binding="#{new_password}" ... />
<h:commandLink ... rendered="#{not new_password.valid}" />
Note that manually adding a faces message to the context won't mark the input component invalid. Therefor either a true Validator should be used which throws a ValidatorException, or an explicit input.setValid(false) call has to be done programmatically.
I think with the answer to this question you requirement can be archived:
How to number JSF error messages and attach number to invalid component
I think you can do something like this:
<h:outputText value="output text" rendered="#{bean.messageIndexes['form:input1'] > 0}" />

Can't dynamically set component identifier

I have a p:inputText field inside a dataTable and I'm trying to set its identifier dynamically.
However I get the following error:
component identifier must not be a zero-length String
None of the String used as id are null or of zero-length
<h:form id="updateform">
<p:dataTable id="updatetable" value="#{EditingBean.row}" var="column"
style="width: 983px; overflow-x: auto; white-space: normal;">
<f:facet name="header">
<h:outputText value="#{EditingBean.currentStatement.statementName}" />
</f:facet>
<p:column rendered="#{column.display}" style="white-space: normal;">
<h:outputText value="#{column.alias}" />
</p:column>
<p:column rendered="#{column.display}" style="white-space: normal;">
<p:inputText id="#{column.name}" value="#{column.value}" />
</p:column>
<f:facet name="footer" style="text-align: right;">
<h:commandButton value="Update" action="#{EditingBean.update()}"
ajax="false" />
</f:facet>
</p:dataTable>
</h:form>
The id attribute must be set during view build time.
However, as #{column} is declared as <h:dataTable var>, it's only available during view render time. As it runs after view build time, it's thus null during view build time.
I'm not sure about the concrete functional requirement for which you thought that doing this would be the right solution. The code is not self-documenting enough to properly guess it. So it's hard to give a suitable answer. In any case, you've basically 2 options:
Use an iterating tag which runs during view build time, such as JSTL <c:forEach>.
<c:forEach items="#{EditingBean.row}" var="column">
...
<p:inputText id="#{column.name}" ... />
You perhaps actually don't need a dynamic ID at all. Give it a fixed ID.
<p:inputText id="name" ... />
JSF will already take care of generating unique IDs (prefixed with row index) in HTML output. Rightclick page and do View Source to see it yourself.
See also:
JSTL in JSF2 Facelets... makes sense?

f:param is not working with h:graphicImage

I'm getting <f:param> i.e., menuItemIndex value as null during my ajax call. Is it because it is with in <h:graphicImage> ??? Can any one suggest please? Note: Parameter is getting passed if I use <h:commandLink> instead of <h:graphicImage>.
<c:forEach items="#{cc.attrs.value}" var="menuItem" varStatus="loopItem">
....
<ui:fragment rendered="#{menuItem.hasChildren}">
<h:graphicImage library="images" name="#{menuItem.symbol}">
<f:ajax render=":fatcaForm:myMenu:menuID" event="click" listener="#{menuBean.refreshMenu}" />
<f:param name="menuItemIndex" value="{loopItem.count}" />
</h:graphicImage>
</ui:fragment>
....
</c:forEach>
The <h:graphicImage> is not a command component and it does therefore not support <f:param> children at all. The <f:param> works only when nested in an UICommand component, such as <h:commandLink> as you found out yourself.
So, this should do:
<h:commandLink action="#{menuBean.refreshMenu}">
<h:graphicImage library="images" name="#{menuItem.symbol}" />
<f:ajax render=":fatcaForm:myMenu:menuID" />
<f:param name="menuItemIndex" value="#{loopItem.count}" />
</h:commandLink>
(note that I fixed the EL syntax error in the <f:param value> as well)
Unrelated to the concrete problem, how you used the <h:graphicImage library> is not entirely right. Please carefully read What is the JSF resource library for and how should it be used?

Cascade dynamic rendering selectOneMenu doesn't work

My problem I thougth is simple but I can't find a clear solution.
I have three selectOneMenu, and I want that the first one is always rendered, the second one is rendered if the first one has some value selected and the third one is rendered if the second has some value selected.
The relation between the first one and the second works well, but doesn't work between the second and the third.
When I change the value for the first selectOneMenu the second selectOneMenu is diplayed or hidden correctly.
But when I change the value for the second nothing happened to the third selectOneMenu, like if the f:ajax render isn't fired.
Bellow the jsf code:
<h:panelGrid columns="2">
<h:outputText value="Type Paiement" />
<h:selectOneMenu
value="#{employeurBean.idTypePaiement}">
<f:selectItem itemValue="" itemLabel="Choix typePaiement" />
<f:selectItems value="#{typePaiementBean.typesPaiement}" var="vtp"
itemLabel="#{vtp.libelle}" itemValue="#{vtp.idTypePaiement}" />
<f:ajax event="change" render="gmodp" />
</h:selectOneMenu>
</h:panelGrid>
<h:panelGrid id="gmodp">
<h:panelGroup rendered="#{employeurBean.idTypePaiement == 2}">
<h:outputText value="Mode Paiement" />
<h:selectOneMenu
value="#{employeurBean.idModePaiement}">
<f:selectItem itemValue="" itemLabel="Choix mode Paiement" />
<f:selectItems value="#{modePaiementBean.modesPaiement}" var="vmp"
itemLabel="#{vmp.libelle}" itemValue="#{vmp.idModePaiement}" />
<f:ajax event="change" render="grib"/>
</h:selectOneMenu>
</h:panelGroup>
</h:panelGrid>
<h:panelGrid id="grib">
<h:panelGroup rendered="#{employeurBean.idModePaiement == 1}">
<h:outputText value="Compte" />
<h:inputText value="#{employeurBean.compte}">
</h:inputText>
</h:panelGroup>
</h:panelGrid>
Is there any idea to achieve that.
Many thanks for your help
This construct will fail if the bean is request scoped and/or you're doing business actions (such as preloading the list) in getter methods instead of action listener methods. Ensure that the bean is been placed in the view scope and that you're doing business actions in action listener methods.
If that doesn't solve the problem, then you really have to post the backing bean code along the view code in your question.

f:ajax within ui:repeat, unknown id for the component [duplicate]

This question already has answers here:
How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar"
(6 answers)
Closed 7 years ago.
When I try to render a panelGroup by an ajax call, it gives unknown id.
<h:form id="form1" prependId=false>
<h:panelGroup id="panel1">
<h:dataTable/>
<ui:repeat value="#{bean.page}" var="page">
<h:commandLink value="#{page}">
<f:ajax execute="#form" render="panel1" listener="#{bean.page}" />
</h:commandLink>
</ui:repeat>
</h:panelGroup>
</h:form>
When I tried this code, it gives unknown id panel1. I tried with id ":panel1" too. I get the same error.
A relative client ID (i.e. not starting with :) is relative to the current parent NamingContainer component. The <ui:repeat> is also a NamingContainer. So the render="panel1" is looking for the component within the context of <ui:repeat>. This isn't going to work. An absolute client ID (i.e. starting with :) is looking for the component within the context of view root. But you've it inside a <h:form> -which is in turn another NamingContainer component-, so the render=":panel" is also not going to work.
The following should work, with prependId="false" removed so that you can refer it:
<h:form id="form1">
<h:panelGroup id="panel1">
<h:dataTable/>
<ui:repeat value="#{bean.page}" var="page">
<h:commandLink value="#{page}">
<f:ajax execute="#form" render=":form1:panel1" listener="#{bean.page}" />
</h:commandLink>
</ui:repeat>
</h:panelGroup>
</h:form>
By the way, if you actually want to render only the table, then you should be doing this:
<h:form id="form1">
<h:panelGroup>
<h:dataTable id="table1" />
<ui:repeat value="#{bean.page}" var="page">
<h:commandLink value="#{page}">
<f:ajax execute="#form" render=":form1:table1" listener="#{bean.page}" />
</h:commandLink>
</ui:repeat>
</h:panelGroup>
</h:form>
Update: as per the comments, it turns out that you have changed the JSF default NamingContainer separator character from : to _ by configuration. In that case, you need to use _ instead of : in the client ID selector.
<f:ajax execute="#form" render="_form1_panel1" listener="#{bean.page}" />
<ui:repeat value="#{interaction.interactionListBean}" var="interactionItr">
<h:commandLink value="#{interactionItr.interactionDate}" styleClass="#{interaction.createImage}" style="margin:0 5px 5px 0!important; display:inline-block;"><br/>
<f:ajax render=":formId:interactionDate :formId:category :formId:activity :formId:status :formId:channel :formId:status1 :formId:caseId :formId:comment :formId:user1 :formId:team :formId:user :formId:transit "
listener="#{interactionController.interactionDetailGet}"/>
<f:param name="RefId" value="#{interaction.interactionRefId}"/>
</h:commandLink>
</ui:repeat>
problem :
please refer above with above code their is no exception on console but the ajax listerner will not invoke when I saw this post similiar kind of concept being use just I was place the exceute="#form" then my coding is working fine
solution:
execute="#form"

Resources