Europe: +41 78 715 83 09 - Asia: +84 ‎975 112 112
contact@finix.asia

Blog

28 Dec 2017

Kotlin Spring 5: JPA + MVC Part 2

//
Comments0

Kotlin: Spring 5 JPA + MVC Part 1

Technologies used:   Kotlin 1.2.10 | Spring 5 | Maven 3.3.9 | Spring-boot 2.0.0.M7

In this second post, we continue our study on Spring 5 MVC and Kotlin. In this post, we create our first controller and we use the ThymeLeaf template.

Create the first controller

As you know already it is quite forward to work with Spring, there is a few trick that doesn’t work same in Java and in Kotlin.

First of all, let’s respect the convention and create a package controllers under the existing package vn.finixasia.springframework.spring5webapp.

SpringMVC Structure

we add the controller’s package

Inside the new package, we create a new class BookControler

package vn.finixasia.springframework.spring5webapp.controllers

import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import vn.finixasia.springframework.spring5webapp.repositories.BookRepository

@Controller
class BookController constructor(var bookRepository: BookRepository) {

    @RequestMapping("/books")
    fun books(model: Model): String {
        model.addAttribute("books", bookRepository.findAll())
        return "books"
    }
}

To be a controller the class need to use the class annotation @Controller, then just create a function for the page you wish to render. In this case, we wish to display the list of books and so we create a function books, that needs to have a parameter model, to receive the Model.

Unlike Java, SpringMVC with Kotlin doesn’t know the default path to the controller and so you must use always the annotation @RequestMapping(“/books”).

Thymleaf template

The MVC software design pattern is a method for separating concerns within a software application. In principle, the application logic, or controller, is separated from the technology used to display information to the user, or the view layer. The model is a communications vehicle between the controller and view layers.

Spring web-based applications support a variety of view options, often referred to as view templates. These technologies are described as “templates” because they provide a markup language to expose model attributes within the view during server-side rendering.

The following view template libraries, among others, are compatible with Spring:

  • JSP/JSTL
  • Thymeleaf
  • Tiles
  • Freemarker
  • Velocity

Spring 5 communities don’t recommend anymore the use of JSP/JSTL but only the use of templates.

For the example, we will use Thymeleaf template that comes with a specific adaptation from Spring-5.

So we have already added on the previous post the artifact spring-boot-starter-thymeleaf, we need just add our pages to the folder templates under resources.

For our example, we add a view page: books.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Spring Framework Guru</title>
</head>
<body>
<h1>Product list</h1>

<table>
    <tr>
        <th>ID</th>
        <th>title</th>
        <th>author</th>
    </tr>
    <tr th:each="book : ${books}">
        <td th:text="${book.id}">123</td>
        <td th:text="${book.title}">Lord of the Ring</td>
        <td th:text="${book.isbn}">4456677</td>
        <td th:text="${book.publisher.name}">J.R. Tolkien</td>
    </tr>
</table>
</body>
</html>

The namespace tymeleaf add a series of methods to iterate through the books object we created on our controller BookController. That’s all if now we browse to http://localhost:8080/books, we have the following render:

Screenshot 2017-12-27 11.47.52

Note: All the model need to have default values, if you don’t set a default value, you will not be able to render anything. I fixed the previous post according to this issue I discover while writing this post.

Leave a Reply

Translate »