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 ?