Is it possible to render/update a c:forEach of a map via ajax? - jsf

I believe JSF 2.2 does not iterate over Maps, so c:forEach has to be used.
I have a button that adds an object in a Map via Ajax. Afterwards, it renders the list of existing objects in the Map. I logged it, and the values are inserted well. The problem is that they are not updated/refreshed correctly in the xhtml page - well, sometimes they are, sometimes they are not.
<h:commandButton action="#{myController.addValue()}" type="submit" value="Add">
<f:ajax execute="#this input1 input2" render="#form" />
</h:commandButton>
...
<h:panelGroup id="valuelist">
<!-- This does not refresh properly -->
<c:forEach items="#{myController.main.values}" var="entry">
#{entry.value.name} / #{entry.value.description}
</c:forEach>
<!-- This does refresh properly -->
<ui:repeat var="entry" value="#{myController.main.values.entrySet().toArray()}">
#{entry.value.name} / #{entry.value.description}
</ui:repeat>
</h:panelGroup>
I suppose it is something related to the JSF phases. I found a solution that uses ui:repeat, but it converts the Map to an array so I am not sure it is very efficient. How could I make the c:forEach refresh properly?

Related

Why JSF composite component doesn't work inside a ui:repeat? [duplicate]

I have following code:
<ui:repeat var="class2" value="#{bean.list}" varStatus="status">
<h:form id="#{class2.name}">
<h:outputText value="#{class2.name}" />
</h:form>
</ui:repeat>
However, when I open the page, it errors as follows:
component identifier must not be a zero-length String
But it is properly printed in the <h:outputText>. How is this caused and how can I solve it?
You can use EL in the id attribute of a JSF component, but the EL variable has to be available during view build time, while the JSF component tree is to be built. However, the <ui:repeat> runs during view render time, while the HTML output is to be generated based on JSF component tree. The <ui:repeat var> is not available during view build time and #{class2.name} evaluates to null which totally explains the error you got. That it works in <h:outputText> is because it runs during view render time.
If you replace <ui:repeat> by <c:forEach>, which runs during view build time, then it'll work as you intented. The <c:forEach> will namely generate physically multiple <h:form> components in the JSF component tree which each generate individually their own HTML output (in contrary to <ui:repeat>, wherein the very same <h:form> component is been reused multiple times to generate HTML output).
<c:forEach var="class2" items="#{bean.list}" varStatus="status">
<h:form id="#{class2.name}">
<h:outputText value="#{class2.name}" />
</h:form>
</c:forEach>
However, I really wonder why you need to do that. There's usually no need to dynamically assign component IDs. JSF will already ensure the uniqueness of the ID. The below example,
<ui:repeat var="class2" value="#{bean.list}" varStatus="status">
<h:form id="form">
<h:outputText value="#{class2.name}" />
</h:form>
</ui:repeat>
will end up in multiple forms with each an unique ID, suffixed with iteration index of the <ui:repeat>. If you actually need to use #{class2.name} for some JavaScript/jQuery purposes (you did nowhere state the concrete functional requirement in the question for which you thought that this would be the right solution, so it's merely guessing), then just wrap it in a plain vanilla HTML element:
<ui:repeat var="class2" value="#{bean.list}" varStatus="status">
<div id="#{class2.name}">
<h:form id="form">
<h:outputText value="#{class2.name}" />
</h:form>
</div>
</ui:repeat>
Or set it as style class of a JSF component, which is also just selectable via a CSS selector:
<ui:repeat var="class2" value="#{bean.list}" varStatus="status">
<h:form id="form" styleClass="#{class2.name}">
<h:outputText value="#{class2.name}" />
</h:form>
</ui:repeat>
See also:
JSTL in JSF2 Facelets... makes sense?

<c:choose><c:when> in JSF page does not work, seems to always evaluate false

In my JSF page I use <c:choose><c:when> tag to conditionally display content. However, it does not work as it seems to always evaluate false.
E.g.
<h:outputText value="#{pollInfo.active} -" />
<c:choose>
<c:when test="#{pollInfo.active}">
<h:outputText value="Active" />
</c:when>
<c:otherwise>
<h:outputText value="Deactive" />
</c:otherwise>
</c:choose>
There are for sure a few items with active=true, which is confirmed by <h:outputText>, but it just prints Deactive for all items. You can see the actual output in the following picture:
How is this caused and how can I solve it?
The symptoms and the screenshot suggests that the #{pollInfo} represents the currently iterated item of an iterating UI component such as <ui:repeat>, <h:dataTable>, <p:tabView>, <p:dataTable>, etc.
JSTL tags run during view build time, that moment when JSF component tree is built based on XHTML source code. The var attribute of such an iterating UI component is only available during view render time, that moment when HTML output is produced based on JSF component tree.
In other words, they don't run "in sync". The #{pollInfo} is always null during view build time.
In this particular case, you need the JSF component's rendered attribute instead.
<h:outputText value="Active" rendered="#{pollInfo.active}" />
<h:outputText value="Deactive" rendered="#{not pollInfo.active}" />
Or if you intend to conditionally render larger pieces of code, wrap all in a <ui:fragment>:
<ui:fragment rendered="#{pollInfo.active}">
<h:outputText value="Active" />
<!-- Some more components here if necessary. -->
</ui:fragment>
<ui:fragment rendered="#{not pollInfo.active}">
<h:outputText value="Deactive" />
<!-- Some more components here if necessary. -->
</ui:fragment>
Again another alternative, given that you've a pure if-else, is using the conditional operator in EL:
<h:outputText value="#{pollInfo.active ? 'Active' : 'Deactive'}" />
Additional bonus is, you end up with much less code.
See also:
JSTL in JSF2 Facelets... makes sense?

