SpringBoot【整合Freemarker】
【摘要】
本文介绍下SpringBoot整合Freemarker的过程,具体的Freemarker的介绍参考如下:https://dpb-bobokaoya-sm.blog.csdn.net/column/in...
本文介绍下SpringBoot整合Freemarker的过程,具体的Freemarker的介绍参考如下:https://dpb-bobokaoya-sm.blog.csdn.net/column/info/34783
整合Freemarker
1.添加依赖
我们需要额外添加freemarker的依赖,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.创建controller
创建一个普通的控制器,跳转到ftl中。
/**
* @program: springboot-03-freemarker
* @description: SpringBoot整合freemarker
* @author: 波波烤鸭
* @create: 2019-05-12 22:14
*/
@Controller
public class UserController {
/*
* 处理请求,产生数据
*/
@RequestMapping("/showUser")
public String showUser(Model model){
List<User> list = new ArrayList();
list.add(new User(1,"张三",20));
list.add(new User(2,"李四",22));
list.add(new User(3,"王五",24));
//需要一个 Model 对象
model.addAttribute("list", list);
//跳转视图
return "user";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
3.创建ftl文件
注意:springBoot 要求模板形式的视图层技术的文件必须要放到 src/main/resources 目录下必须要一个名称为 templates
ftl代码
<html>
<head>
<title>展示用户数据</title>
<meta charset="utf-9"></meta>
</head>
<body>
<table border="1" align="center" width="50%">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<#list list as user >
<tr>
<td>${user.userid}</td>
<td>${user.username}</td>
<td>${user.userage}</td>
</tr>
</#list>
</table>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
属性文件中添加后缀
spring.freemarker.suffix=.ftl
- 1
4.测试
创建启动类,然后启动访问测试。
@SpringBootApplication
public class Springboot03FreemarkerApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot03FreemarkerApplication.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
访问成功~整合成功
文章来源: dpb-bobokaoya-sm.blog.csdn.net,作者:波波烤鸭,版权归原作者所有,如需转载,请联系作者。
原文链接:dpb-bobokaoya-sm.blog.csdn.net/article/details/90146588
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)