springboot继承freemarker
时间: 2023-10-19 10:09:19 浏览: 154
可以通过以下步骤在Spring Boot项目中集成Freemarker模板引擎:
1. 首先,在pom.xml文件中添加Freemarker的依赖项:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
```
2. 在application.properties(或application.yml)配置文件中指定Freemarker模板的位置:
```yaml
spring.freemarker.template-loader-path=classpath:/templates
spring.freemarker.suffix=.ftl
```
3. 创建一个控制器类,使用`@Controller`注解,并使用`@RequestMapping`注解来处理请求:
```java
@Controller
public class MyController {
@RequestMapping("/hello")
public String hello(ModelMap model) {
model.addAttribute("message", "Hello, Freemarker!");
return "hello";
}
}
```
4. 在resources/templates目录下创建一个名为hello.ftl的模板文件,并在该文件中使用Freemarker语法进行页面渲染,例如:
```html
<!DOCTYPE html>
<html>
<head>
<title>Hello Freemarker</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
```
在上述示例中,控制器方法`hello`返回的字符串"helloworld"将会匹配到名为hello.ftl的模板文件进行渲染,模板文件中的`${message}`将会被替换为"Hello, Freemarker!"。
阅读全文
相关推荐


















