0

我的 p:commandButton 没有触发 action 或 actionListener (我都试过了),我不知道是什么导致了这个问题。

两天前我一直在搜索这个网站,直到现在测试你的解决方案,但没有一个对我有用。

我正在使用 JSF 2.1 和 primefaces 3.5

这是我的代码

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="../template/template.xhtml">

<ui:define name="body">
        <div id="button_bar">
            <div class="button_bar_left">
                <h:form id="frm_form" enctype="multipart/form-data">
                    <p:fileUpload fileUploadListener="#{import.handleFile}"
                        id="fup"
                        mode="advanced" 
                        update=":form_table:table, :frm_btns:btn_remove, :frm_btns:btn_save, fup"
                        allowTypes="/(\.|\/)(xlsx)$/"  />
                </h:form>
            </div>
            <div class="button_bar_right">
                <h:form id="frm_btns" >
                    <p:commandButton id="btn_save"
                        disabled="#{empty import.items}"
                        actionListener="#{import.save}"
                        update=":form_table:table,btn_save,btn_remove" 
                        value="#{msg['import.save']}"
                        ajax="false"
                        process="@all" 
                        icon="ui-icon-disk" />

                    <p:commandButton id="btn_remove" 
                        action="#{import.remove}" 
                        disabled="#{empty import.items}"
                        value="#{msg['import.remove']}"
                        update=":form_table:table,btn_save,btn_remove"
                        icon="ui-icon-trash" />

                    <p:commandButton id="btn_return" 
                        action="tecnical_team" 
                        value="#{msg['import.return']}"
                        icon="ui-icon-triangle-1-w"
                        ajax="false" />

                    <p:blockUI block=":form_table:table" trigger="#{p:component('btn_save')}">
                        <p:graphicImage value="${pageContext.request.contextPath}/resources/images/ajax-loader.gif" />
                    </p:blockUI>
                </h:form>           
            </div>
        </div>



        <div class="clear" />

        <div class="legend align_right">
            <div class="little_square_legend align_left" />
            <div class="text_legend"><h:outputText value="#{msg['import.legend']}"/></div>
        </div>

        <div class="clear" /><br/>

        <h:panelGroup id="pg_table" styleClass="scrollable" layout="block">
            <h:form id="form_table">
                <p:dataTable id="table" var="item" value="#{import.items}" paginator="true" 
                    paginatorPosition="bottom" rows="20" rowStyleClass="#{item.problem ? 'problem_line' : null}">
                    <p:column>
                        <f:facet name="header">
                            <p:selectBooleanCheckbox value="#{import.allSelected}">
                                <p:ajax update="table" listener="#{import.selectAll}" />
                            </p:selectBooleanCheckbox>
                        </f:facet>
                        <p:selectBooleanCheckbox value="#{item.selected}" />
                    </p:column>
                    <p:column headerText="#{msg['import.table.item']}" sortBy="#{item.itemId}">  
                        <h:outputText value="#{item.nrItem}"/>
                    </p:column>
                    <p:column headerText="#{msg['import.table.sc.number']}">
                        <h:outputText value="#{item.nrSc}"/>
                    </p:column>
                    <p:column headerText="#{msg['import.table.item.group']}"> 
                        <h:outputText value="#{item.contract.nmContract}" />
                    </p:column>
                    <p:column headerText="#{msg['import.table.applicant']}"> 
                        <h:outputText value="#{item.applicant}"/>
                    </p:column>
                </p:dataTable>
            </h:form>
        </h:panelGroup>
</ui:define>

这是我的托管 Bean:

package com.xxx.web.mb;

import static com.xxx.enumeration.AuthorityType.PROJECT_MANAGER;
import static com.xxx.excel.ExcelToEntity.items;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;


@ManagedBean(name = "import")
@ViewScoped
public class ImportView implements Serializable {
    private static final long serialVersionUID = 491969435579306400L;

    private List<Item> items;
    private Boolean allSelected;

    @ManagedProperty(value = "#{i18n}") private I18n i18n;
    @ManagedProperty(value = "#{unicaSession}") private Session session;
    @ManagedProperty(value = "#{importService}") private ImportService service;
    @ManagedProperty(value = "#{emailService}") private EmailService emailService;


