thymeleaf+springboot找不到html,只返回了字符串
问题描述:
1:项目结构:
2:核心代码
@RestController
@RequestMapping(value = "/city")
public class CityWebFluxController {
@Autowired
private CityHandler cityHandler;
@GetMapping("/hello")
public Mono<String> hello(final Model model) {
model.addAttribute("name", "泥瓦匠");
model.addAttribute("city", "浙江温岭");
String path = "hello";
return Mono.create(monoSink -> monoSink.success(path));
}
private static final String CITY_LIST_PATH_NAME = "cityList";
@GetMapping("/page/list")
public String listPage(final Model model) {
final Flux<City> cityFluxList = cityHandler.findAllCity();
model.addAttribute("cityList", cityFluxList);
return CITY_LIST_PATH_NAME;
}
在浏览器用链接http://localhost:8080/city/page/list 访问时页面上只出现了cityList字样,预期是返回cityList.html
解决:在controller中使用了注解@RestController,修改注解为@Controller.
原因:RestController = Controller + ResponseBody.加上RestController,返回的内容是你return中的内容,如果是return "Hello World",页面显示的就是Hello World。加上Controller,返回的是return中对应的页面,比如return “hello”,页面的名称是hello。