2013-07-08 31 views
1

即時通訊發展的JSF primefaces Web應用程序,即時通訊面臨的一個問題,從dataTable不是從primefaces表

這裏所獲得的價值是我的XHTML代碼獲取選定值

<p:contextMenu for="table"> 
     <p:menuitem value="Allocate" actionListener="#{allocation.allocate}" update="msg"/> 
</p:contextMenu> 
<p:dataTable id="table" var="batch" value="#{allocation.batchInfoList}" rowKey="#{batch.id}" 
       selection="#{allocation.batchInfo}" selectionMode="single"> 
     <p:column headerText="Tan"> 
      <h:outputText value="#{batch.tan}" /> 
     </p:column> 
     <p:column headerText="Username"> 
      <h:outputText value="#{batch.users.curator}" /> 
     </p:column> 
     <p:column headerText="Status"> 
      <h:outputText value="#{batch.status}" /> 
     </p:column> 
</p:dataTable> 

,這裏是我的行動代碼

package com.cation.action; 

import java.util.ArrayList; 
import java.util.List; 

import javax.faces.context.FacesContext; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpSession; 

import com.cation.bean.BatchInfo; 
import com.cation.bean.Users; 
import com.cation.controller.CationController; 

public class Allocation { 

    private String batchName; 

    private String curator; 

    private String tanNumber; 

    private List<BatchInfo> batchInfoList; 

    private List<String> batchList = new ArrayList<String>(); 

    private List<Users> usersList = new ArrayList<Users>(); 

    private BatchInfo batchInfo = new BatchInfo(); 

    private CationController cationController = new CationController(); 

    public String getBatchName() { 
     return batchName; 
    } 

    public void setBatchName(String batchName) { 
     this.batchName = batchName; 
    } 

    public List<BatchInfo> getBatchInfoList() { 
     return batchInfoList; 
    } 

    public void setBatchInfoList(List<BatchInfo> batchInfoList) { 
     this.batchInfoList = batchInfoList; 
    } 

    public List<String> getBatchList() { 
     return batchList; 
    } 

    public void setBatchList(List<String> batchList) { 
     this.batchList = batchList; 
    } 

    public List<Users> getUsersList() { 
     return usersList; 
    } 

    public void setUsersList(List<Users> usersList) { 
     this.usersList = usersList; 
    } 

    public String getCurator() { 
     return curator; 
    } 

    public void setCurator(String curator) { 
     this.curator = curator; 
    } 

    public String getTanNumber() { 
     return tanNumber; 
    } 

    public void setTanNumber(String tanNumber) { 
     this.tanNumber = tanNumber; 
    } 

    public BatchInfo getBatchInfo() { 
     return batchInfo; 
    } 

    public void setBatchInfo(BatchInfo batchInfo) { 
     this.batchInfo = batchInfo; 
    } 

    @SuppressWarnings("unchecked") 
    public Allocation() { 
     try { 
      HttpServletRequest request = (HttpServletRequest) FacesContext 
        .getCurrentInstance().getExternalContext().getRequest(); 
      HttpSession session = request.getSession(); 
      usersList = (List<Users>) session.getAttribute("allUsers"); 
      batchList = cationController.getAllBatch(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public String handleBatch() { 
     try { 
      batchInfoList = new ArrayList<BatchInfo>(); 
      batchInfoList = cationController.getBatchByName(batchName); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return "allotInput"; 
    } 

    public void allocate() { 
     System.out.println(batchInfo); 
    } 
} 

我的問題是,當通過右鍵單擊事件從數據表中選擇行值並選擇分配,它調用m ethod,但該對象返回爲null。我不確定我是否已將代碼正確放入xhtml中的數據表標記中。

任何人都可以幫我解決這個問題。只是爲了獲得選定的行值或其對象。

回答

1

首個,所有嘗試

@ManagedBean 
@SessionScoped 
public class Allocation { 
... 
} 

來註釋豆你是如何使用的ActionListener,也許你需要用戶動作事件的方法

public void allocate(ActionEvent event) { 
    System.out.println(batchInfo); 
} 
0

嘗試使用阿賈克斯呼籲。這很簡單。爲rowSelect事件添加行單擊偵聽器方法。

<h:form id="tableForm"> 
    <p:dataTable id="userTable" var="item" value="#{myMB.batchList}" 
       selectionMode="single" rowKey="#{item}" paginator="true" rows="10" paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" 
       rowsPerPageTemplate="5,10,15" paginatorPosition="bottom"> 
     <p:ajax event="rowSelect" listener="#{myMB.rowClickListener}" 
        update="@form" /> 
       <p:column headerText="Column Field1" > 
        <h:outputText value="#{item.field1}" /> 
       </p:column> 
       <p:column headerText="Column Field2" > 
        <h:outputText value="#{item.field2}" /> 
       </p:column> 
    </p:dataTable> 
</h:form> 

和你的託管bean:

public void rowClickListener(SelectEvent event) { 
    if (event.getObject() != null) { 
     Batch pickedBatch= (Batch) event.getObject(); 
     // do what you need with your selected batch object 
    } 
} 

希望它可以很方便你。祝你好運!