1

i have the following bean:

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 page:

<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>

problem is that when i click on add, it generates a new row, and clear the old one, and i want to maintain the values of old row, any ideas ?

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

2 Answers2

4

You're using a <h:outputLink> instead of a <h:commandLink>. The <h:outputLink> doesn't submit the form at all, it fires a plain GET request. The <a4j:support> won't work properly inside a <h:outputLink>. Replace it by a <h:commandLink>:

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

Then, you need to ensure that you preserve the data model for subsequent request in case that your bean is request scoped. There are several ways to achieve this:

  1. Set either Tomahawk datatable's preserveDataModel to true:

    <t:dataTable preserveDataModel="true">
    
  2. Or save the bean state in the view scope. Add the following tag somewhere in the page:

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

    or since you're also using RichFaces/Ajax4jsf:

    <a4j:keepAlive beanName="myBean" />
    
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • when i use first solution it gives an exception: java.io.NotSerializableException, so i made the bean implements serilizable, the error gone, but when i tried it, nothing new, still old row text fileds reset – Mahmoud Saleh Sep 10 '11 at 14:04
  • Yes, the bean must implement serializable. Try the ``. I'm by the way not sure how that works in combination with ``. Perhaps the `` is the only which would work. At least, the point is that you need to preserve the same datamodel/bean in subsequent requests. The JSF 2.x solution would by the way be to put the bean in view scope. – BalusC Sep 10 '11 at 14:07
  • third solution gives me an exception too: Attribute 'name' for view scope bean must be literal – Mahmoud Saleh Sep 10 '11 at 14:07
  • According to the [RF 3.3.x documentation](http://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/a4j_keepAlive.html) the attribute name should be `beanName` and it should accept EL. What RF/A4J version are you using? The exception basically tells that it should actually be a literal like ``. Give it a try, apparently you're using an older version. – BalusC Sep 10 '11 at 14:12
  • by myBean do you mean bean name or instance of bean ? both doesn't work for sorry with third solution. – Mahmoud Saleh Sep 10 '11 at 14:38
  • Yes, the bean name as you have in `#{myBean}` in your question. Sure that you didn't use all ways simultaneously? It has to be the one or other, not all. To naildown the culprit further, try removing `` and let me know. – BalusC Sep 10 '11 at 14:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3320/discussion-between-sword101-and-balusc) – Mahmoud Saleh Sep 10 '11 at 14:49
1

i just used the a4j command link and everything worked fine.

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498