spring-boot入門(一)——使用idea初始化一個項目






maven下載地址

  • 自動導入maven



  • 新建一個loginController,RequestMapping是紅色的鼠標點擊alt+回車
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
public class loginController {
    @RequestMapping("/")
    public String loginPage(Map<String, Object> map) {
        //通過 map 向前臺頁面傳遞數據
        map.put("msg", "hello world");
        return "login";
    }
}

resources->templates文件夾下新建一個login.html

<!DOCTYPE html>
<!--導入thymeleaf的名稱空間-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--th:text 爲 Thymeleaf 屬性,用於獲取指定屬性的值-->
<h1 th:text="${msg}"></h1>
</body>
</html>
  • 在pow.xml中添加依賴thymeleaf
<!--Thymeleaf 啓動器-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

啓動打開http://localhost:8080/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章