自己动手建设网站,如何用自己的电脑建网站,游戏试玩网站怎么做,wordpress访问简介 简介 FreeMarker是一款模板引擎#xff1a;一种基于模板的、用来生成输出文本#xff08;任何来自于HTML格式的文本用来自动生成源代码#xff09;的通用工具。它是为Java程序员提供的一个开发包或者说是类库。它不是面向最终用户#xff0c;而是为程序员提供的可以嵌…简介 简介 FreeMarker是一款模板引擎一种基于模板的、用来生成输出文本任何来自于HTML格式的文本用来自动生成源代码的通用工具。它是为Java程序员提供的一个开发包或者说是类库。它不是面向最终用户而是为程序员提供的可以嵌入他们开发产品的一款应用程序。 特点 功能 基础 概要、关键字 建议 前言 FreeMarker是一款模板引擎一种基于模板的、用来生成输出文本任何来自于HTML格式的文本用来自动生成源代码的通用工具。它是为Java程序员提供的一个开发包或者说是类库。它不是面向最终用户而是为程序员提供的可以嵌入他们开发产品的一款应用程序。 FreeMarker的设计实际上是被用来生成HTML网页尤其是通过基于实现了MVCModel View Controller模型-视图-控制器模式的Servlet应用程序。使用MVC模式的动态网页的构思使得你可以将前端设计者编写HTML从程序员中分离出来。所有人各司其职发挥其擅长的一面。网页设计师可以改写页面的显示效果而不受程序员编译代码的影响因为应用程序的逻辑Java程序和页面设计FreeMarker模板已经分开了。页面模板代码不会受到复杂的程序代码影响。这种分离的思想即便对一个程序员和页面设计师是同一个人的项目来说都是非常有用的因为分离使得代码保持简洁而且便于维护。 FreeMarker不是Web应用框架。它是Web应用框架中的一个适用的组件。 第1章 入门 1.2 模板数据模型输出 1.3 数据模型一览 1.4 模板一览 1.4.1 简介 FTL tags标签FreeMarker模板的语言标签。一般以符合#开头用户自定义的FTL标签使用代替#。 Comments注释FreeMarker的注释和HTML的注释相似但是它用#--和--来分隔。 directives指令就是所指的FTL标签。 1.4.2 指令示例 1.4.2.1 if指令 假设你只想向你的老板Big Joe而不是其他人问好就可以这样做h1
Welcome ${user}#if user Big Joe, our beloved leader/#if!
/h1 使用#else标签#if animals.python.price animals.elephant.pricePythons are cheaper than elephants today.
#elsePythons are not cheaper than elephants today.
/#if 如果变量本身就是布尔值可以直接作为if的条件#if animals.python.protectedWarniing! Pythons are protected animals!
/#if 实例 /FreeMarker-hello-web/src/main/java/org/yejq/fre/model/Animal.java
public class Animal {private String name;private double price;private boolean protect;。。。
}/FreeMarker-hello-web/src/main/java/org/yejq/fre/service/Exercises.javapublic void testIf(Model model){model.addAttribute(user, Big Joe);MapString, Animal animals new HashMapString, Animal();animals.put(python, new Animal(python, 300, true));animals.put(elephant, new Animal(elephant, 400, false));model.addAttribute(animals, animals);}/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/if.ftl
!doctype html
html langen
headmeta charsetUTF-8 /titleIf指令/title
/head
bodyh1Welcome ${user}#if user Big Joe, our beloved leader/#if/h1p#--大于号两边要加括号括起来否则会以为是结束标签 --#if (animals.python.price animals.elephant.price)python.price elephant.price#elsepython.price elephant.price/#if/pp#if animals.python.protectpython.protect true;/#if/p
/body
/html测试 http://localhost/test/2/if/testIf 1.4.2.2 list指令 当需求遍历集合的内容时list指令是非常好用的。#list animals as beingtrtd${being.name}td${being.price} Euros
/#list 实例 /FreeMarker-hello-web/src/main/java/org/yejq/fre/service/Exercises.javapublic void testList(Model model){ListAnimal animals new ArrayListAnimal();animals.add(new Animal(python, 300, true));animals.add(new Animal(elephant, 400, false));model.addAttribute(animals, animals);}/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/list.ftlh3list指令/h3table border1#list animals as animaltr#-- boolean类型要设置默认输出值否则报错 --td${animal.name}, ${animal.price}, ${animal.protect?c}/td/tr/#list/table测试 http://localhost/test/2/list/testList 1.4.2.3 include指令 在当前模板中插入其他文件的内容。 copyright_footer.html
hr
i
Copyright (c) 2000a hrefhttp://www.xxx.comAcmee Inc/a,
br
All Rights Reserved.
/i当需要copyright时引入#include /copyright_footer.html 实例 /FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/copyright.html
hr
iCopyright (c) 2000a hrefhttp://www.xqsoso.comAcmee Inc/a,brAll Rights Reserved.中文测试
/i/FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/include.ftlh3include指令/h3#include copyright.html测试http://localhost/test/2/include/null 1.4.2.4 联合使用指令 指令可以嵌套使用 1.4.2.5 处理不存在的变量h1Welcome ${user!Anonymous}!/h1 通过在变量名后边跟一个!和默认值。h1Welcome ${user!Anonymous}!/h1 可以使用??询问freemarker一个变量是否存在将它和if指令合并那么如果user变量不存在的话将会忽略整个问候代码段#if user??h1Welcome ${user}!/h1/#if 对于多级访问的变量比如animals.python.price书写代码animals.python.price!0仅当animals.python存在且最后一个子变量price可能不存在这种情况下我们假设价格是0。如果animals或者python不存在那么模板处理过程将会以“未定义的变量”错误而停止。为了防止这种情况的发生可以这样来书写代码(animals.python.price)!0。这种情况下当animals或python不存在时表达式的结果仍然是0。对于??也是同样用来的处理这种逻辑的animals.python.price??对比(animals.python.price)??来看 实例 /FreeMarker-hello-web/src/main/webapp/WEB-INF/ftl/2/null.ftlh3处理不存在的变量/h3pwelcome, ${user!anonymous}/pp检测user是否存在#if user??Welcome, ${user}/#if/p#--不加括号会报错: nested exception is freemarker.core.InvalidReferenceException: The following has evaluated to null or missing--p多级访问, ${(animals.python.price)!0}/p测试 http://localhost/test/2/null/null 参考资料 书 B1 《FreeMarker中文版文档.pdf》 B2 项目 P1F:\360\Learn\FreeMarker\workspace\FreeMarker-hello-java\https://github.com/yejq/FreeMarker-hello-java.git。 P2F:\360\Learn\freemarker\workspace\FreeMarker-hello-web\ https://github.com/yejq/FreeMarker-hello-web.git。转载于:https://www.cnblogs.com/yejq/p/3964326.html