在pom.xml文件中引入开发须要的依赖,上一篇是在工具上选择的,也能够手动编辑xml引入html
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
这样就能够使用 @RestController和@Controller 控制器注解,分别对应返回数据与页面的控制器,也可经过@ResponseBody注解 与 返回ModelAndView 组合使用web
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DataController { @RequestMapping("/getByName") public String getByName(String name){ return "hi ,welcome to " + name; } }
运行项目,访问 localhost:8080/getByName?name=zy,带上name参数,可看到页面返回的数据,能够返回对象,List等,框架会自动转换为Json对象
spring
@Controller public class IndexController { //返回一个String的返回值,框架会自动在 templates目录下找到与返回值对应的html,后由Thymeleaf渲染出来 @RequestMapping("/index") public String index(){ return "index"; } }
<!--index.html内容以下--> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>SpringBoot项目开发系列</title> </head> <body> <div style="padding-top: 20px;text-align: center">首页</div> <div style="padding-top: 20px;text-align: center">SpringBoot项目开发系列</div> </body> </html>
运行项目,访问 localhost:8080/index ,可看到页面的内容
app
这是新建完项目的第一次配置工做,想一想以前的SpringMvc项目,新建完项目每次都要配置web.xml,建立springcontext.xml等等,大量重复的工做,SpringBoot把这些工做大大简化了,让开发人员只须要快速开发业务就好框架
SpringBoot内置了Tomcat,因此新建完项目能够直接运行svg
下一篇 Thymeleaf 模版引擎spring-boot