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
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
IndexController.class
@Controller
public 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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用自動(dòng)配置xxxAutoConfiguration
這篇文章介紹了SpringBoot自動(dòng)配置xxxAutoConfiguration的使用方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
SpringBoot實(shí)現(xiàn)動(dòng)態(tài)多線程并發(fā)定時(shí)任務(wù)
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)動(dòng)態(tài)多線程并發(fā)定時(shí)任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
詳解如何實(shí)現(xiàn)SpringBoot的底層注解
今天給大家?guī)淼奈恼率侨绾螌?shí)現(xiàn)SpringBoot的底層注解,文中有非常詳細(xì)的介紹及代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴很有幫助,需要的朋友可以參考下2021-06-06
SpringBoot快速接入OpenAI大模型的方法(JDK8)
本文介紹了如何使用AI4J快速接入OpenAI大模型,并展示了如何實(shí)現(xiàn)流式與非流式的輸出,以及對(duì)函數(shù)調(diào)用的使用,AI4J支持JDK8,適用于多種應(yīng)用場(chǎng)景,包括Spring Boot項(xiàng)目,感興趣的朋友一起看看吧2025-02-02
Springboot使用Rabbitmq的延時(shí)隊(duì)列+死信隊(duì)列實(shí)現(xiàn)消息延期消費(fèi)
本文介紹了RabbitMQ的延時(shí)隊(duì)列和死信隊(duì)列,解釋了它們的工作原理及其應(yīng)用場(chǎng)景,延時(shí)隊(duì)列允許消息在設(shè)定的時(shí)間后被消費(fèi),結(jié)合實(shí)際案例,展示了如何實(shí)現(xiàn)和使用延時(shí)隊(duì)列和死信隊(duì)列,感興趣的朋友一起看看吧2025-01-01
基于NIO的Netty網(wǎng)絡(luò)框架(詳解)
下面小編就為大家?guī)硪黄贜IO的Netty網(wǎng)絡(luò)框架(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