    /**
     * 
     * @return
     */
    public String save() {
        try {
            int saved = 0;
            System.out.println("Hello !");
            List<Item> selected = new ArrayList<Item>();
            if (items != null) {
                for (Item item : items) {
                    if (item.getSelected() && !item.getProblem()) {
                        item.setProject(session.getProject());
                        item.setDtRegistration(new Date());
                        selected.add(item);
                        saved ++;
                    } else if (item.getSelected() && item.getProblem()) {
                        Faces.errorMessage("A row with a problem is seleted");
                        return null;
                        //return;
                    }
                }

                if (saved == 0) {
                    Faces.infoMessage("Select at least one item");
                    return null;
                    //return;
                }

                service.save(selected);
                items.clear();

                emailService.send(i18n.getMessage("mail.message.new.items", saved), PROJECT_MANAGER);

                Faces.infoMessage(i18n.getMessage("import.mb.saved.items", saved));
            }
        } catch (ServiceException e) {
            Faces.errorMessage(i18n.getMessage("import.mb.error.save.items"));
        }
        return null;
    }

    /**
     * 
     * @return
     */
    public String remove() {
        boolean removed = false;
        if (items != null) {
            Iterator<Item> iterator = items.iterator();
            while (iterator.hasNext()) {
                Item item = (Item) iterator.next();
                if (item.getSelected()) {
                    iterator.remove();
                    removed = true;
                }
            }
        }

        if (removed) {
            Faces.infoMessage(i18n.getMessage("import.mb.success.remove.items"));
        } else {
            Faces.alertMessage(i18n.getMessage("import.mb.error.none.selected"));
        }

        return null;
    }

    public void handleFile(FileUploadEvent event) {
        try {
            if (items != null && !items.isEmpty()) {
                items.clear();
            }

            UploadedFile file = event.getFile();
            InputStream excel = file.getInputstream();
            ExcelMapping excelMapping = new ExcelMapping(excel);
            excelMapping.mapping();

            items = items(excelMapping.getExcelData());

            validate(items);

            if (FilterProblem.instance().notEmpty()) {
                Faces.errorMessage(FilterProblem.instance().get());
            }


        } catch (ValidationException e) {
            if (Assert.isNotNullOrEmpty(items)) { items.clear(); }
            Faces.errorMessage(e.getMessage());
        } catch (IOException e) {
            if (Assert.isNotNullOrEmpty(items)) { items.clear(); }
            Faces.errorMessage(i18n.getMessage("import.mb.error.handle.file"));
        } catch (Exception e) {
            if (Assert.isNotNullOrEmpty(items)) { items.clear(); }
            Faces.errorMessage(i18n.getMessage("import.mb.error.obligatory.template"));
        } finally {
            FilterProblem.instance().clear();
        }
    }

    public void selectAll() {
        for (Item item : items) {
            if (allSelected) {
                item.setSelected(true);
            } else {
                item.setSelected(false);
            }
        }
    }

    private void validate(List<Item> items) {
        FilterChain<Item> chain = new FilterChain<Item>();

        if (!items.isEmpty()) {
            chain.add(new SCNumberFilter(items.get(0), service));
        }

        for (Item item : items) {
            chain.add(new RequiredFilter(item));
            chain.add(new DomainValidationFilter(item, service));
            chain.add(new ViewValidationFilter(item, service));
        }

        chain.execute();
    }


    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }

    public Boolean getAllSelected() {
        return allSelected;
    }

    public void setAllSelected(Boolean allSelected) {
        this.allSelected = allSelected;
    }

    public void setService(ImportService service) {
        this.service = service;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public void setI18n(I18n i18n) {
        this.i18n = i18n;
    }

    public void setEmailService(EmailService emailService) {
        this.emailService = emailService;
    }
}

吓死我了!请帮我 !

谢谢 !

PS:对不起我的英语不好

4

1 回答 1

0

#{import.items}似乎只在handleFile()方法中被初始化。<p:commandButtons/>在您的支持 bean 中调用方法的唯一两个是btn_removebtn_add。它们都被标记为disabled="#{empty import.items}",这意味着这些按钮最初是禁用的。另请注意,两者都位于<h:form id="frm_btns"/>.

这就是问题所在- 似乎,唯一尝试(并且失败)更新按钮(即使其功能正常)的地方是您的文件上传组件,但更新属性说明update=":form_table:table, :form_table:btn_remove, :form_table:btn_save, fup"它应该是什么时候update=":form_table:table, :frm_btns:btn_remove, :frm_btns:btn_save, fup"

下次,请在发帖前检查您的身份证,并一定要在http://sscce.org/上阅读。

于 2013-08-14T20:10:51.757 回答