This article introduces Spring Boot and kotlin to render web views using the Thymeleaf template engine.
Static resource access
When we develop web applications, we need to reference a large number of static resources such as js, css, and images. How to support these static resources using Spring Boot and kotlin? , very simple.
Default configuration
Spring Boot provides static resource directory locations by default and must be placed under classpath, and the directory name must comply with the following rules:
/static/public/resources/META-INF/resources
For example: We can create static in the src/main/resources/ directory and place an image file at that location. After starting the program, try to access http://localhost:8080/ruby.jpg. If the picture can be displayed, the configuration will be successful.
Rendering a web page
Previously, the request was processed through @RestController, and the returned content was a json object. If you need to render an html page, how to implement it?
Template Engine
Under the template engine recommended by Spring Boot, we can quickly get started with developing dynamic websites.
Spring Boot provides default configuration template engines with the following main types:
ThymeleafFreeMarkerGroovyMustache
Spring Boot recommends using these template engines to avoid using JSP. If you have to use JSP, you will not be able to implement various features of Spring Boot. For details, you can see the following text: Supporting JSP configuration.
When you use any of the template engines above, their default template configuration path is: src/main/resources/templates . Of course, this path can also be modified. For details, you can query and modify it in the configuration properties of subsequent template engines.
Thymeleaf
Thymeleaf is an XML/XHTML/HTML5 template engine that can be used for application development in both web and non-web environments. It is an open source Java library based on the Apache License 2.0 license and was created by Daniel Fernández, the author of the 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, etc. The main goal of Thymeleaf is to provide a well-formed template creation method that can be displayed correctly by the browser, and therefore can also be used as static modeling. You can use it to create validated XML and HTML templates. Compared to writing logic or code, developers simply need to add tag attributes to the template. Next, these tag properties execute pre-formed logic on the 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 the html, it will ignore it when it checks that there is no attribute. Therefore, Thymeleaf's template can be directly recorded through the browser, which is very conducive to the separation of the front and back ends.
To use Thymeleaf in Spring Boot, you only need to introduce the following dependencies and write the template file under the default template path src/main/resources/templates to complete it.
compile "org.springframework.boot:spring-boot-starter-thymeleaf:$spring_boot_version"
After completing the configuration, give a simple example, based on the quick start project, give a simple example to render a page through Thymeleaf.
import org.springframework.steretype.Controllerimport org.springframework.ui.ModelMapimport org.springframework.web.bind.annotation.RequestMapping/** * Created by http://quanke.name on 2018/1/10. */@Controllerclass HelloController { @RequestMapping("/") fun index(map: ModelMap): String {// Add an attribute to read map.addAttribute("host", "http://quanke.name") in the template // The name of the return template file, corresponding to src/main/resources/templates/index.html return "index" }}By default, add index.html file in the src/main/resources/templates directory.
<!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 the kotlin language:
import org.springframework.boot.SpringApplicationimport org.springframework.boot.autoconfigure.SpringBootApplication/** * Created by http://quanke.name on 2018/1/9. */@SpringBootApplicationclass Applicationfun main(args: Array<String>) { SpringApplication.run(Application::class.java, *args)}As shown in the above page, you can directly open the html page to display Hello World, but after starting the program, visit http://localhost:8080/, which shows the host value in the Controller: http://quanke.name, which achieves the logical separation of data without destroying the HTML's own content.
For more Thymeleaf's page syntax, please visit Thymeleaf's official documentation for querying.
Thymeleaf default parameter configuration
If you need to modify the default configuration, just copy the attributes you want to modify below into application.yml and modify them to the required 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. General settings: spring.thymeleaf.cache=true
Support JSP configuration
Spring Boot is not recommended, but if you must use it, you can refer to this project as a scaffolding: JSP support
In general, Kotlin supports Spring Boot very well. You only need to use spring boot in Java and translate it into kotlin.
Summarize
The above is the method of using the Thymeleaf template engine to render web views by Spring Boot and kotlin introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!