mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 06:03:52 +08:00
完善案例
This commit is contained in:
@@ -3,10 +3,8 @@ package org.spring.springboot.controller;
|
|||||||
import org.spring.springboot.domain.City;
|
import org.spring.springboot.domain.City;
|
||||||
import org.spring.springboot.service.CityService;
|
import org.spring.springboot.service.CityService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.ui.ModelMap;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -22,9 +20,16 @@ public class CityController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private CityService cityService;
|
private CityService cityService;
|
||||||
|
|
||||||
@RequestMapping(value = "/api/city")
|
@RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
|
||||||
public String findOneCity(Model model, @RequestParam(value="id", required=false, defaultValue="1") Long id) {
|
public String findOneCity(Model model, @PathVariable("id") Long id) {
|
||||||
model.addAttribute("city", cityService.findCityById(id));
|
model.addAttribute("city", cityService.findCityById(id));
|
||||||
return "city";
|
return "city";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/api/city", method = RequestMethod.GET)
|
||||||
|
public String findAllCity(Model model) {
|
||||||
|
List<City> cityList = cityService.findAllCity();
|
||||||
|
model.addAttribute("cityList",cityList);
|
||||||
|
return "cityList";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
15
springboot-freemarker/src/main/resources/web/cityList.ftl
Normal file
15
springboot-freemarker/src/main/resources/web/cityList.ftl
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<#list cityList as city>
|
||||||
|
|
||||||
|
City: ${city.cityName}! <br>
|
||||||
|
Q:Why I like? <br>
|
||||||
|
A:${city.description}!
|
||||||
|
|
||||||
|
</#list>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user