Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Spark/Velocity Templates/SQL2o

I'm using the Spark Java Web Framework with Apache's Velocity Template Engine in order to help design a responsive web application that pulls data from a SQL database. Using SQL2o I've created some Java objects of custom class types, i.e. user, group, site, etc.

I've checked and the list of objects created is populated. When I then go to put my object list into a hashmap and return a ModelandView, for some reason my list is there but I can't use any of its properties in vtl.

Relevant portion of 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());

Relevant portion 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>

Relevant portion of Layout VTL

<!-- 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 )
like image 638
TheFunk Avatar asked Nov 08 '22 16:11

TheFunk


1 Answers

I have found my answer! The class that defined my user object was not defined as "public" and as such the data was inaccessible to the template file.

To anyone that has followed the tutorials over on the sparkjava site and tried the annotation processor Project Lombok, you'll have created a java file with multiple class definitions, none of which are declared public. Lombok should take care of this for you. If however you're like me and you didn't like how hacky Lombok felt, you may have created class files and copied and pasted the code out of your single java file into individual class definition files. Make sure to declare your classes public!

like image 140
TheFunk Avatar answered Nov 15 '22 12:11

TheFunk