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
@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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python3 json模塊之編碼解碼方法講解2. Linux刪除系統(tǒng)自帶版本Python過程詳解3. Python 制作查詢商品歷史價(jià)格的小工具4. Python 合并拼接字符串的方法5. python 使用事件對象asyncio.Event來同步協(xié)程的操作6. ASP基礎(chǔ)知識VBScript基本元素講解7. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條8. Python 利用Entrez庫篩選下載PubMed文獻(xiàn)摘要的示例9. Python sublime安裝及配置過程詳解10. Python字符串到字節(jié)的轉(zhuǎn)換。雙反斜杠問題

網(wǎng)公網(wǎng)安備