PrimeFaces。数据表选择行和对话框的表单

时间:2012-08-01 12:56:53

标签: java-ee jsf-2 datatable primefaces selecteditem

我有下一个JSF代码:
按钮:

        <p:layoutUnit id="centerLayoutUnit" position="center">
            <h:form id="form" styleClass="my_form">

                <p:growl id="msgs" showDetail="false" />

                <p:toolbar id="alarmTableToolbar">
                    <p:toolbarGroup align="left">
                        <p:commandButton value="Ack/comment" disabled="false"
                            action="#{alarmTable.checkAllSelectedAlarmsIsInOneAckCondition()}"
                            icon="ui-icon-close" update="msgs :ackDialog"
                            style="min-width : 75px; height : 26px" />

........

并以相同的形式和layoutUnit dataTable:

                <p:dataTable id="alarmTable" value="#{alarmTable.alarms}"
                    selection="#{alarmTable.selectedAlarm}" selectionMode="multiple"
                    var="alarm" resizableColumns="true" paginator="true" rows="50"
                    emptyMessage="No alarms" sortBy="#{alarm.alarmTs}"
                    sortOrder="descending"
                    paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                    rowsPerPageTemplate="25, 50, 75, 100" lazy="true"
                    style="table-layout: auto" dblClickSelect="true">

同一个LayoutUnit中的对话框,但不是相同的形式:

            <p:dialog id="ackDialog" header="Acknowledge/comment dialog"
                widgetVar="ackDlg" modal="false" height="143" width="285"
                dynamic="false" resizable="false" appendToBody="false">

                <h:form id="formAckDialog">
                    <h:selectOneMenu id="ackTemplate"
                        value="#{alarmTable.selectedIdOfAckTemplate}" required="false"
                        disabled="#{alarmTable.checkSelectedAlarmsIsAck()}">
                        <f:selectItem itemLabel="template selection" itemValue="" />
                        <f:selectItems value="#{alarmTable.initAckTemplatesName()}" />
                    </h:selectOneMenu>

                    <p:inputTextarea rows="4" cols="47" counter="counter"
                        maxlength="1024" counterTemplate="{0} characters remaining."
                        autoResize="true" value="#{alarmTable.commentText}" />
                    <h:outputText id="counter" />

                    <div align="right">
                        <p:commandButton id="ackComment" value="Ack/comment" disabled="false"
                            actionListener="#{alarmTable.sumbitAckComment}"
                            update=":form:msgs" style="min-width : 75px; height : 26px;" />
                    </div>
                </h:form>
            </p:dialog>

AlarmTable是一个bean:

@ManagedBean
@SessionScoped

和字段:

private MyClass[] selectedAlarm;

方法:

 public boolean checkAllSelectedAlarmsIsInOneAckCondition() {
    if (selectedAlarm == null || selectedAlarm.length == 0) {
        JSFUtil.addErrorMessage("No alarms are selected");
        return false;
    }

    RequestContext context = RequestContext.getCurrentInstance();
    for (AlarmMts alarm : selectedAlarm) {
        if (!selectedAlarm[0].isAck.equals(alarm.isAck)) {
        JSFUtil.addErrorMessage("Not all selected alarms are acknowledged or not acknowledged");
        return false;
        }
    }
    context.execute("ackDlg.show()");
    return true;
    }

public boolean checkSelectedAlarmsIsAck() {
    if (selectedAlarm == null || selectedAlarm.length == 0)
        return false;
    for (AlarmMts alarm : selectedAlarm) {
        if (!alarm.isAck.equals(new BigDecimal(1)))
        return false;
    }
    return true;
}


public void sumbitAckComment(ActionEvent actionEvent) {
    boolean closeDialog = false;
    if ((selectedIdOfAckTemplate == null || selectedIdOfAckTemplate.equals(new BigDecimal(0))) && commentText.isEmpty())
        JSFUtil.addErrorMessage("Please fill in something");
    if (selectedIdOfAckTemplate != null && !selectedIdOfAckTemplate.equals(new BigDecimal(0))) {
        closeDialog = ackSelectedAlarms(Factory.getInstance().getAcknowledgeTemplateDAO().getAckTemplateById(selectedIdOfAckTemplate));
    }
    if (commentText != null && !commentText.isEmpty()) {
        closeDialog = commentSelectedAlarms(commentText);
    }
    if (closeDialog) {
        RequestContext.getCurrentInstance().execute("ackDlg.hide()");
    }
}

问题是,在对话框创建方法 checkAllSelectedAlarmsIsInOneAckCondition() checkSelectedAlarmsIsAck()期间,他们在中有一个正确的(!= null)选定项目selectedAlarm 字段。但是当用户按下按钮 - 对话框中的ackComment时, selectedAlarm 为空。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我不明白为什么,但是当我将SessionScope更改为ViewScope时,它已按预期工作。