No external: as readers continue to put together several new tutorials covering Spring Boot, Spring Cloud, micro-service architecture and other PDF.
Access: public attention to the right number, "Masons BYSocket", to receive it!
Abstract: Original Source https://www.bysocket.com "Public number: masons BYSocket 'attention and welcome to reprint, reserved summary, thank you!
This is the first 105 original mason
Article project:
* the JDK 1.8
* Maven 3.5.2
* the Spring the Boot 2.1.3.RELEASE
* Project name: springboot-webflux-4-Thymeleaf
* Project address: see end of text
Foreword
Last lecture, we use MongoDB to achieve WebFlux operations on the data source. So we have the data needed to render to the front to show users. This is the View layer article concerned. There are many forms View, such as JSON and HTML. Development of common template language is very common to have Thymeleaf, Freemarker and so on. that
What is a template language?
Common language template contains the following concepts: data (Data), the template (Template), template engine (Template Engine) and the resulting document (Result Documents).
- data
And the data is a representation of the information carrier, may be symbols, words, figures, voice, images and video. Data and information are inseparable, the expression data information, the data information is a connotation. Does not make sense data itself, the data only when the impact on the real behavior become information.
- template
Template, a blueprint that has nothing to do with a type of class. When using a template compiler will instantiate a template argument according to the template, to obtain a type-specific classes.
- Template engine
Template engine (here especially for Web development template engine) is to make the user interface and business data (content) resulting from the separation, it can generate a document in a specific format, for a website template engine will generate a standard HTML document.
- The results document
A particular document format, such as the site for the template engine will generate a standard HTML document.
Template Language wide range of uses, common uses are as follows:
- Page rendering
- Document Generation
- Code Generation
- All "Data + text template =" application scenarios
Spring Boot template language is recommended Thymeleaf, that
What is Thymeleaf?
The official explanation is as follows:
Thymeleaf modern template language engine that can run independently to serve the Web. The main goal is to provide a template for the development of natural, and can accurately display inside HTML.
Thymeleaf is a new generation of Java template engine, it is recommended after Spring 4. Spring 5 is now naturally more recommended.
structure
Mentioned above similar engineering structures, preparation of a new project this case. Project Figure:
Contents are as follows
- org.spring.springboot.webflux.controller – Controller 层
- org.spring.springboot.dao - data manipulation layer DAO
- org.spring.springboot.domain - Entity class
- org.spring.springboot.handler - Yewuluojiceng
- Application - application startup class
- application.properties - Application Profiles
- pom.xml maven configuration
- application.properties profile
Templates will use the following two directories
- static directory which contains CSS, JS files and other resources
- templates directory which contains views
This article focuses on writing Controller layer and templates view.
New POM dependence and configuration
The new configuration dependencies in pom.xml:
Here we added Thymeleaf dependent, but not in application.properties - Application Profile Configuration people of any configuration. Default startup its default configuration, the reference Thymeleaf To modify the configuration dependent configuration, as follows:
Including common encoding, whether to open the cache, and so on.
WebFlux used Thymeleaf
In CityWebFluxController control layer, add the following two methods:
Explain the following syntax:
- Return value String Mono or will do, but Mono represents my return View also callback.
- return string corresponding template directory name in the resources / templates of.
- Model data objects to bind to the view
- Usually with a centralized view of the path of constant Administrative Templates
Tymeleaf view
And then write two views hello cityList, codes are as follows:
hello.html:
cityList.html:
Common syntactic sugar as follows
- $ {...} variable expression
- th: text processing Tymeleaf expression
- th: each traversal expression, may traverse objects: to achieve java.util.Iterable, java.util.Map (java.util.Map.Entry taken when traversing), and the like Array
There are many use refer to the official party document http://www.thymeleaf.org/documentation.html
Run the project
The operation of the project under verification. IDEA using the right side of the toolbar, click the Maven Project Tab, click the Maven plugin using the following install
command. Or use the command line of the form, in the project root directory, execute instructions Maven clean-up and installation of:
See the success of the output in the console:
IDEA performed in the Application
class started, the normal mode or any Debug mode. You can see the output of a successful run in the console:
Open the browser and go to http: // localhost: 8080 / city / hello, you can see the response as shown:
Continue to visit http: // localhost: 8080 / city / page / list, we found no value, according to the last lecture insert a few data can have a value, as shown:
to sum up
Here, we discuss how to integrate Spring WebFlux Thymeleaf. Other integrated template language Thymeleaf, Freemarker, on any different. Below, we will be able to integrate Thymeleaf and MongoBD, to achieve an overall simple cases.
The sample code
Readers can view the examples in this article by the following warehouse module project name: 2-x-spring-boot-webflux-handling-errors:
- Github:https://github.com/JeffLi1993/springboot-learning-example
- Gitee:https://gitee.com/jeff1993/springboot-learning-example
If you are interested in these, welcomed the star, follow, bookmarking, forwarding support!
Reference material
- Spring Boot 2.x WebFlux Series: https: //www.bysocket.com/archives/2290
- spring.io official documents
The following tutorial Maybe you will be interested in the topic
- "Art of War Program: Algorithms and Data Structures" https://www.bysocket.com/technique/2314.html
- "Spring Boot 2.x series of tutorials"
https://www.bysocket.com/springboot - "Java core tutorial series"
https://www.bysocket.com/technique/2100.html