- 创建一个springboot工程,修改pom.xml文件,添加freemarker的启动类
<!-- 添加freemarker的启动类 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
-
编写模板页面user.ftl文件(必须是以.ftl结尾的文件)文件需要放到src/main/resources 目录下必 须要一个名称为 templates
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>显示用户数据</title> </head> <body> <table border="1" align="center" width="50%"> <tr> <th>name</th> <th>age</th> <th>sex</th> </tr> <#list list as user > <tr> <th>${user.name }</th> <th>${user.age }</th> <th>${user.sex }</th> </tr> </#list> </table> </body> </html>
有关freemarker的标签的使用,我将在后序的学习中为大家推出
-
编写Controllr类
package com.test.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.test.pojo.User; /** * 整合freemarker * @author 26920 * */ @Controller public class UserController { @RequestMapping(value="/showUser") public String user(Model model){ List<User> list=new ArrayList<User>(); list.add(new User("list",20,"男")); list.add(new User("wanwu",22,"男")); list.add(new User("zhangsan",24,"男")); model.addAttribute("list", list); return "user"; } }
注意方法的返回字符串必须和模块文件名的前缀一样,大小写不限
-
编写启动类,启动项目,访问路径http://localhost:8080/showUser(有关freemarker的配置只要有一步不对,就会报404)
springboot 整合freemarker
最新推荐文章于 2024-05-04 01:14:46 发布