3

JBossAS7.1.1でJSF2.1.7とMyfacesCODI1.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;
    }
}

関連するCommandButtonがポストバックを開始しない最後の「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 に答える