在写这篇博客的时候,我也走过很多springboot整合前端的坑,实在是不想让大家再次进入这样的坑,所有下面就是我的一些见解,有错大家请指正。
首先我们要把 静态的资源文件加载在项目下resource文件夹下,当然springboot中不建议使用jsp作为页面展示。springboot默认是使用的thymeleaf模板引擎的
使用
thymeleaf
在pom.xml中添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
配置thymeleaf
##thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.mvc.static-path-pattern=/static/** spring.thymeleaf.cache=false
这里要声明一下,在springboot中 templates文件下一般是html文件(注:最好名字不要改,因为springboot默认是这个(这里是我没注意看到的,我也忘记怎么表达)),static是一些css,js等东西,一般情况下配置这么多就可以,接着编写controller层代码
@Controller public class HelloController { @GetMapping("/test") public String index(){ return "index"; } }