1.首先要理解@RestController与@Controller区别
@RestController返回字符串
@Controller返回页面
返回字符串用RestController
返回页面用Controller
原因:
@RestController注解相当于@ResponseBody和@Controller的结合,用于springboot返回字符串
@Controller Spring MVC中用 返回界面
2. 确认控制类是@RestController
3. 确认添加依赖
pom.xml中添加如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
4. 确认配置完毕
application.yml配置如下
thymeleaf:
prefix: classpath:/templates/
5.控制类
@Controller
public class PersonController {
@RequestMapping("/query")
public String Query(){
return "query";
}
6.在templates创建页面
补充方法通过ModelAndView
@Controller
public class PersonController {
@RequestMapping("/list")
@ResponseBody
public ModelAndView list(){
ModelAndView mav=new ModelAndView();
mav.addObject("bookList", bookDao.findAll());
mav.setViewName("bookList");
return mav;
}
返回是html
spring boot中返回JSP或ftl详见下篇