Java Spark / Velocity / SQL2o Templates

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> <!-- The above line should return a name for my 3 users, but it doesn't. Removing the .firstname allows the code to run but it just returns User@ and then a memory location --> </li> #end </ul> </div> </div> 

The corresponding part of the VTL layout

 <!-- Overlay effect when opening sidebar on small screens --> <div class="w3-overlay w3-hide-large w3-animate-opacity" onclick="w3_close()" style="cursor:pointer" title="close side menu" id="myOverlay"></div> <!-- !PAGE CONTENT! --> #parse( $template ) 
+5
source share
1 answer

I found my answer! The class that my custom object defined was not defined as "public", and therefore the data was not available to the template file.

For those who followed the tutorials on sparkjava and tried the Project Lombok annotation handler, you created a Java file with several class definitions, none of which were declared public. Lombok should take care of this for you. If, however, you are like me and you donโ€™t like how Hacky Lombok felt, you may have created class files and copied and pasted code from one java file into the definition files of individual classes. Be sure to declare your classes public!

+1
source

Source: https://habr.com/ru/post/1271913/


All Articles