Spring Boot如何整合FreeMarker模板引擎
更新時間:2020年10月09日 11:11:59 作者:StrongerBrother
這篇文章主要介紹了Spring Boot如何整合FreeMarker模板引擎,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
POM
<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>
項目結構
src/ +- main/ +- java/ | +- com | +- controller/ | | +- IndexController.class | +- Application.class +- resources/ +- templates/ +- index.ftlh
- Application為應用程序啟動類
- IndexController為控制器,里面含有一個index請求處理方法,它返回index字符串,表示渲染模板文件index.ftlh。
- index.ftlh為freemarker模板文件
Applciation.class
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
IndexController.class
@Controller public class IndexController { @GetMapping("/index") public String index(Model model) { model.addAttribute("name", "Alice"); return "index"; } }
注意@ResponseBody注解不能和freemarker一起使用,所以此處不能標注@RestController注解。
index.ftlh
<!DOCTYPE html> <html> <head> <title>test</title> </head> <body> hello ${name}! </body> </html>
運行
運行Application類里的main方法。
然后訪問localhost:8080/index,結果展示為:
hello Alice!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ElasticSearch 動態(tài)映射實戰(zhàn)詳解
這篇文章主要為大家介紹了ElasticSearch 動態(tài)映射實戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01網關Spring Cloud Gateway HTTP超時配置問題
這篇文章主要介紹了網關Spring Cloud Gateway HTTP超時配置問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Java8中CompletableFuture使用場景與實現原理
CompletableFuture是java8引入的新類,該類實現了Future接口和 CompletionStage接口,封裝了future、forkjoin相關類來執(zhí)行異步,這篇文章主要給大家介紹了關于Java8中CompletableFuture使用場景與實現原理的相關資料,需要的朋友可以參考下2022-02-02