mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +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.service.CityService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@@ -22,9 +20,16 @@ public class CityController {
|
||||
@Autowired
|
||||
private CityService cityService;
|
||||
|
||||
@RequestMapping(value = "/api/city")
|
||||
public String findOneCity(Model model, @RequestParam(value="id", required=false, defaultValue="1") Long id) {
|
||||
@RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
|
||||
public String findOneCity(Model model, @PathVariable("id") Long id) {
|
||||
model.addAttribute("city", cityService.findCityById(id));
|
||||
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