springboot整合freemarker(轉)


  • 添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
  • 編寫配置文件(application.yml)
spring:
freemarker:
cache: false

為了實現熱部署,這里僅配置一下freemarker的緩存,關於freemarker的其它配置使用默認即可。

  • 創建freemarker模板
    在src/java/resources目錄下創建templates文件夾並創建demo.ftl。
    模板默認是從【classpath:/templates/】這個位置查找的。
    demo.ftl文件內容如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
請看說明:${descrip} <br /> haahaaaa </body>
</html>
  • 創建web層輔助類
@Controller
public class FreemarkerController {

@RequestMapping("/demo")
public String demo(Map<String, Object> map) {
map.put("descrip", "It's a springboot integrate freemarker's demo!!!!");
return "demo";
}
}
  • 創建測試啟動類
@SpringBootApplication
public class Application {

public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}

源代碼鏈接:https://github.com/myNameIssls/springboot-study
參考鏈接:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples
關於springboot配置文件中的屬性及freemarker的配置可參考鏈接:
http://docs.spring.io/spring-boot/docs/1.5.3.RELEASE/reference/htmlsingle/#appendix


免責聲明!

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



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