thymeleaf完善前端界面 | 從0開始構建SpringCloud微服務(4)

照例附上項目github連接javascript

本項目實現的是將一個簡單的天氣預報系統一步一步改形成一個SpringCloud微服務系統的過程,本節主要講的是經過引入thymeleaf模塊構建項目的UI界面。css



添加依賴

在pom文件中添加thymeleaf的依賴html

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>



天氣預報服務


提供查詢天氣的接口

定義一個天氣預報服務的接口,提供根據根據城市Id查詢信息的功能。前端

@Service
public class WeatherReportServiceImpl implements WeatherReportService{
    @Autowired
    private WeatherDataService weatherDataService;
    
    @Override
    public Weather getDataByCityId(String cityId) {
        WeatherResponse resp=weatherDataService.getDataByCityId(cityId);
        return resp.getData();
    }

}



界面的完善

界面模板內容以下:java

<!DOCTYPE html>
<html>
<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://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"
    integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb"
    crossorigin="anonymous">

<title>豬豬天氣預報</title>
</head>
<body>
    <div class="container">
        <div class="row">
            <h3 th:text="${reportModel.title}">waylau</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.aqi}"></span>
            </p>
        </div>
        <div class="row">
            <p>
                當前溫度:<span th:text="${reportModel.report.wendu}"></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://code.jquery.com/jquery-3.2.1.slim.min.js"
        integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
        crossorigin="anonymous"></script>
    <script
        src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"
        integrity="sha384-vFJXuSJphROIrBnz7yo7oB41mKfc8JzQZiCq4NCceLEaO4IHwicKwpJf9c9IpFgh"
        crossorigin="anonymous"></script>
    <script
        src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"
        integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ"
        crossorigin="anonymous"></script>
    <!-- Optional JavaScript -->
    <script type="text/javascript" th:src="@{/js/weather/report.js}"></script>
</body>
</html>

js文件內容以下:jquery

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



在Controller層中使用SpringMVC裏面的模型以及視圖的概念。
須要將城市列表傳入模型中,做爲下拉框的選擇信息提供給用戶。git

@RestController
@RequestMapping("/report")
public class WeatherReportController {
    //返回天氣列表使用的
    @Autowired
    private CityDataService cityDataService;
    
    //返回天氣信息使用的
    @Autowired
    private WeatherReportService weatherReportService;
    
    @GetMapping("/cityId")
    public ModelAndView getReportByCityId(String cityId,Model model)throws Exception{
        model.addAttribute("title","豬豬天氣預報");
        model.addAttribute("cityId",cityId);
        //返回listget前端,供用戶選擇
        model.addAttribute("cityList",cityDataService.listCity());
        model.addAttribute("report",weatherReportService.getDataByCityId(cityId));
        return new ModelAndView("weather/report", "reportModel", model);
    }
    
}

增長配置:github

spring: 
  thymeleaf:
    cache: false



測試結果

在這裏插入圖片描述

相關文章
相關標籤/搜索