3

我在 JBoss AS 7.1.1 上使用 JSF 2.1.7 和 Myfaces CODI 1.0.5。我<h:commandButton>的不工作。我已经阅读了要求并通过许多博客中的示例都无济于事。我的facelets代码如下

<ui:define name="pagecontent">  
    <h1 class="title ui-widget-header ui-corner-all">Upload Bulk Contact File</h1>
    <div class="entry">
        <h:form enctype="multipart/form-data" id="upload">
            <p:panel closable="false" collapsed="false" header="Excel Contact Uploader"
                id="pnlupload" rendered="true" toggleable="false" visible="true" widgetVar="pnlupload">
                <p:growl id="msg"  showDetail="true" life="3000" showSummary="true"/>
                <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"/>
                <p:scrollPanel rendered="true" style="height:200px;">
                    <p:dataTable draggableColumns="false" editable="false" emptyMessage="No 
                        Contacts Uploaded" id="tblcontacts" rendered="true"  rows="8"
                        selection="#{excelFileController.contactsSelected}" 
                        value="#{excelFileController.contactDataModel}" var="contact" style="width:50pc;">
                        <p:column selectionMode="multiple" style="width:18px" />
                        <p:column headerText="File Name">
                            #{contact.groupName}  
                        </p:column>  
                        <p:column headerText="Number of Contacts">  
                            #{contact.numberofentries}  
                        </p:column> 
                        <p:column>
                            <h:button  outcome="blkedit?faces-redirect=true" rendered="true" value="Edit">
                                <f:param name="contact" value="#{contact.contactId}"/>
                            </h:button>
                        </p:column>
                    </p:dataTable>
                </p:scrollPanel>
                <br />
            </p:panel>
            <h:commandButton value="Delete" id="btndelete" 
                action="#{excelFileController.removeContact}"  type="button" immediate="true" 
                disabled="false"     rendered="true"/>
            <h:message for="btndelete" />
        </h:form>
    </div>
</ui:define>

的代码ExcelFileController如下:

@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 BulkContactDataModel contactDataModel;

    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());

    }
    /*
     * (non-Javadoc)
     * 
     * @see
     * org.jboss.tools.examples.controller.IFileController#handleFileUpload(
     * org.primefaces.event.FileUploadEvent)
     */
    @Override
    public void handleFileUpload(FileUploadEvent event) {
        StringBuffer buffer = new StringBuffer();
        // create a new file input stream with the input file specified
        // at the command line

        InputStream fin = null;
        try {
            fin = event.getFile().getInputstream();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // create a new org.apache.poi.poifs.filesystem.Filesystem
        POIFSFileSystem poifs = null;
        try {
            poifs = new POIFSFileSystem(fin);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        HSSFWorkbook wb = null;
        try {
            wb = new HSSFWorkbook(poifs);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int numberofsheets = wb.getNumberOfSheets();
        for (int i = 0; i < numberofsheets; i++) {
            HSSFSheet sheet = wb.getSheetAt(i);
            for (Row row : sheet) {
                for (Cell cell : row) {
                    switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING :
                            if (!cell.getStringCellValue().isEmpty())
                                buffer.append(formatter.formatPhoneNumber(cell
                                        .getStringCellValue()));
                            buffer.append(producer.getConfiguration(
                                    SettingsName.SMS_PHONENUMBERDELIMITER
                                            .toString()).getValue());

                            break;
                        case Cell.CELL_TYPE_NUMERIC :
                            if (cell.getNumericCellValue() != 0) {

                                buffer.append(formatter
                                        .formatPhoneNumber(String.valueOf(cell
                                                .getNumericCellValue())));
                                buffer.append(producer.getConfiguration(
                                        SettingsName.SMS_PHONENUMBERDELIMITER
                                                .toString()).getValue());
                                break;
                            }

                        default :
                            break;
                    }

                }

            }
        }
        BulkSMSContacts contacts = new BulkSMSContacts();
        contacts.setAccount(profile.getSmsAccount());
        int number = formatter.splitPhoneNumbers(buffer.toString()).length;
        contacts.setContacts(buffer.toString());
        String filenameString = event.getFile().getFileName();
        int index = filenameString.indexOf(".");
        filenameString = filenameString.substring(0, index);
        contacts.setGroupName(filenameString);
        contacts.setNumberofentries(number);
        try {
            deleter.addContact(contacts);
            List<BulkSMSContacts> temp = listProducer.getBulkSMSContacts();
            contactDataModel.setWrappedData(temp);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
                "Success", number
                        + " entries processed. Please refresh page to view"));

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.jboss.tools.examples.controller.IFileController#removeContact()
     */
    @Override
    public String removeContact() {
        int contactsdeleted = 0;
        if (contactsSelected != null) {
            for (BulkSMSContacts contacts : contactsSelected) {
                if (contacts != null) {
                    deleter.deleteContact(contacts);
                    contactsdeleted += 1;
                }

            }

            List<BulkSMSContacts> temp = listProducer.getBulkSMSContacts();
            contactDataModel.setWrappedData(temp);

            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;
    }
}

所有方法都可以正常工作,除了最后一个“removeContact”,其中相关的命令按钮甚至不启动回发。

这是如何引起的,我该如何解决?

4

1 回答 1

5

您需要type="button"<h:commandButton>. 应该是type="submit",这已经是默认设置了。

type="button"使它成为一个<input type="button">而不是<input type="submit">仅对您通常使用的客户端处理程序有用onclick等等。

于 2012-10-18T16:09:16.400 回答