springboot 视图使用freemarker
时间: 2023-10-14 09:20:59 浏览: 126
要在Spring Boot中使用FreeMarker视图,需要完成以下步骤:
1. 在pom.xml中添加FreeMarker依赖项:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
```
2. 在application.properties文件中配置FreeMarker:
```properties
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftl
```
3. 创建一个controller类来处理请求并返回视图:
```java
@Controller
public class MyController {
@RequestMapping("/")
public String index(Model model) {
model.addAttribute("message", "Hello, World!");
return "index";
}
}
```
4. 创建一个FreeMarker模板文件来渲染返回的数据:
```html
<!DOCTYPE html>
<html>
<head>
<title>Spring Boot FreeMarker Example</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
```
5. 启动应用程序并访问根URL,您应该看到消息“Hello, World!”。
以上是使用FreeMarker视图的基本步骤,您可以根据需要进行自定义和扩展。
阅读全文
相关推荐
















