1

我有以下豆:

public class MyBean {

public ArrayList<ReportRow> getReportRows()
    {
        return reportRows;
    }



    private final ArrayList<ReportRow> reportRows = 
        new ArrayList<ReportRow>(Arrays.asList(

                new ReportRow("","")
    ));

    public ArrayList<ReportRow> getOrderList() {

        return reportRows;

    }

    public String addAction() {

        ReportRow row = new ReportRow("", "");
        reportRows.add(row);
        return null;
    }



    public class ReportRow{

        String reportColumnName;
        String reportColumnDesc;

        public ReportRow(String reportColumnName,String reportColumnDesc) {

            this.reportColumnName=reportColumnName;
            this.reportColumnDesc=reportColumnDesc;
        }

        public String getReportColumnName()
        {
            return reportColumnName;
        }

        public void setReportColumnName(String reportColumnName)
        {
            this.reportColumnName = reportColumnName;
        }

        public String getReportColumnDesc()
        {
            return reportColumnDesc;
        }

        public void setReportColumnDesc(String reportColumnDesc)
        {
            this.reportColumnDesc = reportColumnDesc;
        }

    }

}

jsf页面:

<t:dataTable value="#{myBean.reportRows}" var="o"
            id="reportColumnsTable" styleClass="standardTable" headerClass="standardTable_Header"
            rowStyleClass="#{myBean.viewDelayedRsd}"
            >

            <h:column>

            <t:outputLabel value="Column name:"></t:outputLabel>
            <t:inputText id="ReportColumnName" value="#{o.reportColumnName}" required="true">
            </t:inputText>

            </h:column>

            <h:column>

            <t:outputLabel value="Column Desc:"></t:outputLabel>
            <t:inputText id="ReportColumnDesc" value="#{o.reportColumnDesc}" >

            </t:inputText>

            </h:column>

            <h:column>

            <h:outputLink value="#add"><h:outputText value="Add"/>
                        <a4j:support ajaxSingle="true" event="onclick" action="#{rprtBean.addAction}"
                        reRender="reportColumnsTable,msgPanel" />                       
            </h:outputLink>

            </h:column>

            </t:dataTable>

问题是当我点击添加时,它会生成一个新行,并清除旧行,我想保持旧行的值,有什么想法吗?

4

2 回答 2

4

您使用的是 a<h:outputLink>而不是 a <h:commandLink><h:outputLink>根本不提交表单,它会触发一个普通的 GET 请求。将<a4j:support>无法在<h:outputLink>. 将其替换为<h:commandLink>

<h:commandLink value="Add" action="#{rprtBean.addAction}">
    <a4j:support reRender="reportColumnsTable,msgPanel" ajaxSingle="true" />
</h:commandLink>

然后,您需要确保为后续请求保留数据模型,以防您的 bean 是请求范围的。有几种方法可以实现这一点:

  1. 将Tomahawk 数据表设置preserveDataModeltrue

    <t:dataTable preserveDataModel="true">
    
  2. 或者将 bean 状态保存在视图范围内。在页面某处添加以下标签:

    <t:saveState value="#{myBean}" />
    

    或者因为您也在使用 RichFaces/Ajax4jsf:

    <a4j:keepAlive beanName="myBean" />
    
于 2011-09-10T13:41:24.567 回答
1

我刚刚使用了 a4j 命令链接,一切正常。

于 2011-09-12T13:17:09.417 回答