1. 什么是freemark

      Freemaker是一个”模板引擎”,也可以说是一个基于模板技术的生成文本输出的一个通用工具

2.一般的用途:

l 能用来生成任意格式的文本:HTML,XML,RTF,JAVA源码,等等.

3.基本目标:代替以前的jsp显示页面

注意一般用freemarke来做哪些jsp页面:

1. 多查询页面

2.操作少的页面只是单纯的展示 比如新闻页面

3.访问量比较大的网站

优点 : 访问速度快 效率高

每次查询不进入数据库 缓解数据库压力

防止前台的高并发

JSP编译后的class代码的位置
D:\WorkspaceExam\six\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
 
而使用freemarker 则不用生成class文件 

freemark是需要jar包的

freemerk是要导入jar包的

导入freemark的jar 包 一下用的是 2.3.16.jar 包   有文件

maven : 坐标

<dependency>

<groupId>org.freemarker</groupId>

<artifactId>freemarker</artifactId>

<version>2.3.16</version>

</dependency>

第一个简单例子:

控制台输出

//1、创建数据模型

Map<String,Object> root = new HashMap<String,Object>();

//2、为数据模型添加值

root.put("user", "橙子");

root.put("url", "www.baidu.com");

root.put("name", "王博");

//3.指定一个你要使用的模板名字   模型的名字不一定是 .ftl 文件

String name="tempalte_news.shtml";

//4.通过Freemaker的Configuration读取相应的ftl文件 用的是template 接受模板

Configuration cfg = new Configuration();

//设定去哪里读取相应的ftl模板文件

cfg.setClassForTemplateLoadin

uiuuug(TestYang.class,"../template")

//然后在模板文件目录中找到名称为name的文件   这样一个模板就找到了 temp

Template temp = cfg.getTemplate(name);

//5.最后一步 把模板和你的数据模板处理加工一下 就是说融合一下 并且输出

temp.process(root, new PrintWriter(System.out));

// 需要抛异常的!!!!

生成的静态页面

public void fprint(String name,Map<Object,Object> rootList,String outFile) {

//name 是要使用的模板名称  rootList 是数据模型  注意事项: 必须是Map

// outFile 是要生成的静态化页面

FileWriter out = null;

try {

//通过一个文件输出流FileWriter,就可以写到相应的文件中

//获取文件得保存路径

String rootPath=getClass().getResource("/").getFile().toString();

rootPath = rootPath.replace("/build/classes", "/WebContent/WEB-INF");

System.out.println(rootPath);

//实例化文件输出流

out = new FileWriter(new File(rootPath+"html/"+outFile));

//根据名字查询索要的模板

Template temp = this.getTemplate(name);

// temp是使用的模板  root为数据模板   out是写出的文件物理地址  temp

temp.process(rootList, out);

System.out.println(outFile+"生成成功!!!");

catch (IOException e) {

e.printStackTrace();

catch (TemplateException e) {

e.printStackTrace();

finally {

try {

if(out!=null) out.close();

catch (IOException e) {

e.printStackTrace();

}

}

}

模板:

<#list rootList as user>

<tr>

<td>${user.userId}</td>

<td>${user.userName}</td>

<td>${user.userAccount}</td>

<td>${user.userPw}</td>

<td>

<#if user.userSex == 1>

<#elseif user.userSex == 2>

<#else>

没选

</#if>

</td>

<td>删除</td>

</tr>

</#list>

注意事项:

在显示时使用的list 循环标签 其实里面循环的是Map

freemerk 标签!!!

循环迭代标签

list 和 <#break>

//sequence 要循环的属性   好比c 标签的里面的 items

// item 是你循环后的属性名类似c 标签里面的 var

//注意事项: sequence 是map里的键

<#list sequence as item>   
                 
        </#list>

<#break>  //跳出list循环

实例:

<#list users as user>

${user.id}---------${user.name}-------${user.age}<br/>

</#list>

</#list> 其他属性:

item_index:是list当前值的下标 
                item_has_next:判断list是否还有值

<#if 判断条件>

<#if x = 1>

</#if>

<#if 判断条件> 和<#else 判断条件>
<#if x = 1>   
  x is 1  
<#else>   
  x is not 1  
</#if>

<#if><#else if ><#else> 联合使用

<#if  x == 1>

x is 1

<#else x == 2>

x is 2

<#else>

x not 1 or 2

</#if>

日期转换

<#--日期也不能直接输出,需要转换为字符串String-->

注意事项:<#--${now?string}没有为日期设定格式也会报错-->

${值?string("yyyy-MM-dd HH:mm:ss")}

实例:

用户生日:

${user.userBir?string("yyyy-MM-dd")}

声明和定义

定义:<#assign 属性='值'>;

取${属性}

实例:

<#--定义变量-->

<#assign username="王博"/>

${username}

最新文章

  1. Knock: 使用压电传感器来检测敲击
  2. git 新建服务器的版本以及项目的用户
  3. jQuery常用方法验证
  4. LeetCode Best Time to Buy and Sell Stock with Cooldown
  5. git学习笔记10-新开发的功能不想要了-强行删除分支
  6. css优先级判断
  7. 做SqlDependency总结的一些经验
  8. IOS 在Ipad 横屏 上使用UIImagePickerController
  9. 我的第一个QML Button的实现
  10. 使用Python管理Azure(1):基础配置
  11. Android ProgressBar 反向进度条/进度条从右到左走
  12. iOS 设置控件圆角、文字、字体
  13. Vue导出json数据到Excel表格
  14. 饮冰三年-人工智能-Python-21 Python数据库MySql
  15. openvpn 初步使用
  16. Delphi 导出数据至Excel的7种方法【转】
  17. Linux 网络监控工具 ss
  18. 课堂讨论 alpha版最后总结
  19. android开发(39) 使用android系统的账户中心管理账
  20. 避免使用jQuery的html方法来替换标签,而是使用replaceWith方法

热门文章

  1. No mapping found for HTTP request with URI [/test2/test/add.json] in DispatcherServlet with name &#39;dispatcher&#39;
  2. 实体转XML XML转实体
  3. c# 值类型 之枚举
  4. UI Framework-1: Aura Layout Managers
  5. CentOS6.9下sftp配置和scp用法
  6. 最大优先队列 A - 奇怪的玩意
  7. LinkedList源码学习
  8. Redis批量执行(如list批量添加)命令工具 —— pipeline管道应用
  9. 今日SGU 6.5
  10. scala细节