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!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)2. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息3. jsp網(wǎng)頁實(shí)現(xiàn)貪吃蛇小游戲4. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向5. CSS hack用法案例詳解6. asp中response.write("中文")或者js中文亂碼問題7. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法8. PHP設(shè)計(jì)模式中工廠模式深入詳解9. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明10. ASP實(shí)現(xiàn)加法驗(yàn)證碼
