Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass parameter to h:commandButton

 
Piya Patil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

In our application, we are having parent-child pages. Here parent page contains details of parent object and list of child objects(in tabular form. Used <h:dataTable> to display list of child objects). If user mutates parent object details and selects edit option for one of the child object. When user come back to parent page after saving child, details of parent object does not remain. <s:link> is used for navigation and passed id of child as a parameter.

But when we try to use <h:commandButton> instead of <s:link>, not able to get id of child at backing bean. As h:commandButton does not support f:param.

Please, suggest solution for this.

Thanks...!
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JR.
Not sure but have you tried using a f:setPropertyActionListener?

Please note that JavaRanch has a formal naming policy and you might want to change your display name.
 
sreejith sreedharan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check <f:attribute name="id" value="#{bean.id}" />
 
Piya Patil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sreejith,

Thanks for your solution...! But its not working as per requirement.

if i used <f:attribute> with <h:commandButton> it always gives me last element from list, even if i select any other element.
 
sreejith sreedharan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is a problem with the way you pass value to f:attribute

give the same value of Id as you give it for displaying list..

i hope that helps...
 
Piya Patil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"give the same value of Id as you give it for displaying list.. " this is not clear to me.

This is my code:

<h:dataTable id="dt1"
value="#{subCriteriumList}" var="item">

<h:column>
.
</h:column>

<h:column>
<h:commandButton
action="#{detailManagedBean.editSubCriteria()}"
value="#{messages['common.button.edit']}"
title="#{messages['common.button.edit']}">
<f:attribute name="subCriteriaId" value="#{item.id}" />
</h:commandButton>
</h:column>

</h:dataTable>

If there are more than one elements in the list and for example user selects edit button for second element so he will get navigate to another screen and need to set id of selected element as subCriteriaId on backing bean.
 
Tim Holloway
Bartender
Posts: 18423
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you put a commandButton or commandLink in a DataTable and click on it, the associated action processor can tell which row was selected by using the DataTable's selectedRow property. Usually you'll have some distinguishing characteristic such as a key column in the DataModel row.

Of course to make that work the backing bean containing the data model cannot be in request scope, since at that level the datamodel is destroyed once the page is sent to the user and a whole new one is constructed before the action processor is invoked (lacking the row context!).
 
Piya Patil
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Due to some business limitation of project can not use DataModel feature.
 
Tim Holloway
Bartender
Posts: 18423
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piya Patil wrote:Due to some business limitation of project can not use DataModel feature.


Due to a high level of cynicism, I tend to interpret "business limitation" as meaning that some idiot who's in charge of the project doesn't understand the fundamentals of the technology being used.

My standard recommendation on that is to get out the CV and start bringing it up to date, because when clueless politics meddle with technology, the product is generally doomed, and if by sheer brute force, it manages to get into production anyway, it's going to be excruciatingly painful to maintain.

Actually, let me amend that. Excruciatingly painful and expensive to maintain. Pain, alas, is meaningless to management.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!