博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud学习笔记5——天气预报系统(4)为天气预报制作 UI
阅读量:3944 次
发布时间:2019-05-24

本文共 4356 字,大约阅读时间需要 14 分钟。

开发环境

  • JDK8+
  • Gradle4+
  • Redis 3.2.100
  • Apache HttpClient 4.5.3
  • Spring Boot Web Starter
  • Spring Boot Data Redis Starter
  • Spring Boot Quartz Starter
  • Quartz Scheduler
  • Spring Boot Thymeleaf Starter 2.0.0.M4
  • Thymeleaf 3.0.7.RELEASE
  • Bootstrap 4.1.3

天气预报服务的功能

  • 按照不同的城市来进行查询
  • 查询近几天的天气信息
  • 界面简洁、优雅

天气预报服务的API

获取到该城市ID的天气预报信息:GET/report/cityId/{cityId}

新建项目

复制之前的micro-weather-quartz项目,将副本改名为micro-weather-report

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

修改源码

修改build.gradle配置,加入thymeleaf的依赖:

//依赖关系dependencies {
//该依赖用于编译阶段 compile('org.springframework.boot:spring-boot-starter-web') //HttpClient compile('org.apache.httpcomponents:httpclient:4.5.6') //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') //该依赖用于测试阶段 testCompile('org.springframework.boot:spring-boot-starter-test')}

com.study.spring.cloud.weather.service包下新建接口WeatherReportService

package com.study.spring.cloud.weather.service;import com.study.spring.cloud.weather.vo.Weather;public interface WeatherReportService {
//根据城市id查询天气信息 Weather getDataByCityId(String cityId);}

com.study.spring.cloud.weather.service包下新建类WeatherReportServiceImpl

package com.study.spring.cloud.weather.service;import com.study.spring.cloud.weather.vo.Weather;import com.study.spring.cloud.weather.vo.WeatherResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class WeatherReportServiceImpl implements WeatherReportService {
@Autowired private WeatherDataService weatherDataService; @Override public Weather getDataByCityId(String cityId) {
WeatherResponse resp=weatherDataService.getDataByCityId(cityId); return resp.getData(); }}

com.study.spring.cloud.weather.controller包下新建类WeatherReportController

package com.study.spring.cloud.weather.controller;import com.study.spring.cloud.weather.service.CityDataService;import com.study.spring.cloud.weather.service.WeatherReportService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;@RestController@RequestMapping("/report")public class WeatherReportController {
@Autowired private CityDataService cityDataService; @Autowired private WeatherReportService weatherReportService; @GetMapping("/cityId/{cityId}") //@PathVariable:标识从路径中获取参数 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); } }

修改application.properties配置:

#热部署静态文件spring.thymeleaf.cache=false

resourcesstatic目录下创建js目录,再在js目录下创建weather目录,在weather目录下新建report.js文件:

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

resourcestemplates目录下创建weather目录,在weather目录下新建前端页面report.html

    
天气预报

天气

城市名称

空气质量指数:

当前温度:

温馨提示:

日期

天气类型

最高温度

最低温度

风向

运行

注意一定要先运行Redis

运行应用:

在这里插入图片描述
运行结果如下:
在这里插入图片描述
访问http://localhost:8080/report/cityId/101020100页面:
在这里插入图片描述
更改选择的城市,页面中的天气信息会随之改变:
在这里插入图片描述

转载地址:http://igiwi.baihongyu.com/

你可能感兴趣的文章
JAVA面试题集
查看>>
Embedded System Interview Questions:
查看>>
The Standalone Programmer:Tips from the trenches
查看>>
优化C代码常用的几招
查看>>
Embedded firmware interview questions
查看>>
一道微软亚洲工程院C语言笔试题的解答
查看>>
别踩static的地雷
查看>>
嵌入式软件测试的十大秘诀
查看>>
宏的几个绝妙用法
查看>>
STL学习小结
查看>>
关于fatal error C1063
查看>>
Image在内存中占用的空间计算
查看>>
常见J2ME系统属性及其作用列表
查看>>
java手机机型对应表
查看>>
JSR规范大全
查看>>
关于各种牌子手机的字体问题
查看>>
J2ME中关于网络程序中的一些总结
查看>>
JSR 规范
查看>>
C,C++经典问题及面试笔试题
查看>>
其他J2ME知识
查看>>