Software Development, Spring Boot

Spring MVC with Spring Boot, Kotlin, and Thymeleaf

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.

  1. Windows 10 Enterprise Edition 64bit
  2. JDK 8
  3. IntelliJ IDEA Ultimate 2016.3
  4. Spring Boot 1.5.4.RELEASE
  5. 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]

URLHTTP METHODDESCRIPTION
/studentsGETLists all student records
/student/{studentId}GETSearches for a student record by Id
/studentPOSTCreates a new student record and displays it
/studentGETDisplays the form where users input the student data and submit the form to create a new student record in the application
/GETDisplays 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

The Page

The Student Controller

This controller displays various pages depending on URL.

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]

The class com.turreta.sb.smvc.kotlin.services.StudentServiceImpl provides codes to retrieve, and add Student records.

Down the codes

https://github.com/Turreta/Spring-MVC-with-Spring-Boot-Kotlin-and-Thymeleaf

[wp_ad_camp_5]

References

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like