0

I'm running a query using ActiveJdbc

List<Game> games = District.findAll("where createor_id = ?", creatorId);

And according o the documentation the query is triggered when I do this

    for (Game game : games) {
        //do things with result
    }

But I want to put the result in a ModelMap in order to use in in the jstl view (Spring mvc 4). So How can I trigger the query? right now in order to trigger the query I have to do

game.size();

But I guess it's an optimal solution.

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
Rony
  • 101
  • 1
  • 8

1 Answers1

0

You should not worry about when the list will make a trip to the database. If you just pass the games object to the JSP, then it will make DB call during rendering of the page. Also, you do not need to make additional ModelMap, just pass a list to a view.

If you insist on passing maps to JSP, you can do this:

List<Map> games = District.findAll("where createor_id = ?", creatorId).toMaps();

I hope it helps!

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • I don't use scriptlet I use jstl so I use my . And it doesn't trigger anything if the List is empty. If I use it with scriptlet <% for (Game game : games) { } %> it would work. So yeah, I'll use the solution you gave me. Thanks. – Rony Feb 17 '17 at 18:31
  • in orde to know if the list is empty, someone will try to call `list.size()` or another method. This will trigger a trip to a database. In any case, glad it's working for you. – ipolevoy Feb 17 '17 at 22:20