springboot整合freemarker檢視層
阿新 • • 發佈:2018-12-01
一、在pom檔案中引入freemarker需要的依賴:
<!-- 引入freemarker的依賴包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
二、編寫Contoller程式碼:
//在訪問返回檢視時,我們不需要返回json,所以就不使用@RestController,使用@Controller @Controller public class MyFreemarker { @RequestMapping("/freemarkerIndex") public String freemarkerIndex() { return "myfreemarker";//返回的是freemarker檔案的檔名,不需要寫字尾,因為預設字尾名是.ftl } }
三、建立ftl檔案:在src/main/resources下面建立一個templates資料夾,字尾為.ftl
注意:該Controller必須是在@springbootapplication註解能掃到的包裡面
執行之後訪問: