创建简单的springboot-web工程
1、引入freemarker的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2. 配置application.properties
- 将application.properties改为application.yml
- 然后在里面写入:(重要)
spring:
freemarker:
suffix: .ftl
在resources的templates中创建index.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello freemarker</title>
</head>
<body>
Hello ${name}!
</body>
</html>
在Controller包中创建FreemarkerController
(注意):
- 要用@Controller ,而不是@RestController
- springboot的启动App要和Controller包同级
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("res")
public class FreemarkerController {
@RequestMapping("/index")
public String index(Model model) {
model.addAttribute("name", "DL");
//返回freemarker模板的位置,基于resources/templates路径
return "index";
}
}