2012-10-18 18 views
2

我在JBoss AS 7.1.1上使用JSF 2.1.7和Myfaces CODI 1.0.5。我的<h:commandButton>不起作用。我已經閱讀了這些要求,並通過很多博客中的例子都無濟於事。我的小面的代碼如下<h:commandButton>不會發起回發

<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」回發。

這是如何造成的,我該如何解決?

+0

因此文件上傳工作正常嗎?如果您沒有上傳文件,該按鈕是否也可以工作?至於你的陳述「甚至不發起回發」,你的意思是根本沒有發送HTTP請求?您是否檢查過webbrowser的開發者工具集中的HTTP流量? – BalusC

+0

@BalusC,當文件沒有上傳時,該按鈕不起作用。至於第二個問題,沒有任何反應。根本沒有請求被啓動。我嘗試了各種瀏覽器,都無濟於事。感謝您的即時回覆,儘管 –

+0

因此,該按鈕只有在文件上傳時才起作用?所以文件上傳本身工作正常? JS控制檯中是否有錯誤? – BalusC

回答

4

您需要從<h:commandButton>中刪除type="button"。它應該是type="submit",這已經是默認了。

type="button"使得<input type="submit">這是僅適用於客戶端的處理程序,您通常使用附加和onclick等有用它的<input type="button">來代替。

+0

謝謝,我真的很感謝幫助 –

+0

不客氣。 – BalusC