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>
項目結(jié)構(gòu)
src/
+- main/
+- java/
| +- com
| +- controller/
| | +- IndexController.class
| +- Application.class
+- resources/
+- templates/
+- index.ftlh
- Application為應(yīng)用程序啟動類
- IndexController為控制器,里面含有一個index請求處理方法,它返回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>
運行
運行Application類里的main方法。
然后訪問localhost:8080/index,結(jié)果展示為:
hello Alice!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用自動配置xxxAutoConfiguration
這篇文章介紹了SpringBoot自動配置xxxAutoConfiguration的使用方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
SpringBoot實現(xiàn)動態(tài)多線程并發(fā)定時任務(wù)
這篇文章主要為大家詳細(xì)介紹了SpringBoot實現(xiàn)動態(tài)多線程并發(fā)定時任務(wù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
SpringBoot快速接入OpenAI大模型的方法(JDK8)
本文介紹了如何使用AI4J快速接入OpenAI大模型,并展示了如何實現(xiàn)流式與非流式的輸出,以及對函數(shù)調(diào)用的使用,AI4J支持JDK8,適用于多種應(yīng)用場景,包括Spring Boot項目,感興趣的朋友一起看看吧2025-02-02
Springboot使用Rabbitmq的延時隊列+死信隊列實現(xiàn)消息延期消費
本文介紹了RabbitMQ的延時隊列和死信隊列,解釋了它們的工作原理及其應(yīng)用場景,延時隊列允許消息在設(shè)定的時間后被消費,結(jié)合實際案例,展示了如何實現(xiàn)和使用延時隊列和死信隊列,感興趣的朋友一起看看吧2025-01-01
基于NIO的Netty網(wǎng)絡(luò)框架(詳解)
下面小編就為大家?guī)硪黄贜IO的Netty網(wǎng)絡(luò)框架(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

