0

Form and table are on the same page. When user adds form details and clicks add, the data should be copied to the table.

When user enters the details and clicks add for the second time(already existing data in table) the first value is overwritten by the new value and the next in list is empty.

How can we display both values on the table.

Name.xhtml

<h:form>
  <h:inputText value="#{bean.fName}"/>
  <h:inputText value="#{bean.lName}"/>

   <h:dataTable value="#{bean.list}" var="name">
     <h:column>
       <h:outputText value="#{name.fName}">
     </h:column> 
   </h:dataTable>
   <h:commandLink action="#{bean.add}"/>
 </h:form>

Bean.java //Bean class is ViewScoped

@ManagedProperty(value="#{buyerTO}") protected BuyerTo buyerTO;

List buyerTOList = new ArrayList(); // BuyerTO is in ViewScope

public String addBuyer(){
 buyerTOList.add(buyerTO);
 buyerTO = new BuyerTO();
 return "";

}

user679526
  • 845
  • 4
  • 16
  • 39

1 Answers1

2

You're ending the view scope by returning non-null/void. You need to return null or void in order to retain the view scope for the subsequent request.

Replace

return "";

by

return null;

or

public void addBuyer() {
    // ...
    // No return statement.
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • A return of null is not resolving the issue. A new view is created every time a button is clicked on the page. – user679526 Oct 29 '12 at 15:58
  • Apparently you've bound a property of the view scoped bean to some tag/attribute which runs during view build time. The code posted so far in the question does however not indicate that. So it's caused elsewhere. For detailed background information, head to this answer: http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense/3343681#3343681 – BalusC Oct 29 '12 at 16:00
  • h:dataTable is a composite component in my code. Looks like we cannot use view scoped bean attributed in composite components. – user679526 Oct 29 '12 at 17:18
  • Depends on if they are used during view build time or view render time. Your problem indicates that it's been used during view build time. – BalusC Oct 29 '12 at 17:29