SpringBoot學習8:springboot整合freemarker
阿新 • • 發佈:2019-02-06
att autoconf ava order creat mark mini 整合 ren
1、創建maven項目,添加pom依賴
<!--springboot項目依賴的父項目--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <!--註入springboot啟動器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--註入springboot對freemarker視圖技術的支持--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies>
2、創建controller
package com.bjsxt.controller; import com.bjsxt.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2019/2/6.*/ @Controller public class UserController { @RequestMapping("/toUserList") public String toUserList(Model model){ List<User> userList=new ArrayList<User>(); userList.add(new User(1L,"張三","男")); userList.add(new User(2L,"李四","女")); userList.add(new User(3L,"王五","男")); model.addAttribute("userList",userList); return "user_list"; } }
3、創建freemarker模版文件user_list.ftl
註意:springboot 要求模板形式的視圖層技術的文件必須要放到 src/main/resources 目錄下必
須要一個名稱為 templates
<html> <head> <title>用戶列表</title> </head> <body> <table border="1px solid red"> <tr> <th>id</th> <th>姓名</th> <th>性別</th> </tr> <#list userList as user> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.sex}</td> </tr> </#list> </table> </body> </html>
4、創建啟動器
package com.bjsxt; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by Administrator on 2019/2/6. */ @SpringBootApplication public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
SpringBoot學習8:springboot整合freemarker