Spring Boot (二): 集成 FreeMarker 与 Bootstrap

Spring Boot (一): 快速构建 WebMvc 应用 中,我们展示了 Spring Boot 如何快速构建一个超简单的 WebMvc 应用(永远的 Hello 系列, 233)。接下来,我们需要加入一些更实际的特性,集成 FreeMarker 和 Bootstrap 作为前端显示。

本篇代码以 Spring Boot (一): 快速构建 WebMvc 应用 构建代码为基础。

使用 FreeMarker 作为页面展示

  • build.gradle 中增加 FreeMarker 依赖:
1
...java
  compile("org.springframework.boot:spring-boot-starter-freemarker")
...
  • src/main/resources 中创建 application.properties 文件,内容如下:
1
2
3
4
5
6
7
8
9
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 创建目录 templates, 并在此目录创建 hello.ftl,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>

<html lang="en">

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

</html>
  • 修改 HelloController.java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<#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 href="//cdn.jsdelivr.net/bootstrap/3.3.6/css/bootstrap.min.css" 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="//cdn.jsdelivr.net/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="//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">&copy;2016 fyunli</p>
</div>
</footer>


<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="//cdn.jsdelivr.net/ie10-viewport/1.0.0/ie10-viewport.min.js"></script>
<script src="//cdn.jsdelivr.net/jquery/1.12.1/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
  • 在 src/resources 中创建 static/css 目录,添加 main.css:
main.css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 会从以下路径寻找静态文件:

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

源代码

源代码链接: github