0

我正在使用 primefaces 数据表来显示数据列表。我希望我的用户能够选择多行,之后将对所选行表示的数据执行操作。一切正常,直到我发现无法解决选择的操作执行为止。

下面是我的 xhtml 页面

<h:form enctype="multipart/form-data" id="upload">
<primefaces-p:panel closable="false" collapsed="false" header="Excel Contact Uploader" id="pnlupload" rendered="true" toggleable="false" visible="true" widgetVar="pnlupload">

<primefaces-p:growl id="msg"  showDetail="true" life="3000" showSummary="true">

</primefaces-p:growl>
        <primefaces-p:fileUpload auto="true" 
        allowTypes="/(\.|\/)(xls)$/" 
        sizeLimit="1024000" 
        mode="advanced" 
        multiple="true" invalidFileMessage="Invalid file type" invalidSizeMessage="File too large" dragDropSupport="true" fileUploadListener="#{excelFileController.handleFileUpload}" showButtons="true" update="msg, tblcontacts
        " required="false"/>


      <br />
<br />


        <primefaces-p:scrollPanel rendered="true" style="height:200px;">
            <primefaces-p:dataTable  emptyMessage="No Contacts Uploaded" id="tblcontacts" rendered="true"  rows="8" 
            value="#{excelFileController.contacts}" var="contact" style="width:50pc;">
        <primefaces-p:column style="width:18px" headerText="Select"> 
       <h:selectBooleanCheckbox value="#{contact.selected}" />

        </primefaces-p:column>

        <primefaces-p:column headerText="File Name">  
        #{contact.groupName}  
        </primefaces-p:column>  
        <primefaces-p:column headerText="Number of Contacts">  
        #{contact.numberofentries}  
    </primefaces-p:column> 
    <primefaces-p:column>
    <h:button  outcome="blkedit?faces-redirect=true" rendered="true" value="Edit">
         <f:param name="contact" value="#{contact.contactId}"/>
        </h:button>
    </primefaces-p:column>
    </primefaces-p:dataTable>
   </primefaces-p:scrollPanel>

<br />
</primefaces-p:panel>
<h:commandButton value="Delete" id="btndelete" action="#{excelFileController.removeContact}"  immediate="true" disabled="false" rendered="true"/>
<h:message for="btndelete" />
</h:form>

下面是我的支持豆

@Named
@ViewAccessScoped
public class ExcelFileController implements Serializable, IFileController {

/**
 * 
 */
private static final long serialVersionUID = -8117258104485487921L;

@Inject
PhoneNumberFormatter formatter;

@Inject
@Authenticated
UserProfile profile;

public PhoneNumberFormatter getFormatter() {
    return formatter;
}

public void setFormatter(PhoneNumberFormatter formatter) {
    this.formatter = formatter;
}

@EJB
BulkContactDeleter deleter;

@Inject
Logger logger;

@Inject
@CurrentContext
FacesContext context;

@Inject
BulkSMSContactListProducer listProducer;

@Inject
ConfigurationListProducer producer;

private BulkSMSContacts[] contactsSelected;

private List<BulkSMSContacts> contacts;

public BulkSMSContacts[] getContactsSelected() {
    return contactsSelected;
}

public void setContactsSelected(BulkSMSContacts[] contactsSelected) {
    this.contactsSelected = contactsSelected;
}

/*
 * public BulkContactDataModel getContactDataModel() { return
 * contactDataModel; }
 */

@PostConstruct
public void init() {
    logger.log(Level.INFO, "Entering excel file controller");
    /*
     * contactDataModel = new BulkContactDataModel(
     * listProducer.getBulkSMSContacts());
     */
    this.contacts = listProducer.getBulkSMSContacts();

}

@Override
public String removeContact() {
    int contactsdeleted = 0;

    if (this.contacts != null) {
        /*
         * for (BulkSMSContacts contacts : contactsSelected) { if (contacts
         * != null) { deleter.deleteContact(contacts); contactsdeleted += 1;
         * }
         */
        for (BulkSMSContacts c : contacts) {
            if (c.isSelected()) {

                deleter.deleteContact(c);
                contactsdeleted += 1;
            }

        }
        contacts = listProducer.getBulkSMSContacts();

        logger.log(Level.INFO, "Deleted " + contactsdeleted + " Contacts");
        context.addMessage(null, new FacesMessage(
                FacesMessage.SEVERITY_INFO, "Success", contactsdeleted
                        + " entries where deleted successfully"));
    } else {
        context.addMessage(null, new FacesMessage(
                FacesMessage.SEVERITY_ERROR, "Error",
                "No contact file was selected!"));
    }
    return null;
}

public List<BulkSMSContacts> getContacts() {
    return contacts;
}

public void setContacts(List<BulkSMSContacts> contacts) {
    this.contacts = contacts;
}

}

