Jsf 通过复选框从p:datatable中选择多个值并将其发送到bean

Jsf 通过复选框从p:datatable中选择多个值并将其发送到bean,jsf,primefaces,datatable,Jsf,Primefaces,Datatable,我遵循此链接选择多个复选框并检索bean中的值: 我的jsf代码如下: <p:dataTable value="#{deviceController.devicesModel}" var="item" widgetVar="deviceTable" selection="#{deviceController.selectedDevices}" rowKey='#{item}'> <p:column

我遵循此链接选择多个复选框并检索bean中的值:

我的jsf代码如下:

<p:dataTable value="#{deviceController.devicesModel}"
            var="item" widgetVar="deviceTable"
            selection="#{deviceController.selectedDevices}"
            rowKey='#{item}'>
    <p:column  sortBy="#{item.manufacturerSerialNum}" filterBy="#{item.manufacturerSerialNum}">
        <f:facet name="header">
            <h:outputText value="Manufacturer Serial No"/>
        </f:facet>
        <h:outputText value="#{item.manufacturerSerialNum}" />  
    </p:column>             
    <p:column selectionMode="multiple">
        <f:facet name="header">
            <h:outputText value="#{bundle.ListLaptopTitle_inService}"/>
        </f:facet>
        <h:selectBooleanCheckbox value="#{item.isInService}"/>
    </p:column>
</p:dataTable> 

<p:commandButton action="#{deviceController.getSelectedItems}" 
        value="Submit Request" style="margin-top: 20px"/>

我的bean代码是:

public String getSelectedItems(){
        selectedDevicesList = new ArrayList<Device>();
        List<Device> dev=createDeviceList();
        for (Device item :dev ) {
            if (item.getIsInService()) {
                selectedDevicesList.add(item);
                item.setIsInService(false); // Reset.
            }
    }
        return "selected";
    }
公共字符串getSelectedItems(){
SelectedDeviceList=new ArrayList();
List dev=createDeviceList();
用于(设备项:dev){
if(item.getIsInService()){
选择设备列表。添加(项目);
item.setIsInService(false);//重置。
}
}
返回“已选择”;
}

我不理解的是我们从表中传递所选行的值的位置。最初,如果我有4行,并且现在当我选中两行时所有复选框都未选中,那么它应该转到bean,并且这两个值应该在数据库中更新。就像我应该在我的bean中得到这两个bean的
inservice
字段的值一样。你能告诉我我缺少了什么吗。谢谢。

你基本上将POSTrequest内容与get内容混合在一起。您的
操作
方法只需要获取bean上已经存储的内容并将其发送到控制器层

从中可以看出,另一个错误是,当您手动注入一个
来执行该操作时,该列本身就是一个错误

将最后一列替换为
将完成将所选内容存储在
{deviceController.selectedDevices}
中的工作

只需勾选此简单选项,即可为您提供所需的功能:

托管Bean

@ManagedBean
@ViewScoped
public final class TestManagedBean implements Serializable {

    public class Test {
        private int id;
        private String description;
        private boolean selected;

        public Test(int id, String desc) {
            this.id = id;
            this.description = desc;
        }

        public String getDescription() {
            return description;
        }

        public int getId() {
            return id;
        }

        public boolean isSelected() {
            return selected;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public void setId(int id) {
            this.id = id;
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
        }

        @Override
        public String toString() {
            return "Test [description=" + description + ", selected="
                    + selected + "]";
        }
    }
    private List<Test> list;

    private List<Test> selectedValues;

    public TestManagedBean() {
    }

    public void actionSave() {
        //Perform DB stuff with selectedValues
        System.out.println("Table saved");
    }

    public List<Test> getList() {
        return list;
    }

    public List<Test> getSelectedValues() {
        return selectedValues;
    }

    @PostConstruct
    public void init() {
        list = Arrays.asList(new Test(1, "Tanzania"), new Test(2, "India"));
    }
}
@ManagedBean
@视域
公共最终类TestManagedBean实现可序列化{
公开课考试{
私有int-id;
私有字符串描述;
选择私有布尔值;
公共测试(int-id,String-desc){
this.id=id;
this.description=desc;
}
公共字符串getDescription(){
返回说明;
}
公共int getId(){
返回id;
}
公选{
返回选中的;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共无效集合id(内部id){
this.id=id;
}
已选择公共无效设置(已选择布尔值){
this.selected=selected;
}
@凌驾
公共字符串toString(){
返回“Test[description=“+description+”,selected=”
+选中+“]”;
}
}
私人名单;
私有列表选择值;
公共TestManagedBean(){
}
public void actionSave(){
//使用selectedValues执行数据库填充
System.out.println(“保存的表格”);
}
公共列表getList(){
退货清单;
}
公共列表getSelectedValues(){
返回所选的值;
}
@施工后
公共void init(){
列表=数组。asList(新测试(1,“坦桑尼亚”)、新测试(2,“印度”);
}
}
xhtml页面

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Test page</title>
</h:head>
<h:body>
    <h:form>
        <p:dataTable value="#{testManagedBean.list}"
            selection="#{testManagedBean.selectedValues}" var="value"
            rowKey="#{value.id}">
            <p:column>
                #{value.description}
            </p:column>
            <p:column selectionMode="multiple" style="width:2%" />
        </p:dataTable>
        <h:commandButton value="send" action="#{testManagedBean.actionSave}" />
    </h:form>
</h:body>
</html>

测试页
#{value.description}

看起来您走的是正确的道路<代码>项目。单击复选框时,将切换isInService。单击命令按钮时,您正在生成一个可用于更新数据库的
selectedDevicesList
。您应该使用
selectedDeviceList
来创建和执行数据库更新语句。我还没有完全阅读您的问题,但是您链接的文章是针对JSF 1.x datatables的。在PrimeFaces中,您不需要自己将
放在
中。您可以看看这篇文章,让我知道如何实现它。感谢您的帮助。当您自己选择复选框时,值不会更改,而是在发送表单时更改。这是JSF生命周期的一部分,特别是模型更新部分。然后,使用属性中的值执行
action
方法。