This post shows a sample Spring MVC
application developed with Spring Boot
, Kotlin
, and Thymeleaf
. Generally speaking, it is now possible and safe to port your Java
-based Spring MVC
application codes to Kotlin
or start writing new Spring MVC
applications in Kotlin
already.
Requirements
This section lists the Software used for this post.
- Windows 10 Enterprise Edition 64bit
- JDK 8
- IntelliJ IDEA Ultimate 2016.3
- Spring Boot 1.5.4.RELEASE
- Kotlin Version 1.1
The Spring MVC Application
The sample application is quite simple but has the following URLs will associated functionalities.
[wp_ad_camp_1]
URL | HTTP METHOD | DESCRIPTION |
---|---|---|
/students | GET | Lists all student records |
/student/{studentId} | GET | Searches for a student record by Id |
/student | POST | Creates a new student record and displays it |
/student | GET | Displays the form where users input the student data and submit the form to create a new student record in the application |
/ | GET | Displays an index page |
Testing the Spring MVC Application
Showing the Index page
Listing all existing student records
Displaying specific student record
[wp_ad_camp_2]
Displaying the New Student Form/Page
On click the Submit
button, the user is redirected to page shown below.
Creating The Spring MVC Application
Okay, we’ve gone through the fairly simple UI of our sample application. Now, let’s see how is was created step-by-step.
1. File -> New -> Project…
Choose Spring Initialzr
and click Next
.
2. Provide project details
Choose Kotlin
and click Next
.
3. Choose Spring Boot components
Choose Web
and Thymeleaf
. Click Next
to proceed.
4. Provide additional information
Click Finish
to complete the process.
Once done. We’ll have the following initial project files and structure.
Coding, coding, coding
The application has the following files added to it.
[wp_ad_camp_3]
The Controllers and Pages
There are 2 controllers and 4 HTML files.
The Index Controller
This displays the index page index.html
Controller codes
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.turreta.sb.smvc.kotlin.controllers import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.RequestMapping @Controller class IndexController { @RequestMapping("/") fun index(): String { return "index" } } |
The Page
1 2 3 4 5 6 7 8 9 10 11 | <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head lang="en"> <title>Spring with Spring Boot, Spring MVC, and Kotlin</title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> </head> <body> <h1>Hello World!</h1> <h2>This is my index page</h2> </body> </html> |
The Student Controller
This controller displays various pages depending on URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package com.turreta.sb.smvc.kotlin.controllers import com.turreta.sb.smvc.kotlin.domain.Student import com.turreta.sb.smvc.kotlin.forms.CreateStudentForm import com.turreta.sb.smvc.kotlin.services.StudentService import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Controller import org.springframework.ui.Model import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMethod /** * Created by Turreta.com on 16/6/2017. */ @Controller class StudentController { @Autowired lateinit var studentService: StudentService @RequestMapping("/students") fun listStudents(model: Model) : String { model.addAttribute("students", studentService.getStudents()) return "students" } @RequestMapping("/student/{studentId}") fun findStudent(@PathVariable("studentId") sId: String, model: Model) : String { model.addAttribute("student", studentService.findStudentById(sId)) return "student" } @RequestMapping(value="/student", method = arrayOf(RequestMethod.POST)) fun addStudent(createStudentForm: CreateStudentForm, model: Model) : String { studentService.createStudent(Student( studentId = createStudentForm.studentId!!, lastName = createStudentForm.lastName!!, firstName = createStudentForm.firstName!!)) return "redirect:/student/" + createStudentForm.studentId } @RequestMapping(value="/student") fun createStudentPage(model: Model) : String { model.addAttribute("studentForm", CreateStudentForm()) return "new-student-form" } } |
The Domain and Service
The class com.turreta.sb.smvc.kotlin.domain.Student
may represent a JPA
entity or data coming from some data source.
[wp_ad_camp_4]
1 2 3 4 5 6 7 8 | package com.turreta.sb.smvc.kotlin.domain /** * This may represent a JPA Entity. * * Created by Turreta.com on 16/6/2017. */ class Student(val studentId: String, val lastName: String, val firstName: String) |
The class com.turreta.sb.smvc.kotlin.services.StudentServiceImpl
provides codes to retrieve, and add Student
records.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.turreta.sb.smvc.kotlin.services import com.turreta.sb.smvc.kotlin.domain.Student import org.springframework.stereotype.Service /** * Created by Turreta.com on 16/6/2017. */ @Service class StudentServiceImpl: StudentService { // This represents your database or any data source val studentList: MutableList<Student> = mutableListOf( Student(studentId = "1", lastName = "Doe", firstName = "John"), Student(studentId = "2", lastName = "Dean", firstName = "James")) override fun getStudents(): List<Student> { return studentList } override fun findStudentById(id: String): Student? { return studentList.find { s -> s.studentId.equals(id) } } override fun createStudent(student: Student) { studentList.add(student) } } |
Down the codes
https://github.com/Turreta/Spring-MVC-with-Spring-Boot-Kotlin-and-Thymeleaf
[wp_ad_camp_5]
References
- https://kotlinlang.org/docs/reference/
- https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/