springboot 集成 freemarker


  前面我們已經實現了thymeleaf模板,其實freemarker和thymeleaf差不多,都可以取代JSP頁面,實現步驟也差不多,我們來簡單實現一下

引入pom.xml依賴如下

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

  

創建Controller測試類

/**
 * @author pillarzhang
 * @date 2019-06-03
 */
@Controller
class FreemarkerController {
    @RequestMapping("/index")
    public String index(Model model){
        model.addAttribute("name","hello pillar");
        return "index";
    }
}

  

application.properties配置文件你可以選擇不配置默認,也可以進行手動配置

選擇默認時配置路徑一定要寫對,src/main/resources  static(js,css等靜態文件),templates(頁面路徑)注意是ftl后綴

如果要自定義的話,可以在application.properties中設置如下等配置信息

spring.freemarker.charset=UTF-8
spring.freemarker.suffix=.ftl
spring.freemarker.content-type=text/html; charset=utf-8
spring.freemarker.template-loader-path=classpath:/templates
spring.mvc.static-path-pattern=/static/**

  

Index.ftl文件如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>FreeMarker</title>
</head>
<body>
<h1>hello world</h1>
<h1 style="color: red">${name}</h1>
</body>
</html>

  

啟動項目,輸入地址http://localhost:8080/index顯示如下則成功

 

  如果遇到問題,可以結合集成thymeleaf出現的錯誤進行排查

 


免責聲明!

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



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