①、在springMVC中:它代表着view层组件
②、为什么使用freemarker:简单容易学、逻辑分明
③、freemarker优点:它不依赖servlet、网络或者web环境
一、创建一个maven web project
二、检查freemarker包是否已经添加
三、编写测试类
1、新建一个用户数据模型SysUser
package com.example.entity; public class SysUser { private long id; private String name; private String phone; public SysUser() { } public SysUser(long id, String name, String phone) { this.id = id; this.name = name; this.phone = phone; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return "SysUser [id=" + id + ", name=" + name + ", phone=" + phone + "]"; } }
2、编写controller类
package com.example.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.example.entity.SysUser; @Controller public class Hello { @RequestMapping("hello") public String hello(Model m){ m.addAttribute("name", "spring-boot"); return "hello"; } @RequestMapping("sysUser") public String user(Model m){ List<SysUser> list = new ArrayList<SysUser>(); SysUser u1 = new SysUser(0001, "hello1", "11111111111111111"); SysUser u2 = new SysUser(0002, "hello2", "22222222222222222"); SysUser u3 = new SysUser(0003, "hello3", "33333333333333333"); list.add(u1); list.add(u2); list.add(u3); m.addAttribute("userList", list); m.addAttribute("sysUser", "SysUser"); return "sysUser/list"; } }
3、配置application.properties文件
######################################################## ###FREEMARKER (FreeMarkerAutoConfiguration) ######################################################## spring.freemarker.allow-request-override=false spring.freemarker.cache=true spring.freemarker.check-template-location=true spring.freemarker.charset=UTF-8 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=false #spring.freemarker.prefix= #spring.freemarker.request-context-attribute= #spring.freemarker.settings.*= spring.freemarker.suffix=.ftl spring.freemarker.template-loader-path=classpath:/templates/ #comma-separated list #spring.freemarker.view-names= # whitelist of view names that can be resolved
4、编写.ftl模型
说明:我这里使用到的bootstrap-3.3.7方便好看 ,新建一个sysuser的视图层文件,保证良好的规则
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <meta content="text/html;charset=utf-8"></meta> <title>Hello World!</title> <script sec="../jquery-2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="../bootstrap-3.3.7/dist/css/bootstrap.min.css"></link> <script sec="../bootstrap-3.3.7/dist/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <table class="table"> <caption>${sysUser}</caption> <thead> <tr> <th>First Name</th> <th>Last Name</th> <th>User Name</th> </tr> </thead> <tbody> <tr> <td>aehyok</td> <td>leo</td> <td>@aehyok</td> </tr> <tr> <td>lynn</td> <td>thl</td> <td>@lynn</td> </tr> <#list userList as user> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.phone}</td> </tr> </#list> </tbody> </table> </div> </body> </html>
freemarker的表达式可参考本博客【FreeMarker】随笔分类:http://www.cnblogs.com/xxt19970908/p/5594258.html
四、启动测试
1、检查启动信息:freemarker配置是否生效