1. 程式人生 > >SpringBoot結合Thymeleaf模板與Bootstrap快速搭建介面

SpringBoot結合Thymeleaf模板與Bootstrap快速搭建介面

前言

本系列文章將簡單的學習SpringCloud微服務相關知識,其實也是因為時間的原因,一直拖到現在,遂打算趁著假期,決定記錄下來。

從天氣預報微服務系統的單體架構——>分散式架構的演變過程中,一步一步,由淺及深的學習SpringCloud微服務的思想與其實現的元件。

本系列文章分為以下幾個章節:

專案原始碼已上傳至Github.

開發環境

  • JDK 1.8
  • IDEA 2017.3
  • Gradle 4
  • HttpClient 4.5.3
  • SpringBoot 2.0.0.RELEASE
    //該依賴用於編譯階段
    compile('org.springframework.boot:spring-boot-starter-web'
)
//熱部署 compile('org.springframework.boot:spring-boot-devtools') //HttpClient compile('org.apache.httpcomponents:httpclient:4.5.3') //Redis compile('org.springframework.boot:spring-boot-starter-data-redis') //Quartz compile('org.springframework.boot:spring-boot-starter-quartz'
)
//Spring Boot Thymeleaf Starter compile('org.springframework.boot:spring-boot-starter-thymeleaf')

開發過程

為了給專案一個“面子”,我們選擇與SpringBoot整合的標籤模板Thymeleaf和前端框架Bootstrap。

Thymeleaf的使用呢,其實和以前我們學過的EL表示式十分相似。通過對後臺的值進行解析並繫結到相對的標籤上。

後臺請求控制器

@RestController
@RequestMapping("/report")
public class
WeatherReportController {
@Autowired private CityDataService cityDataService; @Autowired private WeatherReportService weatherReportService; @GetMapping("/cityId/{cityId}") public ModelAndView getReportByCityId(@PathVariable("cityId") String cityId, Model model) throws Exception{ model.addAttribute("title","飛翔的天氣預報"); model.addAttribute("cityId",cityId); model.addAttribute("cityList",cityDataService.listCity()); model.addAttribute("report",weatherReportService.getDataByCityId(cityId)); return new ModelAndView("weather/report","reportModel",model); } }

通過天氣預報API查詢到的資料,儲存到model檢視中,返回給前端。前端如何解析呢?

report.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <title>飛翔的天氣預報(ininan.fun)</title>
</head>
<body>
    <div class="container">
        <div class="row">
            <h3 th:text="${reportModel.title}">ininan.fun</h3>
            <select class="custom-select" id="selectCityId">
                <option th:each="city : ${reportModel.cityList}"
                        th:value="${city.cityId}" th:text="${city.cityName}"
                        th:selected="${city.cityId eq reportModel.cityId}"></option>
            </select>
        </div>
        <div class="row">
            <h1 class="text-success" th:text="${reportModel.report.city}">城市名稱</h1>
        </div>
        <div class="row">
            <p>
                當前溫度:
                <span th:text="${reportModel.report.wendu}"></span>
            </p>
        </div>
        <div class="row">
            <p>
                空氣質量指數:
                <span th:text="${reportModel.report.aqi}"></span>
            </p>
        </div>
        <div class="row">
            <p>
                溫馨提示:
                <span th:text="${reportModel.report.ganmao}"></span>
            </p>
        </div>
        <div class="row">
            <div class="card border-info" th:each="forecast : ${reportModel.report.forecast}">
                <div class="card-body text-info">
                    <p class="card-text" th:text="${forecast.date}">
                        日期
                    </p>
                    <p class="card-text" th:text="${forecast.type}">
                        天氣型別
                    </p>
                    <p class="card-text" th:text="${forecast.high}">
                        最高溫度
                    </p>
                    <p class="card-text" th:text="${forecast.low}">
                        最低溫度
                    </p>
                    <p class="card-text" th:text="${forecast.fengxiang}">
                        風向
                    </p>
                </div>
            </div>
        </div>
    </div>
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdn.bootcss.com/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <!-- Optional JavaScript -->
    <script type="text/javascript" th:src="@{/js/weather/report.js}"></script>

</body>
</html>

report.js

實現下拉框的效果

//report頁面下拉框事件
$(function () {
    $("#selectCityId").change(function () {
        var cityId = $("#selectCityId").val();
        var url = "/report/cityId/" + cityId;
        window.location.href = url;
    })
});

效果展示