- 浏览: 81386 次
- 性别:
- 来自: 上海
-
最新评论
-
latent:
我 Ctrl + H 怎么查呀? 求解呀
eclipse中搜索替换所有中文字符 -
marc0658:
匹配特定数字:^[1-9]\d*$ ...
常用正则 -
marc0658:
匹配中文字符的正则表 ...
常用正则 -
marc0658:
1。^\d+$ //匹配非负整数(正整数 + 0) 2。^[ ...
常用正则 -
leejon:
appserv/
一路上next,一分钟搞定apache+ph ...
apache_2.2.14+php-5.2.11+mysql5.0.18
最常用的3个概念 sequence 序列,对应java里的list、数组等非键值对的集合 hash 键值对的集合 namespace 对一个ftl文件的引用,利用这个名字可以访问到该ftl文件的资源 </#if> </#if> </#if> 字符串 </#switch> 数字 如果x=1 输出 1 2, x=2输出 2, x=3 输出d /common/copyright.ftl包含内容 模板文件 Yeah. <#include "/common/copyright.ftl" encoding=”GBK”> 输出结果 Yeah. 假设mylib.ftl里定义了宏copyright那么我们在其他模板页面里可以这样使用 <@my.copyright date="1999-2002"/> "my"在freemarker里被称作namespace </#compress> 用来压缩空白空间和空白的行 输出 I said, test only) 全局赋值语法,利用这个语法给变量赋值,那么这个变量在所有的namespace[A1] 中是可见的,如果这个变量被当前的assign语法覆盖 如<#global x=2> <#assign x=1> 在当前页面里x=2将被隐藏,或者通过${.global.x}来访问 用来设置整个系统的一个环境 locale number_format boolean_format date_format, time_format, datetime_format time_zone classic_compatible 假如当前是匈牙利的设置,然后修改成美国 输出 因为匈牙利是采用“,”作为十进制的分隔符,美国是用“.” <@test foo="a"/> 输出 Test text, and the params: a, Bar, -1 定义循环输出的宏 ${title?cap_first}: <@list items=["mouse", "elephant", "python"] title="Animals"/> Animals: 包含body的宏 </@repeat> 输出 4. 2 Last! ${expression}计算expression并输出 #{ expression }数字计算#{ expression ;format}安格式输出数字format为M和m M表示小数点后最多的位数,m表示小数点后最少的位数如#{121.2322;m2M2}输出121.23 1..5 表示从1到5,原型number..number ${123.23?int} 输出123 ${var?default(“hello world”)?html}如果var is null那么将会被hello world替代 (1)解决输出中文乱码问题: freemarker乱码的原因: 解决方法:在classpath上放置一个文件freemarker.properties,在里面写上模版文件的编码方式,比如 default_encoding=UTF-8 注意:eclipse中除了xml文件、java文件外,默认的文件格式iso8859-1 解决方法:在result的配置中,指定charset,s2的FreemarkerResult.java会将charset传递freemarker <action name="ListPersons" class="ListPersons"> <result type="freemarker"> <param name="location">/pages/Person/view.ftl</param> <param name="contentType"> text/html;charset=UTF-8 </param> </result> </action> (2)提高freemarker的性能 在freemarker.properties中设置: template_update_delay=60000 避免每次请求都重新载入模版,即充分利用cached的模版 (3)尽量使用freemarker本身的提供的tag,使用S2 tags 的标签会在性能上有所损失 Freemarker has support for iterating lists, displaying properties, including other templates, macro's, and so on. There is a small performance cost when using the S2 tags instead of the Freemarker equivalent (eg. <s:property value="foo"/> should be replaced by ${foo}). (4)freemarker的标签种类: (5)一些特殊的指令: (6)对于null,或者miss value,freemarker会报错 (7)模版值插入方式(interpolation) 数值处理,具体参考:Built-ins for numbers http://freemarker.org/docs/ref_builtins_number.html#ref_builtin_string_for_number 数值处理的例子: <#setting number_format="currency"/> 除了使用内置的formate,可以使用任何用Java decimal number format syntax书写的formate,比如 http://freemarker.org/docs/ref_builtins_date.html#ref_builtin_string_for_date 日期处理的例子: ${openingTime?string.short} (8)创建自定义模版
A概念
B指令
if, else, elseif
语法
<#if condition>
...
<#elseif condition2>
...
<#elseif condition3>
...
...
<#else>
...
用例
<#if x = 1>
x is 1
<#if x = 1>
x is 1
<#else>
x is not 1
switch, case, default, break
语法
<#switch value>
<#case refValue1>
...
<#break>
<#case refValue2>
...
<#break>
...
<#case refValueN>
...
<#break>
<#default>
...
</#switch>
用例
<#switch being.size>
<#case "small">
This will be processed if it is small
<#break>
<#case "medium">
This will be processed if it is medium
<#break>
<#case "large">
This will be processed if it is large
<#break>
<#default>
This will be processed if it is neither
<#switch x>
<#case x = 1>
1
<#case x = 2>
2
<#default>
d
</#switch>
list, break
语法
<#list sequence as item>
...
<#if item = "spring"><#break></#if>
...
</#list>
关键字
item_index:是list当前值的下标
item_has_next:判断list是否还有值
用例
<#assign seq = ["winter", "spring", "summer", "autumn"]>
<#list seq as x>
${x_index + 1}. ${x}<#if x_has_next>,</#if>
</#list>
输出
1. winter,
2. spring,
3. summer,
4. autumn
include
语法
<#include filename>
or
<#include filename options>
options包含两个属性
encoding=”GBK” 编码格式
parse=true 是否作为ftl语法解析,默认是true,false就是以文本方式引入.注意在ftl文件里布尔值都是直接赋值的如parse=true,而不是parse=”true”
用例
Copyright 2001-2002 ${me}
All rights reserved.
<#assign me = "Juila Smith">
Some test
Some test
Copyright 2001-2002 Juila Smith
All rights reserved.
Import
语法
<#import path as hash>
类似于java里的import,它导入文件,然后就可以在当前文件里使用被导入文件里的宏组件
用例
<#import "/libs/mylib.ftl" as my>
compress
语法
<#compress>
...
用例
<#assign x = "<span style="mso-spacerun: yes"> </span>moo<span style="mso-spacerun: yes"> </span>\n\n<span style="mso-spacerun: yes"> </span>">
(<#compress>
1 2 3 4 5
${moo}
test only
I said, test only
</#compress>)
(1 2 3 4 5
moo
test only
escape, noescape
语法
<#escape identifier as expression>
...
<#noescape>...</#noescape>
...
</#escape>
用例
主要使用在相似的字符串变量输出,比如某一个模块的所有字符串输出都必须是html安全的,这个时候就可以使用该表达式
<#escape x as x?html>
First name: ${firstName}
<#noescape>Last name: ${lastName}</#noescape>
Maiden name: ${maidenName}
</#escape>
相同表达式
First name: ${firstName?html}
Last name: ${lastName }
Maiden name: ${maidenName?html}
assign
语法
<#assign name=value>
or
<#assign name1=value1 name2=value2 ... nameN=valueN>
or
<#assign same as above... in namespacehash>
or
<#assign name>
capture this
</#assign>
or
<#assign name in namespacehash>
capture this
</#assign>
用例
生成变量,并且给变量赋值
给seasons赋予序列值
<#assign seasons = ["winter", "spring", "summer", "autumn"]>
给变量test加1
<#assign test = test + 1>
给my namespage 赋予一个变量bgColor,下面可以通过my.bgColor来访问这个变量
<#import "/mylib.ftl" as my>
<#assign bgColor="red" in my>
将一段输出的文本作为变量保存在x里
下面的阴影部分输出的文本将被赋值给x
<#assign x>
<#list 1..3 as n>
${n} <@myMacro />
</#list>
</#assign>
Number of words: ${x?word_list?size}
${x}
<#assign x>Hello ${user}!</#assign> error
<#assign x=” Hello ${user}!”> true
同时也支持中文赋值,如:
<#assign 语法>
java
</#assign>
${语法}
打印输出:
java
global
语法
<#global name=value>
or
<#global name1=value1 name2=value2 ... nameN=valueN>
or
<#global name>
capture this
</#global>
setting
语法
<#setting name=value>
用例
${1.2}
<#setting locale="en_US">
${1.2}
1,2
1.2
macro, nested, return
语法
<#macro name param1 param2 ... paramN>
...
<#nested loopvar1, loopvar2, ..., loopvarN>
...
<#return>
...
</#macro>
用例
<#macro test foo bar="Bar"[A2] baaz=-1>
Test text, and the params: ${foo}, ${bar}, ${baaz}
</#macro>
<@test foo="a" bar="b" baaz=5*5-2/>
<@test foo="a" bar="b"/>
<@test foo="a" baaz=5*5-2/>
Test text, and the params: a, b, 23
Test text, and the params: a, b, -1
Test text, and the params: a, Bar, 23
<#macro list title items>
<#list items as x>
</#list>
</#macro>
输出结果
<#macro repeat count>
<#list 1..count as x>
<#nested x, x/2, x==count>
</#list>
</#macro>
<@repeat count=4 ; c halfc last>
${c}. ${halfc}<#if last> Last!</#if>
1. 0.5
2. 1
3. 1.5
t, lt, rt
语法
<#t> 去掉左右空白和回车换行
<#lt>去掉左边空白和回车换行
<#rt>去掉右边空白和回车换行
<#nt>取消上面的效果
C一些常用方法或注意事项
表达式转换类
数字循环
对浮点取整数
给变量默认值
判断对象是不是null
<#if mouse?exists>
Mouse found
<#else>
也可以直接${mouse?if_exists})输出布尔形
--------------------------------------------
locale=zh_CN
比如:
<#if mouse??>
Mouse found
<#else>
No mouse found
</#if>
Creating mouse...
<#assign mouse = "Jerry">
<#if mouse??>
Mouse found
<#else>
No mouse found
</#if>
<#assign answer=42/>
${answer}
${answer?string} <#-- the same as ${answer} -->
${answer?string.number}
${answer?string.currency}
${answer?string.percent}<#setting number_format="0.###E0"/>
${1234}
${12345?string("0.####E0")}
更加方便的格式:
<#setting locale="en_US">
US people writes: ${12345678?string(",##0.00")}
<#setting locale="hu">
Hungarian people writes: ${12345678?string(",##0.00")}
日期处理,参考Built-ins for dates
${openingTime?string.medium}
${openingTime?string.long}
${openingTime?string.full}
${nextDiscountDay?string.short}
${nextDiscountDay?string.medium}
${nextDiscountDay?string.long}
${nextDiscountDay?string.full}
${lastUpdated?string.short}
${lastUpdated?string.medium}
${lastUpdated?string.long}
${lastUpdated?string.full}
注意:
由于java语言中的Date类型的不足,freemarker不能根据Date变量判断出变量包含的部分(日期、时间还是全部),在这种情况下,freemarker
不能正确显示出${lastUpdated?string.short} 或者 simply ${lastUpdated},因此,可以通过?date, ?time and ?datetime built-ins
来帮助freemarker来进行判断,比如${lastUpdated?datetime?string.short}
除了使用内置的日期转换格式外,可以自己指定日期的格式,使用的是Java date format syntax,比如:
${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}
${lastUpdated?string("EEE, MMM d, ''yy")}
${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}
创建:
<#macro greet>
<font size="+2">Hello Joe!</font>
</#macro>
发表评论
-
oracle10g for linux
2013-01-13 22:01 1057Oracle Database 10g Release 2 ( ... -
ORA-12519: TNS:no appropriate service handler found
2013-01-08 09:17 818from http://blog.163.com/kan ... -
jfreechart中文乱码
2012-12-29 09:40 822看看API,设置字体吧 /* 下面是设置曲线图图 ... -
访问局域网Oracle数据库
2012-12-02 17:58 905from http://blog.sina.com.c ... -
windows7开放80端口支持局域网访问apache
2012-12-01 16:52 852from http://www.2cto.com/os/ ... -
Win7下Eclipse中文字体太小
2012-10-23 09:49 759from http://www.cnblogs.com/ ... -
oracle order by
2012-09-05 10:09 828转载自: http://blog.csdn.net/w ... -
php wordpress upload_max_size
2012-08-23 13:33 768http://www.dwuser.com/easyr ... -
windows 快捷键大全 窗口最大化快捷键,最小化,重命名等
2012-08-17 14:36 1031一直以来都很喜欢用windows的快捷键,但学电脑有五年 ... -
本地wordpress 固定链接
2012-08-14 18:07 782win 本地开发环境 固定链接结构不选用默认 ... -
Hibernate 参数设置一览表
2012-08-13 11:37 820转载:http://www.blogjava.net ... -
PLSQL中的&字符处理
2012-08-08 14:21 913转载:http://www.cnblogs.com/Rober ... -
web.xml 中的listener、 filter、servlet 加载顺序及其详解
2012-08-03 11:14 908转载:http://www.cnblogs.co ... -
非法字符:\65279 编码 GBK 的不可映射字符
2012-07-31 15:24 1339警告: 编码 GBK 的不可映射字符"怎么处 ... -
IE6 下 select 动态赋值
2012-07-17 14:25 932setTimeout(function(){ $ ... -
ie6 option innerHTML 关于select 的添加 option 应该注意的问题
2012-07-11 11:14 2311<script type="text/j ... -
jquery获得select option的值 和对select option的操作
2012-07-10 15:31 868jQuery获取Select元素,并选择的Text和Va ... -
Web app root system property already set to different value
2012-06-27 16:36 2234最近在搭建项目环境的时候出现了下面的错误 java.l ... -
fn:length
2012-06-27 15:28 871这个问题曾经也困扰了我好久,不过以后都没有用过也都忘记了 ... -
修改textfield的label
2012-06-15 15:47 804//id:number 下面parent()的个数要看你那个t ...
相关推荐
string.short}; ${lastUpdated?string.long}; ${lastUpdated?String.full}; ``` 在这个例子中,`lastUpdated`被赋值为一个日期时间字符串,然后通过`?string`内建函数进行格式化输出。 总结来说,FreeMarker 提供...
JAVA模版引擎Freemarker常用标签(一) 1. if指令 这是一个典型的分支控制指令,该指令的作用完全类似于Java语言中的if,if指令的语法格式如下: <#if condition>... <#elseif condition>... <#elseif condition>......
`upper_case`, `is_number`, `last_index_of`, `long`, `last`, `is_directive`, `starts_with`, `capitalize`, `datetime`, `is_boolean`, `short`, `is_hash_ex`, `ends_with`, `is_enumerable`, `chunk`, `is_...
- **`short`**:处理短整型相关的操作。 - **`is_hash_ex`**:判断是否是一个哈希表扩展类型。 - **`ends_with`**:判断字符串是否以指定的子串结尾。 - **`is_enumerable`**:判断给定的对象是否可枚举。 - **`...
<short-name>ct</short-name> <uri>/customtags <name>highlight <tag-class>com.example.HighlightTag</tag-class> <body-content>empty <name>text <required>true <name>highlightColor ```...
- 整型: byte, short, int, long - 浮点型: float, double - 字符型: char - 布尔型: boolean - **复合数据类型**: - 数组 - 类 - 接口 **3. 控制结构与函数** - **分支结构**: if...else, switch - **循环...
- **整型**:byte、short、int、long - **浮点型**:float、double - **字符型**:char - **布尔型**:boolean ##### 冒泡排序 - **算法**:比较相邻元素,如果第一个比第二个大,就交换它们的位置。 - **复杂度**...
- 整型:byte、short、int、long - 浮点型:float、double - 字符型:char - 布尔型:boolean ##### 冒泡排序 - **算法思想**:比较相邻元素,将较大的元素往后移动。 ##### 二分查找法 - **算法思想**:在有序...