久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Spring Boot如何整合FreeMarker模板引擎

瀏覽:57日期: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>

項目結構

src/ +- main/ +- java/ | +- com | +- controller/ | | +- IndexController.class | +- Application.class +- resources/ +- templates/ +- index.ftlh Application為應用程序啟動類 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一起使用,所以此處不能標注@RestController注解。

index.ftlh

<!DOCTYPE html><html><head> <title>test</title></head><body>hello ${name}!</body></html>

運行

運行Application類里的main方法。

然后訪問localhost:8080/index,結果展示為:

hello Alice!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 芷江| 夏津县| 勐海县| 石林| 平阴县| 莫力| 喀喇沁旗| 朝阳县| 凌源市| 安康市| 应用必备| 运城市| 革吉县| 武陟县| 阜平县| 桂东县| 延庆县| 喀什市| 上杭县| 阿克陶县| 田阳县| 新竹市| 乾安县| 南召县| 思南县| 安塞县| 凤山市| 临西县| 乌兰察布市| 崇左市| 贵州省| 楚雄市| 南岸区| 比如县| 改则县| 连城县| 集贤县| 屏东市| 潍坊市| 香河县| 雅安市|