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>
項(xiàng)目結(jié)構(gòu)
src/ +- main/ +- java/ | +- com | +- controller/ | | +- IndexController.class | +- Application.class +- resources/ +- templates/ +- index.ftlh
- Application為應(yīng)用程序啟動類
- 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一起使用,所以此處不能標(biāo)注@RestController注解。
index.ftlh
<!DOCTYPE html> <html> <head> <title>test</title> </head> <body> hello ${name}! </body> </html>
運(yùn)行
運(yùn)行Application類里的main方法。
然后訪問localhost:8080/index,結(jié)果展示為:
hello Alice!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ElasticSearch 動態(tài)映射實(shí)戰(zhàn)詳解
這篇文章主要為大家介紹了ElasticSearch 動態(tài)映射實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01關(guān)于@Transactional事務(wù)表被鎖的問題及解決
這篇文章主要介紹了關(guān)于@Transactional事務(wù)表被鎖的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01網(wǎng)關(guān)Spring Cloud Gateway HTTP超時配置問題
這篇文章主要介紹了網(wǎng)關(guān)Spring Cloud Gateway HTTP超時配置問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01scala當(dāng)中的文件操作和網(wǎng)絡(luò)請求的實(shí)現(xiàn)方法
這篇文章主要介紹了scala當(dāng)中的文件操作和網(wǎng)絡(luò)請求的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06springmvc下實(shí)現(xiàn)登錄驗(yàn)證碼功能示例
本篇文章主要介紹了springmvc下實(shí)現(xiàn)登錄驗(yàn)證碼功能示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02Java8中CompletableFuture使用場景與實(shí)現(xiàn)原理
CompletableFuture是java8引入的新類,該類實(shí)現(xiàn)了Future接口和 CompletionStage接口,封裝了future、forkjoin相關(guān)類來執(zhí)行異步,這篇文章主要給大家介紹了關(guān)于Java8中CompletableFuture使用場景與實(shí)現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下2022-02-02簡述IDEA集成Git在實(shí)際項(xiàng)目中的運(yùn)用
這篇文章主要介紹了IDEA集成Git在實(shí)際項(xiàng)目中的運(yùn)用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07