1. 程式人生 > 其它 >SpringBoot快速入門

SpringBoot快速入門

SpringBoot 是一個快速開發的框架, 封裝了Maven常用依賴、能夠快速的整合第三方框架;簡化XML配置,全部採用註解形式,內建Tomcat、Jetty、Undertow,幫助開發者能夠實現快速開發。

快速建立

整合Thymeleaf

  • 1、引入依賴
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
  • 2、配置
###ThymeLeaf配置
spring:
  thymeleaf:
    #prefix:指定模板所在的目錄
    prefix: classpath:/templates/
    #check-tempate-location: 檢查模板路徑是否存在
    check-template-location: true
    #cache: 是否快取,開發模式下設定為false,避免改了模板還要重啟伺服器,線上設定為true,可以提高效能。
    cache: true
    suffix:  .html
    encoding: UTF-8
    mode: HTML5
  • 3、程式碼編寫
@Controller
public class MyThymeleaf {

    @RequestMapping("/thymeleafTest")
    public String index(Model model){
        User user = new User("snow",24);
        model.addAttribute("user",user);
        return "index";
    }
}
  • 4、頁面編寫
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    姓名:<span th:text="${user.username}"></span><br/>
    年齡:<span th:text="${user.age}"></span>
</body>
</html>