[Spring] Thymeleaf with Spring Boot
What is Thymeleaf?
- Thymeleaf is Java templating engine
- Commonly used to generate the HTML views for web apps
- However, it is a general purpose templating engine
- Can use Thymeleaf outside of web apps (more on this later)
- You can create Java apps with Thymeleaf
- No need for Spring
- But there is a lof od synergy between the two projects
- Can be an HTML page with some Thymeleaf expressions
- Include dynamic content from Thymeleaf expressions
Can access Java code, objects Spring beans
Where is the Thymeleaf template porcessed?
- In a web app, Thymeleaf is processed on the server
- Results included in HTML returned to browser
Development Process
- Add Thymeleaf to Maven POM file
- Develop Spring MVC Controller
- Create Thymeleaf template
Step 1: Add Thymeleaf to Maven pom file
Based on this, Spring Boot will auto configures to use Thymeleaf templates
Step 2: Develop Spring MVC Contoller
Where to place Thymeleaf template?
- In Spring Boot, your Thymeleaf template files go in
- src/main/resources/templates
- For web apps, Thymeleaf templates have a .html extension
Step 3: Create Thymeleaf template
Additional Features
- Looping and conditionals
- CSS and JavaScript integration
- Template layouts and fragments
Using CSS with Thymleaf Templates
- You have the option of using
- Local CSS files as part of your project
- Referencing remote CSS files
- We’ll cover both options in this video
Development Process
- Create CSS file
- Reference CSS in Thymeleaf template
- Apply CSS style
Step 1: Create CSS file
- Spring Boot will look for static resources in the directory
- src/main/resources/static
Step 2: Reference CSS in Thymeleaf template
Step 3: Apply CSS
Other search directories
- Spring Boot will search following directories for static resources:
/src/main/resources
3rd Party CSS Libraries - Bootstrap
- Local Installation
- Download Bootstrap file(s) and add to /static/css directory
- Remote files
댓글남기기