Spring Boot and kotlin use the Thymeleaf template engine to render the web view

  • 2020-12-22 17:38:57
  • OfStack

This article shows you how Spring Boot and kotlin use the Thymeleaf template engine to render web views.

Static resource access

When we develop Web application, we need to refer to a large number of js, css, images and other static resources. How to use Spring Boot and kotlin to support these static resources? It's easy.

The default configuration

Spring Boot default static resource directory location should be placed under classpath and the directory name should conform to the following rules:


/static
/public
/resources
/META-INF/resources

For example: We can create static in the src/main/resources/ directory and place 1 image file in that location. Start the program, try to access http: / / localhost: 8080 / ruby jpg. If the image can be displayed, the configuration is successful.

Render the Web page

Previously, the request was processed through @RestController and the content returned was an json object. If you want to render html pages, how do you do that?

A template engine

With the template engine recommended by Spring Boot, we can quickly get started in developing dynamic websites.

Spring Boot provides the default configuration of the template engine, there are mainly the following:


Thymeleaf
FreeMarker
Groovy
Mustache

Spring Boot recommends using these template engines instead of JSP. If you want to use JSP 1, you will not be able to implement many of the features of Spring Boot, as shown below: JSP configuration is supported

When you use any of the above template engine 1, their default template configuration path for: src/main/resources/templates. Of course, you can also modify this path, and how to modify it can be found in the configuration properties of subsequent template engines.

Thymeleaf

Thymeleaf is an XML/XHTML/HTML5 template engine for Web and non-ES78en applications. It is an open source Java library, based on the Apache License 2.0 license, created by Daniel Fernandez, the author of Java encryption library Jasypt.

Thymeleaf provides an optional module for integrating Spring MVC. In application development, you can use Thymeleaf to completely replace JSP or other template engines such as FreeMarker. The primary goal of Thymeleaf is to provide a well-formed way of creating templates that can be displayed correctly in the browser and can therefore be used for static modeling as well. You can use it to create validated XML and HTML templates. Instead of writing logic or code, developers simply add tag attributes to the template. Next, these tag attributes perform the predefined logic on DOM (Document Object model).

Sample template:


<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
 <meta charset="UTF-8" />
 <title>quanke.name</title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>

It can be seen that Thymeleaf is mainly added to the html tag in the form of attributes. When the browser parses html, it will ignore the attributes that are not found. Therefore, the template of Thymeleaf can be displayed directly through the browser, which is very conducive to the separation of the front end and the back end.

Used in Spring Boot Thymeleaf, only need to introduce the following dependencies, and in the default template path src/main/resources/templates writing template file.


compile "org.springframework.boot:spring-boot-starter-thymeleaf:$spring_boot_version"

After the configuration is complete, take a simple example, based on the Quick Start project, to render a page through Thymeleaf.


import org.springframework.stereotype.Controller
import org.springframework.ui.ModelMap
import org.springframework.web.bind.annotation.RequestMapping
/**
 * Created by http://quanke.name on 2018/1/10.
 */
@Controller
class HelloController {
 @RequestMapping("/")
 fun index(map: ModelMap): String {
// /  join 1 Three properties that are used to read in the template 
 map.addAttribute("host", "http://quanke.name")
 // return Name of the template file, corresponding to src/main/resources/templates/index.html
 return "index"
 }
}

Default in src/main/resources/increased index templates directory html file


<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head lang="en">
 <meta charset="UTF-8" />
 <title>quanke.name</title>
</head>
<body>
<h1 th:text="${host}">Hello World</h1>
</body>
</html>

Add Spring Boot startup method implemented using kotlin language:


import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

/**
 * Created by http://quanke.name on 2018/1/9.
 */
@SpringBootApplication
class Application
fun main(args: Array<String>) {
 SpringApplication.run(Application::class.java, *args)
}

Open html directly above page, the page show Hello World, but after the launcher, visit http: / / localhost: 8080 /, is showed the host Controller value: http: / / quanke name, do not damage HTML itself content data logic separation.

For more Thymeleaf's page syntax, please also visit Thymeleaf's official document query for use.

Default parameter configuration for Thymeleaf

If you need to change the default configuration, just copy the following property to application.yml and change it to the desired value, such as modifying the extension of the template file, modifying the default template path, etc.


# Enable template caching.
spring.thymeleaf.cache=true 
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true 
# Content-Type value.
spring.thymeleaf.content-type=text/html 
# Enable MVC Thymeleaf view resolution.
spring.thymeleaf.enabled=true 
# Template encoding.
spring.thymeleaf.encoding=UTF-8 
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.excluded-view-names= 
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5 
# Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.prefix=classpath:/templates/ 
# Suffix that gets appended to view names when building a URL.
spring.thymeleaf.suffix=.html spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain. spring.thymeleaf.view-names= # Comma-separated list of view names that can be resolved.

Test environment or development environment to avoid unexpected problems 1 general Settings: ES164en. thymeleaf. cache=true

JSP configuration is supported

Spring Boot is not recommended, but if 1 is to be used, refer to this project as scaffolding: JSP support

In general, Kotlin has very good support for Spring Boot. You only need to use spring boot of Java language and translate it into kotlin.

conclusion


Related articles: