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