0

My question is about the best practice how to handle the resultset of a SQL statement in a JSF application? In most examples it is recommended to use a ViewScoped backing bean with a method like:

@ManagedBean
@ViewScoped
public class MyController implements Serializable {
   ...
    public List<MyData> getResult()  {
      // select data from SQL database
      ....
    }

And the JSF page looks like this:

<h:dataTable value="#{myController.result}" var="record">
  ...
</h:dataTable>

But from my tests I can see, that the method getResult() is called several times when the page is rendered. This means that my SQL Query is called several times. This is - I think - a bad design.

So I do store the result in a member variable:

@ManagedBean
@ViewScoped
public class MyController implements Serializable {
   private List<MyData> data=null;
   ...
    public List<MyData> getResult()  {
      if (data!=null) 
        return data; 
      else {
        // select data from SQL database
        ....
        data=....;
       }
    }

But with this approach my bean now stores the data on the server. And this wastes memory!

For this reason I use a RequestScoped DataController. With this approach the result dataset it not unnecessarily stored on the server after the page was rendered.

I cannot understand why most of the examples concerning JSF with h:datatables don't go up on these two very important aspects.

So my question is: What is the recommended design of a JSF Backing Bean to query data from a database?

Ralph
  • 4,500
  • 9
  • 48
  • 87
  • *"In most examples it is recommended to use a ViewScoped backing bean with a method like"* next time please post links to these examples, so that we can correct these. – BalusC Dec 10 '18 at 10:17
  • 'old tutorials' are most often wrong since they were often written not with 'writing good examles' in mind but to attract visitors for getting an income from adds. And google is not good in this regard either since it keeps these wrong old tutorials at the top because many people clicked on the results in the links and stayed there for a while. Using a site like https://jsf.zeef.com is a better place to start (or http://www.stackoverflow.com/tags/jsf/info) – Kukeltje Dec 10 '18 at 10:43
  • Thanks for these comments. This makes all sense. I edited my question and added the aspect of a pagination or filter mechanism. How to solve this? – Ralph Dec 11 '18 at 18:02
  • 'Answering' a question in the question is not good practice in stackoverflow. And please ask new questions in new questions. – Kukeltje Dec 11 '18 at 19:55
  • OK, I removed my own "Answering" from my question. Sorry for the incorrect application of StackOverflow. In my own blog, I summarize my conclusions: https://ralph.blog.imixs.com/2018/12/08/jsf-best-practice/ – Ralph Dec 11 '18 at 20:56
  • Thanks, no problem. But I'm curious about the additional question. – Kukeltje Dec 11 '18 at 21:33

1 Answers1

0

Its a normal practice never write any business login inside get/set() method so below code is totally wrong

public class MyController implements Serializable {
   ...
    public List<MyData> getResult()  {
      // select data from SQL database
      ....
    }

Please have a look on this question and read the answer given by @Balusc

Why JSF calls getters multiple times

If you want to load the data there are plenty of way in JSF

You can use preRenderView and call method which will load the data

<f:metadata>
          <f:event type="preRenderView" listener="#{myController.fetchResult}"/>
 </f:metadata>

I am not sure about your page design otherwise with some event also you can load the data into datatable.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202