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方法。
然后訪問(wèn)localhost:8080/index,結(jié)果展示為:
hello Alice!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP.NET MVC實(shí)現(xiàn)城市或車型三級(jí)聯(lián)動(dòng)2. IntelliJ IDEA設(shè)置條件斷點(diǎn)的方法步驟3. php獲取客戶端IP地址的幾種方法4. 淺談Java HttpURLConnection請(qǐng)求方式5. PHP采集程序開發(fā)視頻教程6. Eclipse XSD 生成枚舉類型的Schema的實(shí)例詳解7. PHP數(shù)組array類常見操作示例8. 永久解決 Intellij idea 報(bào)錯(cuò):Error :java 不支持發(fā)行版本5的問(wèn)題9. vue封裝自定義指令之動(dòng)態(tài)顯示title操作(溢出顯示,不溢出不顯示)10. 如何成為一個(gè)偉大的 JavaScript 程序員
