Jsf 未从primefaces表中获取选定值

Jsf 未从primefaces表中获取选定值,jsf,primefaces,datatable,Jsf,Primefaces,Datatable,我正在用jsf primefaces开发web应用程序,在从dataTable 这是我的xhtml代码 <p:contextMenu for="table"> <p:menuitem value="Allocate" actionListener="#{allocation.allocate}" update="msg"/> </p:contextMenu> <p:dataTable id="table" var="batch" val

我正在用jsf primefaces开发web应用程序,在从
dataTable

这是我的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);
    }
}
package com.阳离子.action;
导入java.util.ArrayList;
导入java.util.List;
导入javax.faces.context.FacesContext;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpSession;
导入com.阳离子.bean.BatchInfo;
导入com.阳离子.bean.Users;
导入com.阳离子控制器.CationController;
公共班级分配{
私有字符串批处理名称;
私人弦乐馆长;
私有字符串编号;
私人名单;
private List batchList=new ArrayList();
private List usersList=new ArrayList();
私有BatchInfo BatchInfo=新的BatchInfo();
专用CationController CationController=新CationController();
公共字符串getBatchName(){
返回批处理名称;
}
public void setBatchName(字符串batchName){
this.batchName=batchName;
}
公共列表getBatchInfoList(){
返回批处理信息列表;
}
public void setBatchInfoList(List batchInfoList){
this.batchInfoList=batchInfoList;
}
公共列表getBatchList(){
返回批处理列表;
}
公共作废清单(清单批次清单){
this.batchList=批处理列表;
}
公共列表getUsersList(){
返回用户列表;
}
公共void setUsersList(列表usersList){
this.usersList=usersList;
}
公共字符串getCurator(){
返回馆长;
}
公共图书馆馆长(字符串馆长){
这个。策展人=策展人;
}
公共字符串gettanumber(){
返回单号;
}
公共无效设置编号(字符串编号){
this.tanNumber=tanNumber;
}
public BatchInfo getBatchInfo(){
返回批处理信息;
}
公共无效setBatchInfo(BatchInfo BatchInfo){
this.batchInfo=batchInfo;
}
@抑制警告(“未选中”)
公共拨款(){
试一试{
HttpServletRequest=(HttpServletRequest)FacesContext
.getCurrentInstance().getExternalContext().getRequest();
HttpSession session=request.getSession();
usersList=(列表)session.getAttribute(“allUsers”);
batchList=cationController.getAllBatch();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
公共字符串手柄组(){
试一试{
batchInfoList=newArrayList();
batchInfoList=cationController.getBatchByName(batchName);
}捕获(例外e){
e、 printStackTrace();
}
返回“输入”;
}
公共空间分配(){
System.out.println(batchInfo);
}
}
我的问题是,当通过右键单击事件从datatable中选择行值并选择allocate时,它会调用该方法,但对象返回为null。我不确定是否正确地将代码放在xhtml中的datatable标记中


谁能帮我解决这个问题。只需获取所选的行值或对象。

首先尝试用

@ManagedBean
@SessionScoped
public class Allocation {
...
}
以及如何使用actionListener,可能需要在方法上使用ActionEvent

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

首先,试着用

@ManagedBean
@SessionScoped
public class Allocation {
...
}
以及如何使用actionListener,可能需要在方法上使用ActionEvent

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

尝试使用ajax调用。这很简单。为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>

希望它能方便你。祝你好运

尝试使用ajax调用。这很简单。为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>
希望它能方便你。祝你好运