File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes How to pass parameter to h:commandButton Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSF
Reply Bookmark "How to pass parameter to h:commandButton" Watch "How to pass parameter to h:commandButton" New topic
Author

How to pass parameter to h:commandButton

Piya Patil
Greenhorn

Joined: Aug 01, 2007
Posts: 4
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

Joined: Mar 20, 2007
Posts: 317
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

Joined: Sep 06, 2010
Posts: 5
check <f:attribute name="id" value="#{bean.id}" />
Piya Patil
Greenhorn

Joined: Aug 01, 2007
Posts: 4
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

Joined: Sep 06, 2010
Posts: 5
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

Joined: Aug 01, 2007
Posts: 4

"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
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14491
    
    7

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!).


Customer surveys are for companies who didn't pay proper attention to begin with.
Piya Patil
Greenhorn

Joined: Aug 01, 2007
Posts: 4
Due to some business limitation of project can not use DataModel feature.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 14491
    
    7

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How to pass parameter to h:commandButton
 
Similar Threads
about collections
2nd Level cache eviction not cascading
hibernate HibernateTemplate.get() returns incorrect subclass instance
what changes should i made in save method in one-to-many relationships
How h:message and h:messages work