h:commandLink not working inside dataTable

Fahim Parkar picture Fahim Parkar · Mar 11, 2012 · Viewed 13.7k times · Source

In my JSF file I have below at the start.

<h:form><h:commandLink value="Create New Staff Account" action="adminCreateStaffMember"/></h:form>.

By use of this, when I create on Create New Staff Account I get re-directed to the page where I have form to create new account.

BUT, When I use the same inside dataTable, NO ACTION is taken. I am still on same page :(

<h:dataTable var="c" value="#{newStaffMemberServiceBean.newStaffMemberDataBeanList}"
  styleClass="order-table"
  headerClass="order-table-header"
  rowClasses="order-table-odd-row,order-table-even-row"
  border="1" id="staffListDataTable" width="100%">

  <h:column>
    <f:facet name="header">
       Staff Member Name
    </f:facet>
    <h:form><h:commandLink value="Create New Staff Account" action="adminCreateStaffMember"/></h:form>
  </h:column>
</h:dataTable>

Actually what I wanted to print is details of respective staff member where I would be using f:setPropertyActionListener. But as above is not working, I won't go ahead.

Please suggest me where I am going wrong.

Update 1

My newStaffMemberServiceBean is in RequestScoped

@ManagedBean(name = "newStaffMemberServiceBean")
@RequestScoped
public class NewStaffMemberServiceBean {
    // some code
}

Update 2

HTML Generated are as below

Outside of dataTable

<div align="right">
<form id="j_idt35" name="j_idt35" method="post" action="/adminManageStaffMember" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt35" value="j_idt35" />
<a href="#" onclick="mojarra.jsfcljs(document.getElementById('j_idt35'),{'j_idt35:j_idt36':'j_idt35:j_idt36'},'');return false">Create New Staff Account</a><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="2664682277023387375:-3250423983171933030" autocomplete="off" />
</form>

Inside dataTable

<form id="staffListDataTable:0:j_idt43" name="staffListDataTable:0:j_idt43" method="post" action="/adminManageStaffMember" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="staffListDataTable:0:j_idt43" value="staffListDataTable:0:j_idt43" />
<a href="#" onclick="mojarra.jsfcljs(document.getElementById('staffListDataTable:0:j_idt43'),{'staffListDataTable:0:j_idt43:j_idt45':'staffListDataTable:0:j_idt43:j_idt45'},'');return false">Create New Staff Account</a><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="2664682277023387375:-3250423983171933030" autocomplete="off" />
</form>

Answer

prajeesh kumar picture prajeesh kumar · Mar 11, 2012

I would suggest a few modifications for your original code:

  1. don't use h:form for each of your commandLink, instead use one form for your dataTable.

  2. the action will get executed only when it finds the component that triggered the aciton. If you use a request scoped list for your datatable then that list won’t be there when the view is restored or may not match the original one which was used to create the view. You may have to change the managed bean from request scope to view scope or higher.

  3. as a suggestion, you can use h:outputLink or h:link instead of the commandLink and provide the f:param to simplify this, in which case you don't need the form also.