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)類 IndexController為控制器,里面含有一個(gè)index請(qǐng)求處理方法,它返回index字符串,表示渲染模板文件index.ftlh。 index.ftlh為freemarker模板文件
Applciation.class
@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
IndexController.class
@Controllerpublic 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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程2. Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼3. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式4. 一個(gè) 2 年 Android 開發(fā)者的 18 條忠告5. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼6. Spring的異常重試框架Spring Retry簡單配置操作7. Android studio 解決logcat無過濾工具欄的操作8. 什么是Python變量作用域9. PHP正則表達(dá)式函數(shù)preg_replace用法實(shí)例分析10. vue-drag-chart 拖動(dòng)/縮放圖表組件的實(shí)例代碼
