spring boot ftl
时间: 2023-10-19 14:07:07 浏览: 120
Spring Boot与Freemarker(ftl)模板引擎结合使用可以实现动态页面的渲染。下面是使用Spring Boot和Freemarker的一般步骤:
1. 添加依赖:在`pom.xml`文件中添加Spring Boot和Freemarker的依赖。
```xml
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
```
2. 创建Freemarker模板:在`src/main/resources/templates`目录下创建Freemarker模板文件,例如`index.ftl`。
```html
<!DOCTYPE html>
<html>
<head>
<title>Spring Boot with Freemarker</title>
</head>
<body>
<h1>Welcome to Spring Boot with Freemarker!</h1>
<p>${message}</p>
</body>
</html>
```
3. 创建Controller:创建一个Spring MVC的Controller类,并添加`@Controller`和`@RequestMapping`注解。
```java
@Controller
public class HomeController {
@RequestMapping("/")
public String index(Model model) {
model.addAttribute("message", "Hello, world!");
return "index";
}
}
```
4. 启动应用程序:创建一个Spring Boot的启动类,添加`@SpringBootApplication`注解,并运行应用程序。
```java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
阅读全文
相关推荐


















