Spring Boot View
View
In Spring Boot, we can use thymeleaf.
thymeleaf is template like jsp.
Prepare pom.xml
This is an example of maven
<!-- Thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
Add thymeleaf to pom fiel and Run maven install, we can use thymeleaf in our project
Controller
@Controller public class IndexController { @RequestMapping("/index") public String greeting(@RequestParam(value="name", required=false, defaultValue="Umi") String name, Model model) { model.addAttribute("name", name); return "index"; } }
Set default query key and value name=Umi
And set name for view(template)
Finally, return html file name
View(html)
Prepare html file in src/main/resources/templates/
index.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Thymeleaf Memo</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'Hello, ' + ${name} + '!'"/> </body> </html>
${parameter} is parameter from controller side.
Run and Test
Run Spring boot Application and test http://localhost:8080/index?name=Kotori
http://localhost:8080/index