I am using the Spark Java Web Framework with the Apache speed template engine to help develop a responsive web application that retrieves data from an SQL database. Using SQL2o, I created some Java objects of custom class types, i.e. user, group, site, etc.
I checked and the list of created objects is populated. When I go to put a list of objects in a hash map and return ModelandView, for some reason my list is there, but I cannot use any of its properties in vtl.
The corresponding part of the main method and spark code:
public static void main(String[] args) { WEB_LOGMGR loggr = new WEB_LOGMGR(true); WEB_DBMGR dbmgr = new WEB_DBMGR(true, loggr); Model backend = new ScadaModel(dbmgr, loggr); System.out.println(dataToJson(backend.getUsers())); staticFiles.location("/"); staticFiles.externalLocation("/"); String layout = "/templates/layout.vtl"; //secure("public/keystore.jks", "password", null, null); before("/form", (request, response) -> { boolean authenticated = false; // ... check if authenticated if (!authenticated) { halt(401, "You are not welcome here"); } }); get("/", (req, res) -> { HashMap pdata = new HashMap(); pdata.put("template", "/templates/main.vtl"); return new ModelAndView(pdata, layout); }, new VelocityTemplateEngine()); get("/users", (req, res) -> { HashMap pdata = new HashMap(); pdata.put("template", "/templates/users.vtl"); pdata.put("users", backend.getUsers()); return new ModelAndView(pdata, layout); }, new VelocityTemplateEngine());
Corresponding part of Parsed User VTL
<div class="w3-row-padding w3-margin-bottom"> <div class="w3-container"> <h5>SCADA Users</h5> <ul class="w3-ul w3-card-4 w3-white"> #foreach( $user in $users ) <li class="w3-padding-16"> <img src="/images/cole.jpg" class="w3-left w3-circle w3-margin-right" style="width:35px"> <span class="w3-xlarge">$user.firstName</span><br> </li> #end </ul> </div> </div>
The corresponding part of the VTL layout
<div class="w3-overlay w3-hide-large w3-animate-opacity" onclick="w3_close()" style="cursor:pointer" title="close side menu" id="myOverlay"></div> #parse( $template )