1

I have an requirement where I have to populate multiple number of dropdowns with values which will be fetched from database. I am using struts2 framework and calling a json action which return json type.But the problem here is for each and every dropdown I had to write a separate action which is causing many different calls to DB.

Is it possible to do this in one single json action call so that all the required values can be fetched using one json action only and values will be populated in dropdowns accordingly.

Currently what I am doing looks like this which is working fine but I am looking for a solution(if there exists one) where we will not need these many <s:url/> calls.

<s:url var="fillA" action="fillA" />
<sj:select id="dropdownA" href="%{fillA}" list="listA"/>

<s:url var="fillB" action="fillB" />
<sj:select id="dropdownB" href="%{fillB}" list="listB"/>

<s:url var="fillC" action="fillC" />
<sj:select id="dropdownC" href="%{fillC}" list="listC"/>
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Gaurav Parek
  • 317
  • 6
  • 20
  • 2
    Even with a single action you'd need three different queries (unless doing the circus), so the advantage will be what, avoiding the opening / closing of the connection (isn't it pooled ?). In case like this I suggest a cache, per-user or per-application with a @Singleton according to your project specifications (number of users, number of data, number of concurrent connections etc...) – Andrea Ligios Sep 16 '15 at 19:04
  • If you absolutely had to avoid multiple requests, then I'd consolidate them into a single action which returned a JSON object containing each of the resultant objects produced by your three original methods. Also, wouldn't having three separate `` tags automatically perform three separate queries, even if they were pointed at the same URL? – Shotgun Ninja Sep 16 '15 at 19:16
  • 1
    @AndreaLigios thanks. – Gaurav Parek Sep 19 '15 at 16:06
  • AFAIR sj;select preselects those values in the response but if you use autocomplete option then for every response many XHR requests are made. If it's one resource that it could be fetched by the single XHR request but if it different sources then you can't do anything with it, the browser handles situations using cache control headers. – Roman C Sep 24 '15 at 12:20
  • @AndreaLigios: I looked into and for our requirement it would be better to have cache per application basis, I have not implemented caching before if you could provide any link or example for such implementation would be helpful for me. – Gaurav Parek Oct 06 '15 at 04:25
  • 1
    @GauravParek [you're very lucky](http://stackoverflow.com/questions/26733141/load-and-cache-application-scoped-data-with-singleton-and-stateless). Feel free to upvote the question and/or the answer. – Andrea Ligios Oct 06 '15 at 07:50

0 Answers0