Search code examples
javajsf-2cdimyfaces

@ViewAccessScoped not getting selected row for first time


I am using JSF 2.0 and I have a datatable which gets populated using lazy load.Scope of the page is @ViewAccessScoped (MyFaces CODI) as I am using CDI/Spring.

When datatable is loaded in jsf page and when I select a row for the first time (radiobutton), I am not able to get selected row, for subsequent selections I am able to get selected row in onRowSelect method of ManagedBean. If I use session scope then I am able to get the selected row when I select row for the first time.

JSF Code

<p:dataTable id="dataTable" var="req" lazy="true" value="#{emp.lazyModel}"
      rowKey="#{req.empNo}"
         paginator="true" rows="10"
         selection="#{emp.selectedRequest}">                  
      <p:ajax event="rowSelectRadio" listener="#{emp.onRowSelect}" /> 
       <p:column selectionMode="single" style="width:18px" />  

ManagedBean

@Named("emp")
@ViewAccessScoped
public class EmployeeManagedBean implements Serializable {

private Employee selectedRequest = new Employee();

@PostConstruct
   public void init() {
      initTable();
   }

   private void initTable() {
      lazyModel = new LazyEmployeeDataModel(requestList, requestService);
   }

   public LazyDataModel<Employee> getLazyModel() {
      return lazyModel;

   }

and onRowSelect Method

public void onRowSelect(SelectEvent event) {
      try {
         setSelectedRequest((Employee) event.getObject());
         System.out.println("row "
               + getSelectedRequest()); 

Solution

  • I have resolved this issue, the problem was cookies.

    When list.jsf gets loaded for the first time, cookies was not set. So when user selects a row cookies gets set and thus for subsequent selections user could get selected row value.

    To resolve this issue, I have created a index.jsp page and redirect to list.jsf, thus cookies get set before user selects a row using radio button.

    Thanks