3
votes
  1. I have a jsf page (page1.xhtml) where within a form is a "h:datatable" defined and filled with values from database by using hibernate. this is working fine.

  2. Additionally I placed a "h:commandButton" (Create) on the same page. By clicking the button a new jsf page will be opened (page2.xhtml). On the new page I can set values to some h:inputText elements and can save them with a Button "Save" to the database. This is also working fine because I can see the new values in the database table.

  3. The return value of my save method is the name of the jsf page (page1.xhtml) where the dataTable is shown.

  4. The question I have now is how can I update or refresh the dataTable to show, beside the existing values, also the new value? Do I have to read them again from DB?

  5. My ManagedBean is SessionScoped and I'm using JSF 2.2

I checked this http://balusc.blogspot.de/2006/06/using-datatables.html but I was unable to find the corresponding information.

Please, can anyone assist?

1

1 Answers

3
votes

You have two alternatives.

The first alternative is to switch to @ViewScoped bean for page 1 so that service.load() will be called when the page is initially requested. This way the refreshed data will be fetched from the database as soon as the page is requested, thus the data will be up-to-date and that will be done transparently to you.

The second alternative is to keep on using a @SessionScoped bean. But in page 2 you need to inject that session scoped bean by using @ManagedProperty and add the already persisted entry from the database to the data list kept in the session bean by calling sessionBean.getData(persistedElement);. This way your session scoped bean will contain up-to-date values and that is your responsibility.