十二.spring-boot使用spring-boot-freemarker
①、在 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;</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> SysUser() { } </span><span style="color: rgba(0, 0, 255, 1)">public</span> SysUser(<span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> id, String name, String phone) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.phone =<span style="color: rgba(0, 0, 0, 1)"> phone; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> getId() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setId(<span style="color: rgba(0, 0, 255, 1)">long</span><span style="color: rgba(0, 0, 0, 1)"> id) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getPhone() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> phone; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setPhone(String phone) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.phone =<span style="color: rgba(0, 0, 0, 1)"> phone; } @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> "SysUser [id=" + id + ", name=" + name + ", phone=" + phone + "]"<span style="color: rgba(0, 0, 0, 1)">; }
}
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(</span>"hello"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String hello(Model m){ m.addAttribute(</span>"name", "spring-boot"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> "hello"<span style="color: rgba(0, 0, 0, 1)">; } @RequestMapping(</span>"sysUser"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String user(Model m){ List</span><SysUser> list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<SysUser><span style="color: rgba(0, 0, 0, 1)">(); SysUser u1 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> SysUser(0001, "hello1", "11111111111111111"<span style="color: rgba(0, 0, 0, 1)">); SysUser u2 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> SysUser(0002, "hello2", "22222222222222222"<span style="color: rgba(0, 0, 0, 1)">); SysUser u3 </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> SysUser(0003, "hello3", "33333333333333333"<span style="color: rgba(0, 0, 0, 1)">); list.add(u1); list.add(u2); list.add(u3); m.addAttribute(</span>"userList"<span style="color: rgba(0, 0, 0, 1)">, list); m.addAttribute(</span>"sysUser", "SysUser"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">return</span> "sysUser/list"<span style="color: rgba(0, 0, 0, 1)">; }
}
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 配置是否生效