< h:commandButton>不发起回发 [英] <h:commandButton> does not initiate a postback

查看:84
本文介绍了< h:commandButton>不发起回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JBoss AS 7.1.1上使用JSF 2.1.7和Myfaces CODI 1.0.5.我的<h:commandButton>无法正常工作.我已经阅读了这些要求,并通过许多博客中的示例都徒劳无功.我的facelets代码如下

I am using JSF 2.1.7 and Myfaces CODI 1.0.5 on JBoss AS 7.1.1. My <h:commandButton> is not working. I have read the requirements and have through examples in many blogs all to no avail. My facelets code is as follows

<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"(其中有关的CommandButton甚至不发起回发)之外,所有方法都可以正常工作.

All the methods work fine except for the last one "removeContact" where the CommandButton concerned does not even initiate a postback.

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

How is this caused and how can I solve it?

推荐答案

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

You need to remove type="button" from the <h:commandButton>. It should have been type="submit", which is the default already.

type="button"使其成为<input type="button">而不是<input type="submit">,这仅对通常使用onclick附加的客户端处理程序有用.

The type="button" makes it an <input type="button"> instead of <input type="submit"> which is only useful for client side handlers which you usually attach using onclick and so on.

这篇关于&lt; h:commandButton&gt;不发起回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