Spring Boot 揭秘與實戰(七) 實用技術篇 - FreeMarker 模板引擎


文章目錄

  1. 1. FreeMaker 代替 JSP 作為頁面渲染
  2. 2. 生成靜態文件
  3. 3. 擴展閱讀
  4. 4. 源代碼

Spring Boot 提供了很多模板引擎的支持,例如 FreeMarker、Thymeleaf。這篇,我們看下 Spring Boot 如何集成和使用 FreeMarker。

Spring Boot 中使用 FreeMarker 模板非常簡單方便。如果想要使用FreeMarker 模板引擎,首先,修改 POM 文件,添加依賴。

FreeMaker 代替 JSP 作為頁面渲染

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-freemarker</artifactId>
  4. </dependency>

然后,我們創建模板。值得注意的是,Spring Boot 集成的 FreeMarker 默認的配置文件放在 classpath:/templates/。因此,我們需要在 src/main/resources/templates/ 添加模板文件。

例如,我們添加一個模板文件,叫做 welcome.ftl。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <body>
  4. Date: ${time?date}<br>
  5. Message: ${message}
  6. </body>
  7. </html>

那么,最后一步,我們在控制類中只需要這么配置就可以了。

  1. @Controller("template.freemarkerController")
  2. public class WelcomeController {
  3. @RequestMapping("/template/freemarker/welcome")
  4. public String welcome(Map<String, Object> model) {
  5. model.put("time", new Date());
  6. model.put("message", "梁桂釗");
  7. return "welcome";
  8. }
  9. }

還記得我們之前的 WebMain 么,我們來回顧下。

  1. @RestController
  2. @EnableAutoConfiguration
  3. @ComponentScan(basePackages = { "com.lianggzone.springboot" })
  4. public class WebMain {
  5. public static void main(String[] args) throws Exception {
  6. SpringApplication.run(WebMain.class, args);
  7. }
  8. }

直接運行 WebMain 類,或者可以通過“mvn spring-boot:run”在命令行啟動該應用。會啟動一個內嵌的 Tomcat 服務器運行在 8080 端口。訪問 “http://localhost:8080/template/freemarker/welcome” 可以看到頁面上顯示結果。

生成靜態文件

上面的場景,是非常典型的 MVC 的使用場景,我們通過 FreeMaker 代替 JSP 作為頁面渲染。但是,隨着,前后端分離,JSP 漸漸遠離我們的視野,服務端更多地處理業務邏輯,通過 RESTful 或者 RPC 對外提供服務。頁面的交互,交給前端做渲染。

這種情況下,是不是 FreeMarker 就沒有用武之地了呢?實際上,FreeMarker 作為模板引擎,還有很多使用場景,例如,我們可以把我們可以動靜分離,把相對不會變化的內容通過 FreeMarker 渲染生成靜態文件上傳到內容服務,內容服務通過 CDN 進行資源分發。

那么,我們對上面的代碼進行一個小改造,模擬一個文件生成到本地的場景。

  1. @RestController("template.freemarkerController2")
  2. @EnableAutoConfiguration
  3. public class Welcome2Controller {
  4. @Autowired
  5. private Configuration configuration;
  6. @RequestMapping("/template/freemarker/welcome2")
  7. public String welcome2(Map<string, object=""> model) throws Exception {
  8. <string, object=""> model.put("time", new Date());
  9. <string, object=""> model.put("message", "梁桂釗");
  10. <string, object="">
  11. <string, object=""> Template template = configuration.getTemplate("welcome.ftl");
  12. <string, object=""> String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
  13. <string, object="">
  14. <string, object=""> FileUtils.writeStringToFile(new File("d:/welcome.html"), content);
  15. <string, object="">
  16. <string, object=""> return "welcome";
  17. <string, object=""> }
  18. <string, object="">}

直接運行 WebMain 類,訪問 “http://localhost:8080/template/freemarker/welcome2” 可以看到頁面上顯示結果,並查看D盤,是否生成文件了呢?

擴展閱讀

源代碼

相關示例完整代碼: springboot-action
靜態頁面生成器: freemarker-utils

(完)

 

微信公眾號


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM