- 1.freemarker中Request,Session的用法:
- java块:request.getSession().setAttribute("qq","http://www.qq.com");
- request.setAttribute("qq","http://www.qq.com");
- freemarker代码:
- <#ifSession["qq"]?exists>//Request的用法同session
- <#ifSession["qq"]=="http://www.qq.com">
- <#else>
- </#if>
- <#else>
- </#if>
- 而通常情况下,Request和Session均可省去不写:
- <#ifqq?exists>//Request的用法同session
- <#ifqq=="http://www.qq.com">
- <#else>
- </#if>
- <#else>
- </#if>
- 但是如果他们都设有相同的key值,那么在页面中显示的话,就就应该把他们加上去。
- 2.freemarker中导入样式表的写法,已经javascript的写法注意:
- 引入样式表:<LINKhref="*.css"type=text/cssrel=stylesheet>
- 添加样式表:
- <styletype="text/css">
- .imgsimg{width:150px;height:70px;}
- </style>
- javascript块:
- <scriptlanguage="JavaScript">
- functionmethod(para){
- }
- </script>
- 3.判断集合:
- 集合的集合:
- <#ifvoteWraperObj.itemList?sizegt0>
- <#listvoteWraperObj.itemListasvoteList>
- </#list></#if>
- 对象的集合:
- <#ifsoftDetail?sizegt0>
- <#listsoftDetailassoftlist>
- </#list>
- </#if>
- 关于Session中存放的对象的属性的读取:
- <#ifSession["userweb"].member?exists>
- 4.switch/case的用法:
- <#switchtype>
- <#case"down">
- <#include"softdown.ftl">
- <#break>
- <#case"photo">
- <#ifpara=="photoSquare">
- <#include"photo.ftl">
- <#elseifpara=="photoWidth">
- <#include"photo.ftl">
- <#else>
- </#if>
- <#break>
- </#switch>
- 5.时间类型在页面是如何才能正常显示的:
- 如:${newslist.pubTime?string('yyyy-MM-dd')}
- ${newslist.pubTime?string("yyyy-MM-ddHH:mm:ss")}
- 6.判断存在与否,以及是否有效:
- 如:<#ifgoodslist.f_goods_url?exists&&goodslist.f_goods_url?lengthgt0>
- 7.“default”是如何使用的:
- 如:${qq?default("http://www.qq.com")}
- 即如果属性qq不存在,那么就是要default中的值替代。
- 8.<#assign>的用法:
- <#assigncount=0>
- <#assignstr="12345">
- <#list0..9asi>
- <#assigncount=count+1>
- <#ifigtstr?length>
- ${count}
- </#if>
- </#list>
- 输出为:78910
- 9.freemarker中如何截取字符串:
- 方法1:?substring(,)||方法2:[,]
- 区别:前者包头不包未,后者包头又包未:
- 实例:<#assigncount=0>
- <#assignstr="12345">
- ${str[count..count+1]}
- ${str?substring(count,count+1)}
- 输出为:12,1
- <#assigncount=0>
- <#assignstr="12345">
- ${str[count..count]}<br/>
- ${str?substring(count,count+1)}
- 这时输出均为:1
- 10.freemarker页面中如果包含
- <#assignhtml=JspTaglibs["/WEB-INF/struts-html.tld"]>
- <@html.base/>
- 那么它所指向的路径都是相当路径(相对于当前文件)。
- 11.如何让freemarker模板当html来使用呢:(完整的页面如下)
- <html>
- <head>
- <#assignhtml=JspTaglibs["/WEB-INF/struts-html.tld"]>
- <#assignbean=JspTaglibs["/WEB-INF/struts-bean.tld"]>
- <#assignlogic=JspTaglibs["/WEB-INF/struts-logic.tld"]>
- <@html.base/>
- <linkhref="css/main.css"rel="stylesheet"type="text/css"/>
- <metahttp-equiv="content-type"content="text/html;charset="utf-8">
- </head>
- <body>
- ---------freemarker代码块
- </body>
- </html>
- 12.?使用内建的int(后面讲述)获得整数部分
- 如${1.1?int}=1
- 13.比较操作符:
- 1使用=(或==,完全相等)测试两个值是否相等,使用!=测试两个值是否不相等。
- 2.对数字和日期可以使用<、<=、>和>=,但不能用于字符串
- 3.由于Freemarker会将>解释成FTL标记的结束字符,所以对于>和>=可以使用括号来避免这种情况,例如<#if(x>y)>,另一种替代的方法是,使用lt、lte、gt和gte来替代<、<=、>和>=
- 14.内建函数:
- html:对字符串进行HTML编码
- cap_first:使字符串第一个字母大写
- ??lower_case:将字符串转换成小写
- ??upper_case:将字符串转换成大写
- trim:去掉字符串前后的空白字符
- ??size:获得序列中元素的数目
- int:取得数字的整数部分(如-1.9?int的结果是-1)
- 15.根据缺省格式(由#setting指令设置)将表达式结果转换成文本输出;可以使用内建函数string格式化单个Interpolation
- 如:<#settingnumber_format="currency"/>
- <#assignanswer=42/>
- 输出结果是:$42.00
- 单个Interpolation:
- ${answer?string.number}
- ${answer?string.currency}
- ${answer?string.percent}?
- 16.插入日期值:根据缺省格式(由#setting指令设置)将表达式结果转换成文本输出;可以使用内建函数string格式化单个Interpolation
- 如:${lastUpdated?string("yyyy-MM-ddHH:mm:sszzzz")}
- 17.Session中设置对象属性的判断:
- Session["userweb"].member?exists
- 18.插入布尔值:根据缺省格式(由#setting指令设置)将表达式结果转换成文本输出;可以使用内建函数string格式化单个Interpolation.
- 如:<#assignor=true/>
- ${or?string("Youareright!","Youarewrong!")}
- 输出为:Youareright!
- 19.数字Interpolation的#{expr;format}形式可以用来格式化数字,format可以是:
- ?mX:小数部分最小X位
- ?MX:小数部分最大X位
- 如:<#assignx=2.582/>
- <#assigny=4/>
- #{x;M2}<#--2.58-->
- #{y;M2}<#--4-->
- #{x;m1}<#--2.6-->
- #{y;m1}<#--4.0-->
- #{x;m1M2}<#--2.58-->
- #{y;m1M2}<#--4.0-->
- 20.用户定义指令:宏
- <#macrogreetperson>
- <fontsize="+2">Hello${person}!</font>
- </#macro>
- 作为用户定义指令使用宏变量时,使用@替代FTL标记中的#
- <@greetperson="Fred"/>and<@greetperson="Batman"/>
- 输出为:HelloFred!andHelloBatman!
- 21:宏可以有多参数:
- <#macrogreetpersoncolor>
- <fontsize="+2"color="${color}">Hello${person}!</font>
- </#macro
- 可以这样使用该宏变量:
- <@greetperson="Fred"color="black"/>,其中参数的次序是无关的,只能使用在macro指令中定义的参数,并且对所有参数赋值,所以下面的代码是错误的:
- 只能使用在macro指令中定义的参数,并且对所有参数赋值,多了或者少了都会出错的,完全赋值。如:<@greetperson="Fred"/>为错。
- 可以在定义参数时指定缺省值,
- <#macrogreetpersoncolor="black">
- <fontsize="+2"color="${color}">Hello${person}!</font>
- </#macro>
- 这样<@greetperson="Fred"/>就正确了,宏的参数是局部变量,只能在宏定义中有效。
- 22.嵌套内容
- ?用户定义指令可以有嵌套内容,使用<#nested>指令执行指令开始和结束标记之间的模板片
- <#macroborder>
- <tableborder=4cellspacing=0cellpadding=4><tr><td>
- <#nested>
- </tr></td></table>
- </#macro>
- 这样使用该宏变量:
- <@border>Theborderedtext</@border>
- 输出结果:
- <tableborder=4cellspacing=0cellpadding=4><tr><td>
- Theborderedtext
- </tr></td></table>
- <#ifindexL_has_next>
freemarker笔记
最新推荐文章于 2023-06-19 18:18:56 发布