Rajat Gupta
Rajat Gupta

Reputation: 26597

How to pass item id corresponding to submitted form to action/actionlistener methods?

I have several forms on a webpage to submit responses corresponding to each listed item on page. I tried passing the item id corresponding to the submitted form through the actionListener method itself but that actually leads to passing the 'then current' value of the parameter not the 'current' value of currentItem.id.

<h:form>
    ...
    <p:commandButton actionListener="#{itemController.addUserComment(currentItem.id)}" value="Add" />    
</h:form>

<h:form>
    ...  
</h:form>

How do I pass the current value of currentItem.id to managed bean ?

Upvotes: 0

Views: 244

Answers (1)

BalusC
BalusC

Reputation: 1108722

If I understand you correctly, you want to pass the ID as it was during request to display the form, not during the request to process the form submit?

In that case, use <f:param>.

<p:commandButton actionListener="#{itemController.addUserComment}" value="Add">
    <f:param name="id" value="#{currentItem.id}" />
</p:commandButton>

You can grab it by @ManagedProperty or <f:viewParam>.

See also:


Unrelated to the concrete problem, I however wonder why the #{currentItem.id} changes like that. This suggests that you're loading the source of #{currentItem} inside a getter method instead of inside a (post)constructor or action(listener) method.

Upvotes: 1

Related Questions