0

Myh:form包含一个p:selectOneMenu组件,它有两个值 single 和 multiple。h:form还包含一个默认p:inputText值。p:inputText我的目标是仅在选择值倍数时添加多个组件。请参阅随附的屏幕截图- 在此处输入图像描述

下面是我的视图,假设ajax在单击图标按钮时发送请求-

<h:form>
    <p:panel header="Dynamically Add textbox">
        <p:selectOneMenu id="runType" value="#{repeateRun.runType}">
            <f:selectItems value="#{repeateRun.runList}" var="runType" itemLabel="#{runType}" itemValue="#{runType}" />
            <p:ajax update="outPanel" listener="#{repeateRun.renderComponent}" />
        </p:selectOneMenu>
        <h:panelGrid id="runList">
            <ui:repeat value="#{repeateRun.runs}" var="run">
                <p:inputText value="#{run.runValue}" />
            </ui:repeat>
        </h:panelGrid>
        <p:outputPanel id="outPanel">
            <p:commandButton update="runList" icon="ui-icon-plusthick" title="Add more" rendered="#{repeateRun.showAddButton}">
                <f:ajax render="runList" listener="#{repeateRun.addRun}" />
            </p:commandButton>
        </p:outputPanel>
    </p:panel>
</h:form>

RepeateRun@ViewScoped @ManagedBean如下-

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
@ManagedBean
@ViewScoped
public class RepeateRun implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<String> runList;
    private List<Run> runs;
    private int runValue;
    private String runType;
    private boolean showAddButton = false;
    private static final String SINGLE = "Single";
    private static final String MULTIPLE = "Multiple";
    //Note : Getters Setters are removed while putting here
    @PostConstruct
    public void initBean() {
        this.setRunList(this.populateRunList());
        this.setRuns(this.populateRuns());
    }
    public void addRun() {
        if (this.runs == null) {
            this.setRuns(this.populateRuns());
        } else {
            this.runs.add(this.defaultRun());
        }
    }
    public void renderComponent() {
        if (this.getRunType().equals(SINGLE)) {
            this.setShowAddButton(false);
        } else {
            this.setShowAddButton(true);
        }
    }
    private List<String> populateRunList() {
        List<String> runList = new ArrayList<String>();
        runList.add(SINGLE);
        runList.add(MULTIPLE);
        return runList;
    }
    private Run defaultRun() {
        Run defaultRun = new Run();
        defaultRun.setRunValue(1);
        return defaultRun;
    }
    private List<Run> populateRuns() {
        List<Run> runs = new ArrayList<Run>();
        runs.add(this.defaultRun());
        return runs;
    }
}

因此,在加号图标按钮中选择值 Multiple 后f:selectItems,按钮未调用附加方法,即addRun. 为了确认addRun点击后的方法调用,我在method.xml中放了一些sysout语句addRun。我看到了,sysout没有脸红。与此同时,我xml在萤火虫中看到了一些反应。
哪里有问题?

4

1 回答 1

2

问题在于f:ajaxwhich 不适用于p:commandButton. 以下是罪魁祸首-

<p:commandButton update="runList" icon="ui-icon-plusthick" title="Add more" rendered="#{repeateRun.showAddButton}">
     <f:ajax render="runList" listener="#{repeateRun.addRun}" />
</p:commandButton>

上面的行应该替换为下面的行

<p:commandButton actionListener="#{repeateRun.addRun}" update="runList" icon="ui-icon-plusthick" title="Add more" rendered="#{repeateRun.showAddButton}" />
于 2013-04-07T19:22:54.400 回答