Skip to content

Build the "Model" again!

  • Create model package and add our good old POJO: Course class.

  • Create dao package and bring over the CourseDao interface.

Let's provide a simple in-memory (no persistence) implementation for CourseDao:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class InMemoryCourseDao implements CourseDao {
  private List<Course> courseList;

  public InMemoryCourseDao() {
    courseList = new ArrayList<>();
  }

  @Override
  public void add(Course course) {
    courseList.add(course);
  }

  @Override
  public List<Course> findAll() {
    return Collections.unmodifiableList(courseList);
  }
}

Add the M in MVC

Let's update the WebServer to pass some sample courses to the courses.hbs:

1
2
3
4
5
6
7
8
9
CourseDao courseDao = new InMemoryCourseDao();
courseDao.add(new Course("OOSE", "jhu-oose.com"));
courseDao.add(new Course("Gateway", "jhu-gateway.com"));

get("/courses", (req, res) -> {
    Map<String, Object> model = new HashMap<>();
    model.put("courseList", courseDao.findAll());
    return new ModelAndView(model, "courses.hbs"); 
}, new HandlebarsTemplateEngine());

The power of handlebars is in that it can parse the Java object it receives; replace the following in courses.hbs

1
2
3
4
<ul>
    <li>name1  at  url1</li>
    <li>name2  at  url2</li>
</ul>

with this:

1
2
3
4
5
<ul>
    {{#each courseList }}
        <li>{{ name }} at {{ url }}</li>
    {{/each }}
</ul>

Note the use of {{ }} as part of the handlebars templating engine. Handlebars has several constructs; the one we've used above is the #each construct to iterate over a collection. Handlebars receives courseList and it knows it is an Iterable Java collection. Inside the #each block, you have access to each element of courseList (i.e. a Course object). Moreover, handlebars knows Course is a POJO, so when you write {{ name }}, handlebars understands you are looking for the property (field) called name and it uses the getter methods of Course to retrieve it (i.e. getName()).