Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Intro to Java Web Development with Spark Bells and Whistles Adding to and Iterating a List

Carlos Sanchez
Carlos Sanchez
8,900 Points

How to send values to templates with {{ variable }} tags?

I am implementing my own version of this project, and part of my own implementation is that I added a database to which i can connect and perform CRUD operations. Right now I am trying to print in one of the templates the data I am retrieving from the DB, however it does not seem to work as the page does not render anything, The data is good and not null since it prints the values when I do the print statement. Therefore I figure that the template is never receiving the data, what am I ding wrong? Its so much easier to do this part in NodeJS, why is the Handlebars iteration in java so much harder? :(

main.java

public class Main {
    public static void main(String[] args) {

        get("/", (req, res) -> "Hello");

        get("/hello", (req, res) -> {
            BookDAO bookTransfer = new BookDAO();
            List<BookDTO> bookList = bookTransfer.readAll();
            bookList.stream().forEach(System.out::println);

            return new ModelAndView(bookList, "list.hbs");
        }, new HandlebarsTemplateEngine());
    }
}

list.hbs

    {{#each bookList}}
        <h3> this.getName() </h3>
    {{/each}}

1 Answer

Carlos Sanchez
Carlos Sanchez
8,900 Points

Ok so I went back and rewatched the video again and came up with this fix:

public class Main {
    public static void main(String[] args) {

        get("/", (req, res) -> {
            BookDAO bookTransfer = new BookDAO();
            List<BookDTO> bookList = bookTransfer.readAll();
            bookList.stream().forEach(System.out::println);

            Map<String, Object> model = new HashMap<>();
            model.put("books", bookList);

            return new ModelAndView(model, "list.hbs");
        }, new HandlebarsTemplateEngine());
    }
}

base.hbs

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title> {{#block "title"}} Welcome {{/block}} </title>
</head>
<body>
    {{#block "body"}}


    {{/block}}
</body>
</html>

list.hbs

{{#partial "body"}}
    <div>
    {{#each books}}
        <h3> {{ name }}</h3>
    {{/each}}
    </div>
{{/partial}}

{{>base.hbs}}