下面是我的模型定义

@Entity
public class BulkSMSContacts implements Serializable {

private static final long serialVersionUID = -6444763251149211384L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@XmlAttribute
private long contactId;

@XmlElement
@NotNull(message = "message.validator.notnull")
@Size(min = 9, max = Integer.MAX_VALUE, message = "message.validator.size")
@Column(nullable = false, length = Integer.MAX_VALUE, unique = false)
@Basic(fetch = FetchType.EAGER)
private String contacts;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Id", nullable = false)
@XmlElement(type = SMSAccount.class)
private SMSAccount account;

@NotNull(message = "message.validator.notnull")
@Size(min = 2, max = 100, message = "message.validator.size")
@Column(length = 100, nullable = false, unique = false)
@XmlElement
private String groupName;

@Column(nullable = true)
private int numberofentries = 0;

@javax.persistence.Transient
private boolean selected;

//getters and setters

问题是,当单击删除按钮时(尽管调用了 removeContact() 方法。所有行的评估结果都没有被选中。

我也尝试过使用 primefaces 展示方法,但仍然是示例问题。

请问我是不是做错了什么??

4

2 回答 2

2

我不明白为什么要在实体类中设置选择属性。尝试这样做

  1. 设置数据模型:

    public class BulkSMSContactsDataModel extends ListDataModel<BulkSMSContacts> implements SelectableDataModel<BulkSMSContacts> {    
    
    public BulkSMSContactsDataModel() {  
    }  
    
    public BulkSMSContactsDataModel(List<BulkSMSContacts> data) {  
        super(data);  
    }  
    
    @Override  
    public BulkSMSContacts getRowData(String rowKey) {         
       List<BulkSMSContacts> contacts = (List<BulkSMSContacts>) new yourDao.getListOfBulkSMSContacts(); //yourDAO :BulkSMSContactsSelector i guess
        for(BulkSMSContacts c : contacts) {  
            if(String.valueOf(c.getContactId()).equals(rowKey))  
                return c;  
        }  
    
        return null;  
    }  
    
    @Override  
    public Object getRowKey(BulkSMSContacts obj) {  
        return String.valueOf(obj.getContactId());  
    }
    

    }

  2. 配置你的 bean:

    public class ExcelFileController implements Serializable, IFileController {
        private BulkSMSContacts[] contactsSelected;
        private List<BulkSMSContacts> contacts;
        private BulkSMSContacts[] selectedcontacts;
        private BulkSMSContactsDataModel contactsList;
            public ExcelFileController() {
            contactsList = new BulkSMSContactsDataModel(contacts);
        }
        // getters and setters
        // the rest of your code 
    

    }

  3. 配置你的 xhtml

    <p:dataTable  emptyMessage="No Contacts Uploaded" id="tblcontacts" rendered="true"  rows="8" value="#{excelFileController.contactsList}" var="contact" style="width:50pc;" selection="#{excelFileController.selectedcontacts}">
          <p:column selectionMode="multiple" style="width:20px" />
          <p:column headerText="File Name">  
            #{contact.groupName}  
          </p:column>  
          <p:column headerText="Number of Contacts">  
            #{contact.numberofentries}  
          </p:column>
           <!-- the rest -->
    

  4. 更新你的用户界面。

  5. 使用p而不是primefaces-p它将更容易阅读您的代码。:)

于 2012-10-24T12:24:10.653 回答
1

为了知道选中contacts的 ,dataTable 和 UI Command 组件即<h:commandButton><p:commandButton>等必须是相同的形式。这意味着,您的联系人数据表和您的删除按钮必须在同一个<h:form>

    <h:form>
        <!-- based in your code (pretty odd by the way)... -->
        <primefaces-p:dataTable  emptyMessage="No Contacts Uploaded" id="tblcontacts"
            rendered="true" rows="8" value="#{excelFileController.contacts}"
            var="contact" style="width:50pc;">
            <!-- your columns... -->
        </primefaces-p:dataTable>
        <primefaces-p:commandButton value="Delete contacts"
            action="#{excelFileController.removeContact}" />
    </h:form>

此外,请检查您的页面中是否没有嵌套表单。

更多信息:


问题似乎出在 CDI 注释中。为了让你ExcelFileController活着,是在某处初始化对话(在你的代码中任何地方都没有显示)。另一种解决方案是使用 JSF 注释:

//@Named
//@ViewAccessScoped
@ManagedBean
@ViewScoped
public class ExcelFileController implements Serializable, IFileController {
    //rest of your code...
}
于 2012-10-23T03:07:22.263 回答