spring boot假如要访问html页面,必须要加模板。常用的有thymeleaf,FreeMarker,beetl等。
这里就介绍一下spring boot如何集成thymeleaf。
首先在pom文件里加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
thymeleaf默认的静态html文件的位置在resources下面的templates下,所以要先创建该文件夹然后把html文件放进入,
Controller层返回String类型的字串就会自动去找对应的html,需要注意的是,thymeleaf会自动在String的字串前加classpath:/templates/,所以你的String不用加上classpath:/templates/前缀。
@RequestMapping("/ok")
@Controller
public class IController {
@RequestMapping("/1")
public String index(){
return "index";
}
}
在resources/templates/目录下创建index.html。
之后启动spring boot就可以访问http://localhotst:8080/ok/1就能访问到index.html页面了。