06 Spring Boot Spring MVC
06 Spring Boot Spring MVC
© luv2code LLC
What is Thymeleaf?
• Thymeleaf is a Java templating engine www.thymeleaf.org
Separate project
Unrelated to spring.io
• Commonly used to generate the HTML views for web apps
• However, it is a general purpose templating engine
Output
fi
2. Develop Spring MVC Controller
3. Create Thymeleaf template
www.luv2code.com © luv2code LLC
Step 1: Add Thymeleaf to Maven pom file
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Based on this,
@Controller
@GetMapping("/")
return "helloworld";
src/main/resources/templates/helloworld.html
fi
• src/main/resources/templates
• For web apps, Thymeleaf templates have a .html extension
expressions
<html xmlns:th="http://www.thymeleaf.org"> }
<head> … </head> }
<body>
</body>
</html>
2
Thymeleaf
expression
<html xmlns:th="http://www.thymeleaf.org"> }
<head> … </head> }
<body>
</body>
</html>
2
Thymeleaf
expression
www.thymeleaf.org
© luv2code LLC
Let's Apply CSS Styles to our Page
Before After
Time on the server is Sat Jan 05 11:42:13 EST 2019 Time on the server is Sat Jan 05 11:42:13 EST 2019
font-style: italic;
color: green;
fi
• We'll cover both options in this video
fi
2. Reference CSS in Thymeleaf template
3. Apply CSS style
custom sub-directorie
static/css
static/images
static/js
etc
src/main/resources
static
File: demo.css
css
.funny {
color: green;
}
Can be any sub-directory nam
e
</head>
src/main/resources
static
css
demo.css
</head>
<body>
</body>
/src/main/resources
1. /META-INF/resources
2. /resources
Search order: top-down
3. /static
4. /public
fi
<head>
… …
</head>
<head>
… …
<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" />
… …
</head>