0

Here is 2 working example doing same purpose: Retrieving FAQ results which are global to the application. We fetch results according to the input value and update the datatable.

First one use a RequestScoped and bean bindng.
The second one use an ApplicationScoped bean and view binding.

Example 1 :

xhtml

<h:inputText id="faqSearch" value="#{faqBean.filter}"/>
<h:commandButton value="submit" ajax="true" update="faqResults"/>

<h:dataTable
    id="faqResults"
    value="#{faqBean.retrieveResults()}"
    var="result">

        <h:column>${result}</h:column>

</h:dataTable>

Bean

@RequestScoped
@ManagedBean
public class FaqBean {

   String filter;

   public String getFilter(){
       return filter;
   }

   public void setFilter(String filter){
       this.filter = filter;
   }

   public List<String> retrieveResults(){   
       //compute result from a backend service based on filter
       return doSomething(filter);
   }

Example 2 :

xhtml

<h:inputText id="faqSearch" binding="#{filter}"/>
<h:commandButton value="submit" ajax="true" update="faqResults"/>

<h:dataTable
    id="faqResults"
    value="#{faqBean.retrieveResults(filter.value)}"
    var="result">

        <h:column>${result}</h:column>

</h:dataTable>

Bean

@ApplicationScoped
@ManagedBean
public class FaqBean {
   public List<String> retriveResults(String filter){   
       //Compute result from a backend service based on filter
       return doSomething(filter);
   }

So 1 seems the good way to go with jsf.
But 2 seems a better solution for me cause it doesn't create/destroy a bean at each execution, bean is static to the application.

But with this way of thinking we could kinda get rid of all requestscoped bean so it doesn't feel right. Maybe this solution is worse than the first one, I don't know how all this is managed.

So what's the better way to go ?

Ghetolay
  • 3,222
  • 2
  • 30
  • 29

1 Answers1

0

This would be a matter of opinion rather than an analysis of how much it costs for the server to create a new bean instance per request. If you will only retrieve 1 single value per request, then any of these implementations is valid and maybe you should use latter instead, but since it doesn't look like a real use case where you don't perform any business logic and always retrieve a single result but you can have different filters per request, then the 1st is better since your getters must be as clean as possible. This means, in JSF managed beans, getters should not have any business logic at all since they can be called multiple times by the application. So, when you add some business logic to process the filter, you will realize that every request can return different results, which leads to use the former implementation using a @RequestScoped bean.

Since your real problem is about retrieving the FAQ info that is global for all the application, you should load the FAQ data in a global resource for the application like a cache space or in another @ApplicationScoped bean, then perform each request against this global resource to not hit your database.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • I don't know if I'm confused or you are. GetResultsName() may be bad naming. It's not a getter at all it's a function operating logic and returning different results. – Ghetolay Mar 24 '14 at 16:22