Myfaces, facesContext.messageList always empty unless it's parent has a rendered="#{not empty facesContext.messageList}"

I want to create a simple register form with JSF2 ajax and Bean Validation, which allows to display all error messages with custom format in the ui:repeat value="#{facesContext.messageList}" var="m" area when the validation fails. My code works well with Mojarra, but when I switch to Myfaces2.1, the messages never show with ui:repeat -- it only leaves an empty oltag there, and the facesContext.messageList seems to be empty. It still works with h:messages but that's not what I want.
However, today I found a wierd workaround: Adding the attribute rendered="#{not empty facesContext.messageList}" to one of the ui:repeat's parent component.
I can't understand why this rendered="..." matters because the errorDisplay component did get re-rendered everytime. Does this test #{not empty facesContext.messageList} change anything ?
<h:form>
Name:<h:inputText value="#{hello.name}" id="name"/>
<br/>
Age :<h:inputText value="#{hello.age}" id="age" converterMessage="'Age'必须是一个数字"/>
<h:commandLink value="register" type="submit">
<f:ajax execute="#form" render="errorDisplay"/>
</h:commandLink>
<h:panelGroup> <!--rendered="#{not empty facesContext.messageList}"-->
<h:panelGroup id="errorDisplay"><!--rendered="#{not empty facesContext.messageList}"-->
<span>发生以下错误:</span>
<ol style="color:red;">
<ui:repeat value="#{facesContext.messageList}" var="m">
<li>${m.detail}</li>
</ui:repeat>
</ol>
</h:panelGroup>
</h:panelGroup>
</h:form>

Is it possible to use <c:if> inside <a4j:repeat>

Is it possible to use <c:if> inside <a4j:repeat>?
I need to render a <br /> each 4 elements. I tried the following approach:
<a4j:repeat value="#{MBean.sucursal.events}" var="item" rowKeyVar="idx" >
<h:outputText value="#{item.eventDescription}" id="item1" />
<c: if test=#{idx % 4 == 0}>
<br />
</c:if>
</a4j:repeat>
However, it does not render the <br /> elements at all.
How can I achieve the requirement anyway?
Note: I'll ignore the careless formulation of the code which causes that it wouldn't compile/run properly at all. There's a space in <c :if> and the quotes are missing from the <c:if test> attribute. In future questions please pay a bit more attention to the properness of the code snippets you post, otherwise you'll only get invalid answers due to those red herrings.
Coming back to your concrete problem, this construct will indeed fail. The <c:if> runs during view build time (when the JSF component tree is about to be populated based on Facelets/JSP files), while the <a4j:repeat> runs during view render time (when the HTML code is about to be generated based on the JSF component tree). So, at the moment the <c:if> runs, #{idx} does not exist in the scope at all and always evaluates to null.
There are basically 2 solutions:
Use <c:forEach> instead of <a4j:repeat> to iterate over the items. It runs during view build time, so it'll run "in sync" with <c:if>.
<c:forEach items="#{MBean.sucursal.events}" var="item" varStatus="loop">
<h:outputText value="#{item.eventDescription}" id="item#{loop.index}" />
<c:if test="#{loop.index % 4 == 0}">
<br />
</c:if>
</c:forEach>
(note that I changed the <h:outputText id> as well, otherwise you end up with duplicate component ID errors or malformed HTML; feel free to omit the id attribute altogether, JSF will autogenerate proper one)
Use JSF component's rendered attribute instead of <c:if> to conditionally render HTML. It runs during view render time, so it'll run "in sync" with <a4j:repeat>.
<a4j:repeat value="#{MBean.sucursal.events}" var="item" rowKeyVar="idx">
<h:outputText value="#{item.eventDescription}" id="item1" />
<h:panelGroup rendered="#{idx % 4 == 0}">
<br />
</h:panelGroup>
</a4j:repeat>
See also:
JSTL in JSF2 Facelets... makes sense?

ui:repeat and p:poll

I have a ui:repeat. I want it to refresh for every 5 seconds. I tried p:poll but it doesnt work. Is there any way to make this working?
<ui:repeat value="#{videoImpl.fGetAllComments()}" var="v" id="commentlist">
<div class="comment-entry"><h:outputText value="#{v.comment}"/></div>
</ui:repeat>
<p:poll update="commentlist" interval="5" />
The <ui:repeat> doesn't generate any HTML by itself. So there is no element with id="commentlist" in the HTML output and hence the JS code behind <p:poll> can't find anything to update.
Wrap it in another JSF component which renders a fullworthy HTML element, such as <h:panelGroup>, and update it instead.
<h:panelGroup id="commentlist">
<ui:repeat value="#{videoImpl.fGetAllComments()}" var="v">
<div class="comment-entry">#{v.comment}</div>
</ui:repeat>
</h:panelGroup>
<p:poll update="commentlist" interval="5" />

Resources