springboot支持freemarker,需要在pom.xml文件中添加以下依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
freemarker文件的默认路径是:classpath:/templates/,如下:
测试类如下:
package com.edu.spring.springboot; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class AccountController { @RequestMapping("/reg") public String reg(){ return "/reg"; } }
浏览器输入:http://127.0.0.1:8080/reg即可跳转到 reg.ftl页面
修改freemarker的默认路径,在application.properties文件中添加spring.freemarker.templateLoaderPath属性,可以配置多个,逗号分隔,如下:
spring.freemarker.templateLoaderPath=classpath:/ftl/,classpath:/ftl2/
目录结构如下:
测验类如下:
package com.edu.spring.springboot; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class AccountController { @RequestMapping("/reg") public String reg(){ return "reg"; } @RequestMapping("/logout") public String logout(){ return "logout"; } }
浏览器输入:http://127.0.0.1:8080/reg即可跳转到 reg.ftl页面
浏览器输入:http://127.0.0.1:8080/logout即可跳转到 logout页面