不要使用@RestController注解,@RestController注解是@ResponseBody和@Controller的集合体,使用@RestController(以及@ResponseBody)注解会默认返回数据,而不会请求到页面。
下面代码要使用到 Thymeleaf 在 list/index.html 这个模板,所以这里只用 @Controller !
@Controller
public class NewsController {...}
1
2
@GetMapping("list/all")
public String listAll(Model model) {
List<News> list = mNewsService.selectAll();
model.addAttribute("newsList", list);
return "list/index";
}
1
2
3
4
5
6
如果有需要直接返回的数据,那么在方法上加@ResponseBody即可。
下面这段代码直接返回 list 数据到页面上,不会用到 Thymeleaf 模板。
@GetMapping("list/all")
@ResponseBody
public List<News> listAll() {
List<News> list = mNewsService.selectAll();
return list;
}