springboot使用freemarker模板引擎
首先引入freemarker
<!-- freemarker依賴包 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
?
編寫ftl文件,注意默認放在resource/templates/下,也可以在application.yml重新定義
hello freemarker!${name} <#if sex="0">男 <#else>女 </#if>
bootstrap5模板,?
spring:freemarker:cache: falsetemplate-loader-path: classpath:/templates/suffix: .ftl
?
編寫控制類
package com.tzp.freemarker.controller;import java.util.Map;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class FreeMarkerController {@RequestMapping("/ftlindex")public String index(Map<String, Object> result) {result.put("sex", "0");result.put("name", "xxx");return "index";}}
template模板引擎,?
測試效果
?
?