Spring Boot (二): 集成 FreeMarker 與 Bootstrap

在 《 Spring Boot (一): 快速構(gòu)建 WebMvc 應(yīng)用 》中,我們展示了 Spring Boot 如何快速構(gòu)建一個超簡單的 WebMvc 應(yīng)用(永遠(yuǎn)的 Hello 系列, 233)。接下來,我們需要加入一些更實(shí)際的特性,集成 FreeMarker 和 Bootstrap 作為前端顯示。

本篇代碼以 Spring Boot (一): 快速構(gòu)建 WebMvc 應(yīng)用 構(gòu)建代碼為基礎(chǔ)。

使用 FreeMarker 作為頁面展示

  • build.gradle 中增加 FreeMarker 依賴:
...java
  compile("org.springframework.boot:spring-boot-starter-freemarker")
...
  • src/main/resources 中創(chuàng)建 application.properties 文件,內(nèi)容如下:
application.message: Hello, Boot

spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
  • src/main/resources 創(chuàng)建目錄 templates, 并在此目錄創(chuàng)建 hello.ftl,內(nèi)容如下:
<!DOCTYPE html>

<html lang="en">

<body>
Date: ${time?date}
<br>
Time: ${time?time}
<br>
Message: ${message}
</body>

</html>
  • 修改 HelloController.java:
package li.fyunli.springboot.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * Created by fyunli on 16/4/1.
 */
@Controller
public class HelloController {

    @Value("${application.message:Hello World}")
    private String message = "Hello World";

    @RequestMapping("/hello")
    public String welcome(ModelMap model) {
        model.put("time", new Date());
        model.put("message", this.message);
        return "hello";
    }

}

使之前 @ResponseBody 輸出改成 freemarker 頁面輸出,并從 application.properties 取值填充頁面。

freemarker result

Bootstrap 美化頁面

  • 修改 hello.ftl 內(nèi)容如下:
<#assign base = request.contextPath />
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="author" content="fyunli">

    <base id="base" href="${base}">
    <title>Spring Boot - hello</title>

    <!-- Bootstrap core CSS -->
    <link  rel="stylesheet">
    <link href="${base}/css/main.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="http://cdn.jsdelivr.net/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="http://cdn.jsdelivr.net/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

<!-- Begin page content -->
<div class="container">
    <div class="page-header">
        <h1>Sprint Boot: Hello</h1>
    </div>

    <div>
        Date: ${time?date}
        <br>
        Time: ${time?time}
        <br>
        Message: ${message}
    </div>
</div>

<footer class="footer">
    <div class="container">
        <p class="text-muted">?2016 fyunli</p>
    </div>
</footer>


<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="http://cdn.jsdelivr.net/ie10-viewport/1.0.0/ie10-viewport.min.js"></script>
<script src="http://cdn.jsdelivr.net/jquery/1.12.1/jquery.min.js"></script>
<script src="http://cdn.jsdelivr.net/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
  • 在 src/resources 中創(chuàng)建 static/css 目錄,添加 main.css:
html {
    position: relative;
    min-height: 100%;
}
body {
    /* Margin bottom by footer height */
    margin-bottom: 60px;
}
.footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    /* Set the fixed height of the footer here */
    height: 60px;
    background-color: #f5f5f5;
}

.container {
    width: auto;
    max-width: 680px;
    padding: 0 15px;
}
.container .text-muted {
    margin: 20px 0;
}
bootstrap page

注:Spring Boot 會從以下路徑尋找靜態(tài)文件:

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/

源代碼

源代碼鏈接: github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容