Value not retained for action parameter when inside ui:repeat

761 views Asked by At

Below is my Code:

<ui:repeat var="status" value="#{showUpdatedAction.statusUpdates}">
    <h:panelGroup>
    #{status.content}
        <h:form> 
           <h:commandLink value="Like" action="#{statusAction.likeStatus(status.id,1)}" />
        </h:form>
  </h:panelGroup>
<ui:repeat>

#{status.content} shows correct values. When I print id of status using #{status.id}, it also gives correct value. But when I click the command link, value passed is always 0 for status.id.

Can someone tell me why this happens and how can I avoid this?

Thank you.

Edit 1

Interestingly, when instead of passing the parameter in function, I pass it using <f:param>, it works perfectly. Can anyone comment on that?

3

There are 3 answers

0
Mr.J4mes On

I think you should try using <c:forEach> instead of <ui:repeat>.

3
Daniel On

Your code in the JSF page is just fine, just checked it... (generated the beans at my side too : showUpdatedAction, statusAction , and a simple class Status)

public void likeStatus(String id,long someVal){
    System.out.println(id+"___"+someVal);
}

which prints the ids just fine

id1___1

id4___1

Maybe its something to do with the type of the id or something with your beans?

1
Sebastian Wramba On

I can't tell you exactly, why status.id is 0 in your case but you can directly pass the whole status object in your EL expression. Like so:

<h:commandAction value="Like" action="#{statusAction.likeStatus(status)}" />

Then in your likeStatus you simply do a int statusId = status.getId() or similar and you have what you want.

As an addition: Using <c:forEach> should actually be just a fallback, because people say you shouldn't mix JSTL with JSF for whatsoever reasons.