spring boot整合freemarker

說明:完整資源在我的GitHub
任務:用瀏覽器訪問服務器,跳轉到登錄頁
效果:
在這裏插入圖片描述

在pom.xml中添加依賴

<!-- 引入freemarker包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

在application.yml添加freemarker配置

spring:
  # freemarker
  freemarker:
    suffix: .ftl
    content-type: text/html
    enabled: true
    # 緩存
    cache: false
    template-loader-path: classpath:/templates/
    charset: UTF-8
    allow-request-override: false
    check-template-location: true
    expose-session-attributes: false
    expose-request-attributes: false
    expose-spring-macro-helpers: false
    settings:
      number_format: 0.##

創建靜態資源static目錄(存放css、js、img等),結構如圖:

在這裏插入圖片描述

創建登錄頁面login.ftl

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

    <title>登錄</title>
    <meta name="keywords" content="登錄頁">
    <meta name="description" content="登錄頁面">
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
    <link href="css/animate.min.css" rel="stylesheet">
    <link href="css/style.min.css" rel="stylesheet">
    <link href="css/login.min.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <meta http-equiv="refresh" content="0;ie.html" />
    <![endif]-->
    <script>
        if(window.top!==window.self){window.top.location=window.location};
    </script>

</head>

<body class="signin">
<div class="signinpanel">
    <div class="row">
        <div class="col-sm-5" style="margin-top: 90px;margin-left: 490px;">
            <form method="post" action="">
                <input type="text" class="form-control uname" placeholder="用戶名" />
                <input type="password" class="form-control pword m-b" placeholder="密碼" />
                <a href="#">忘記密碼了?</a>
                <button class="btn btn-success btn-block">登錄</button>
            </form>
        </div>
    </div>
</div>
</body>


</html>

創建LoginController控制層

package com.itor.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 大都督
 * @create 2020/5/10
 */
@Controller
public class LoginController {

    private static final String LOGIN = "login/login";

    @GetMapping("/login")
    public String login() {
        return LOGIN;
    }
}

注意事項

新添加靜態資源後,一定要進行編譯,否則頁面報錯

靜態資源報錯信息: 404
解決辦法: compile 編譯一下即可
在這裏插入圖片描述

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