java - View binding & ApplicationScoped bean vs RequestScoped bean -


here 2 working example doing same purpose: retrieving faq results global application. fetch results according input value , update datatable.

first 1 use requestscoped , bean bindng.
second 1 use applicationscoped bean , 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 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 backend service based on filter        return dosomething(filter);    } 

so 1 seems way go jsf.
2 seems better solution me cause doesn't create/destroy bean @ each execution, bean static application.

but way of thinking kinda rid of requestscoped bean doesn't feel right. maybe solution worse first one, don't know how managed.

so what's better way go ?

this matter of opinion rather analysis of how costs server create new bean instance per request. if retrieve 1 single value per request, of these implementations valid , maybe should use latter instead, but since doesn't real use case don't perform business logic , retrieve single result can have different filters per request, 1st better since getters must clean possible. means, in jsf managed beans, getters should not have business logic @ since can called multiple times application. so, when add business logic process filter, realize every request can return different results, leads use former implementation using @requestscoped bean.

since real problem retrieving faq info global application, should load faq data in global resource application cache space or in @applicationscoped bean, perform each request against global resource not hit database.

more info:


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -