国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Boot如何整合FreeMarker模板引擎

瀏覽:73日期:2023-08-09 18:19:32

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)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 池州市| 巩留县| 六枝特区| 曲麻莱县| 客服| 恩平市| 蚌埠市| 宜城市| 泗水县| 福贡县| 大田县| 怀化市| 张家口市| 寿光市| 德昌县| 腾冲县| 庆城县| 教育| 波密县| 禹州市| 大石桥市| 双辽市| 丘北县| 东城区| 光山县| 喀喇沁旗| 城口县| 棋牌| 饶平县| 华阴市| 安国市| 古丈县| 塔城市| 称多县| 海原县| 西乌珠穆沁旗| 武清区| 南郑县| 万年县| 惠安县| 宝兴县|