FreeMarker 中数值比较的问题

dscideal 2012-03-14 09:55:11
以下是报错信息

FreeMarker template error!

The only legal comparisons are between two numbers, two strings, or two dates.
Left hand operand is a freemarker.ext.beans.NumberModel
Right hand operand is a freemarker.template.SimpleScalar


The problematic instruction:
----------
==> if (list.newstype.id==typeid) [on line 43, column 49 in WEB-INF/inc.ftl]


这里是inc.ftl文件

<#macro indexNews titlelength contLength typeid showCount type>
<#if frontNews??>
<#assign count = 0>
<#list frontNews as list>
<#if (type=="cptj")>
<#if (count<showCount)>
<#if (list.newstype.id==typeid)>
<#assign count = count+1>
<p>
……………………………………………………后面的就省略了
...全文
870 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Erikz 2014-03-06
  • 打赏
  • 举报
回复
怎么解决的啊
dscideal 2012-03-15
  • 打赏
  • 举报
回复
擦。。没人回了。不过已经解决了。。
dscideal 2012-03-14
  • 打赏
  • 举报
回复
我也知道两边是类型 不匹配,但怎么把typeid转换为int型的啊。。。总之两边类型一样就行了
dscideal 2012-03-14
  • 打赏
  • 举报
回复
inc.ftl文件发错了
<#macro indexNews titlelength contLength typeid showCount type>
<#if frontNews??>
<#assign count = 0>
<#list frontNews as list>
<#if (type=="cptj")>
<#if (count<showCount)>
<#if (list.newstype.id==typeid)>
<#assign count = count+1>
<p>
Freemarker页面语法 A 概念 最常用的 3 个概念 sequence 序列,对应java 里的list 、数组等非键值对的集合 hash 键值对的集合 namespace 对一个ftl 文件的引用, 利用这个名字可以访问到该ftl 文件的资源 B 指令 if, else, elseif 语法 Java代码 ... ... ... ... ... ... ... ... ... ... 用例 Freemarker代码 x is 1 x is 1 x is not 1 x is 1 x is 1 x is not 1 switch, case, default, break 语法 Freemarker代码 ... ... ... ... ... ... ... ... ... ... 用例 字符串 Freemarker代码 This will be processed if it is small This will be processed if it is medium This will be processed if it is large This will be processed if it is neither This will be processed if it is small This will be processed if it is medium This will be processed if it is large This will be processed if it is neither 数字 Freemarker代码 1 2 d 1 2 d 如果x=1 输出 1 2, x=2 输出 2, x=3 输出d list, break 语法 Freemarker代码 ... ... ... ... 关键字 item_index:是list当前值的下标 item_has_next:判断list是否还有值 用例 Freemarker代码 ${x_index + 1}. ${x}, ${x_index + 1}. ${x}, 输出: 1.winter, 2.spring, 3.summer, 4.autumn include 语法 Freemarker代码 或则 Java代码 options包含两个属性 encoding="GBK" 编码格式 parse=true 是否作为ftl语法解析,默认是true,false就是以文本方式引入.注意在ftl文件里布尔值都是直接赋值 的如parse=true,而不是parse="true" 用例 /common/copyright.ftl 包含内容 Ftl代码 Copyright 2001-2002 ${me} All rights reserved. Copyright 2001-2002 ${me} All rights reserved. 模板文件 Java代码 Some test Yeah ___________________________________________________________________________ Some test Yeah ___________________________________________________________________________ 输出结果: Some test Yeah. Copyright 2001-2002 Juila Smith All rights reserved. Import 语法 Freemarker代码 类似于java里的import,它导入文件,然后就可以在当前文件里使用被导入文件里的宏组件 用例 假设mylib.ftl 里定义了宏copyright 那么我们在其他模板页面里可以这样使用 Freemarker代码 <#-- "my"在freemarker里被称作namespace --> <#-- "my"在freemarker里被称作namespace --> compress 语法 Freemarker代码 ... ... 用来压缩空白空间和空白的行 escape, noescape 语法 Freemarker代码 ... ... ... ... ... ... 用例 主要使用在相似的字符串变量输出,比如某一个模块的所有字符串输出都必须是html安全的,这个时候就可以使用 该表达式 Freemarker代码 First name: ${firstName} Last name: ${lastName} Maiden name: ${maidenName} First name: ${firstName} Last name: ${lastName} Maiden name: ${maidenName} 相同表达式 Ftl代码 First name: ${firstName?html} Last name: ${lastName } Maiden name: ${maidenName?html} First name: ${firstName?html} Last name: ${lastName } Maiden name: ${maidenName?html} assign 语法 Freemarker代码 <#-- 或则 --> <#-- 或则 --> <#-- 或则 --> capture this <#-- 或则 --> capture this <#-- 或则 --> <#-- 或则 --> <#-- 或则 --> capture this <#-- 或则 --> capture this 用例 生成变量,并且给变量赋值 给seasons赋予序列值 Ftl代码 给变量test加1 Ftl代码 给my namespage 赋予一个变量bgColor,下面可以通过my.bgColor来访问这个变量 Ftl代码 将一段输出的文本作为变量保存在x里 Ftl代码 ${n} Number of words: ${x?word_list?size} ${x} Hello ${user}! error true ${n} Number of words: ${x?word_list?size} ${x} Hello ${user}! error true 同时也支持文赋值,如: Ftl代码 java ${语法} java ${语法} 打印输出: java global 语法 Freemarker代码 <#--或则--> <#--或则--> capture this <#--或则--> <#--或则--> capture this 全局赋值语法,利用这个语法给变量赋值,那么这个变量在所有的namespace [A1] 是可见的, 如果这个变量被当前的assign 语法覆盖 如 在当前页面里x=2 将被隐藏,或者通过${.global.x} 来访问 setting 语法 Freemarker代码 用来设置整个系统的一个环境 locale number_format boolean_format date_format , time_format , datetime_format time_zone classic_compatible 用例 假如当前是匈牙利的设置,然后修改成美国 Ftl代码 ${1.2} ${1.2} ${1.2} ${1.2} 输出 1,2 1.2 因为匈牙利是采用", "作为十进制的分隔符,美国是用". " macro, nested, return 语法 Freemarker代码 ... ... ... ... ... ... 用例 Ftl代码 Test text, and the params: ${foo}, ${bar}, ${baaz} Test text, and the params: ${foo}, ${bar}, ${baaz} 输出 Test text, and the params: a, b, 23 Test text, and the params: a, b, -1 Test text, and the params: a, Bar, 23 Test text, and the params: a, Bar, -1 定义循环输出的宏 Ftl代码 ${title?cap_first}: *${x?cap_first} ${title?cap_first}: *${x?cap_first} 输出结果: Animals: *Mouse *Elephant *Python 包含body 的宏 Ftl代码 ${c}. ${halfc} Last! ${c}. ${halfc} Last! 输出 1. 0.5 2. 1 3. 1.5 4. 2 Last! t, lt, rt 语法 Freemarkder代码 去掉左右空白和回车换行 去掉左边空白和回车换行 去掉右边空白和回车换行 取消上面的效果 去掉左右空白和回车换行 去掉左边空白和回车换行 去掉右边空白和回车换行 取消上面的效果 C 一些常用方法或注意事项 表达式转换类 ${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 替代 判断对象是不是 null Ftl代码 Mouse found Mouse found 也可以直接${mouse?if_exists})输出布尔形 -------------------------------------------- (1)解决输出文乱码问题freemarker乱码的原因: 没有使用正确的编码格式读取模版文件,表现为模版文为乱码 解决方法:在classpath上放置一个文件freemarker.properties,在里面写上模版文件的编码方式,比如 default_encoding=UTF-8 locale=zh_CN 注意:eclipse除了xml文件、java文件外,默认的文件格式iso8859-1 数据插入模版时,没有使用正确的编码,表现出模版的新插入数据为乱码 解决方法:在result的配置,指定charset,s2的FreemarkerResult.java会将charset传递freemarker /pages/Person/view.ftl text/html;charset=UTF-8 (2)提高freemarker的性能 在freemarker.properties设置: template_update_delay=60000 避免每次请求都重新载入模版,即充分利用cached的模版 (3)尽量使用freemarker本身的提供的tag,使用S2 tags 的标签会在性能上有所损失 (4)freemarker的标签种类: ${..}:FreeMarker will replace it in the output with the actual value of the thing in the curly brackets. They are called interpolation s. # ,代表是FTL tags(FreeMarker Template Language tags) ,hey are instructions to FreeMarker and will not be printed to the output ... @ ,代表用户自定义的标签 <#-- --> 注释标签,注意不是<!-- --> (5)一些特殊的指令: r代表原样输出:${r"C:\foo\bar"} ${x} ?引出内置指令 String处理指令: html:特殊的html字符将会被转义,比如"<",处理后的结果是< cap_first 、lower_case 、upper_case trim :除去字符串前后的空格 sequences处理指令 size :返回sequences的大小 numbers处理指令 int:number的整数部分,(e.g. -1.9?int is -1) (6)对于null,或者miss value,freemarker会报错 ?exists:旧版本的用法 !:default value operator,语法结构为: unsafe_expr !default_expr,比如 ${mouse!"No mouse."} 当mouse不存在时,返回default value; (product.color)!"red" 这种方式,能够处理product或者color为miss value的情况; 而product.color!"red"将只处理color为miss value的情况 ??: Missing value test operator ,测试是否为missing value unsafe_expr ?? :product.color??将只测试color是否为null (unsafe_expr )??:(product.color)??将测试product和color是否存在null Ftl代码 <#if mouse??> Mouse found No mouse found Creating mouse... <#if mouse??> Mouse found No mouse found <#if mouse??> Mouse found No mouse found Creating mouse... <#if mouse??> Mouse found No mouse found (7)模版值插入方式 (interpolation) 通用方式 ( Universal interpolations): ${expression } 对于字符串:只是简单输出 对于数值,会自动根据local确定格式,称为human audience,否则称为computer audience,可以"?c", 比如, Details...,因此这里的id是给浏览器使用的,不需要进行格式化,注意?c只对数值有效 对于日期,会使用默认的日期格式转换,因此需要事先设置好默认的转换格式,包括date_format , time_format ,atetime_format 对于布尔值,不能输出,会报错并停止模版的执行,比如${a = 2} 会出错,但是可以 string built-in来进行转换 数值处理,具体参考:Built-ins for numbers http://freemarker.org/docs/ref_builtins_number.html#ref_builtin_string_for_number 数值处理的例子: ${answer} ${answer?string} <#-- the same as ${answer} --> ${answer?string.number} ${answer?string.currency} ${answer?string.percent} 除了使用内置的formate,可以使用任何用Java decimal number format syntax 书写的formate,比如 ${1234} ${12345?string("0.####E0")} 更加方便的格式: US people writes: ${12345678?string(",##0.00")} Hungarian people writes: ${12345678?string(",##0.00")} 日期处理,参考Built-ins for dates http://freemarker.org/docs/ref_builtins_date.html#ref_builtin_string_for_date 日期处理的例子: ${openingTime?string.short} ${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')'")} 数值专用方式 ( Numerical interpolations):#{expression } or #{expression ; format },这是数值专用的输出方式,但是 最好使用通用方式的string built-in或者number_format 来完成转换,Numerical interpolations方式将会被停用 (8)创建自定义模版 Ftl代码 Hello Joe! 评论这张 转发至微博
前言................................................................................................................................. 7 什么是FreeMarker? ................................................................................................. 7 我们应该阅读什么内容? .......................................................................................... 8 文档规约................................................................................................................... 8 联系我们................................................................................................................... 8 几点说明................................................................................................................... 9 第一部分 模板开发指南 ................................................................................................. 10 第一章 模板开发入门 ............................................................................................. 10 1.1 简介 .......................................................................................................... 10 1.2 模板 + 数据模型 = 输出 ........................................................................... 10 1.3 数据模型一览 ............................................................................................ 11 1.4 模板一览 ................................................................................................... 14 第二章 数值和类型 ................................................................................................. 20 2.1 基本内容 ................................................................................................... 20 2.2 类型 .......................................................................................................... 22 第三章 模板 ........................................................................................................... 26 3.1 总体结构 ................................................................................................... 26 3.2 指令 .......................................................................................................... 27 3.3 表达式 ....................................................................................................... 28 3.4 插值 .......................................................................................................... 43 第四章 其它 ........................................................................................................... 46 4.1 自定义指令 ................................................................................................ 46 4.2 在模板定义变量 ..................................................................................... 51 4.3 命名空间 ................................................................................................... 54 4.4 空白处理 ................................................................................................... 57 4.5替换(方括号)语法 ................................................................................... 60 第二部分 程序开发指南 ................................................................................................. 62 第一章 程序开发入门 ............................................................................................. 62 1.1 创建配置实例 ............................................................................................ 62 1.2 创建数据模型 ............................................................................................ 62 1.3 获得模板 ................................................................................................... 63 1.4 合并模板和数据模型 .................................................................................. 64 1.5将代码放在一起 .......................................................................................... 64 第二章 数据模型 .................................................................................................... 66 2.1 基本内容 ................................................................................................... 66 2.2 标量 .......................................................................................................... 66 2.3 容器 .......................................................................................................... 67 2.4 方法 .......................................................................................................... 68 2.5 指令 .......................................................................................................... 69 2.6 节点变量 ................................................................................................... 75 2.7 对象包装 ................................................................................................... 76 第三章 配置 ........................................................................................................... 80 3.1 基本内容 ................................................................................................... 80 3.2 共享变量 ................................................................................................... 80 3.3 配置信息 ................................................................................................... 81 3.4 模板加载 ................................................................................................... 83 3.5 错误控制 ................................................................................................... 86 第四章 其它 ........................................................................................................... 90 4.1 变量 .......................................................................................................... 90 4.2 字符集问题 ................................................................................................ 90 4.3 多线程 ....................................................................................................... 92 4.4 Bean的包装 ................................................................................................ 92 4.5 日志 .......................................................................................................... 98 4.6 在Servlet使用FreeMarker ...................................................................... 99 4.7 为FreeMarker配置安全策略......................................................................106 4.8 遗留的XML包装实现 ................................................................................107 4.9 和Ant一起使用FreeMarker.......................................................................111 4.10 Jython 包装器 ..........................................................................................112 第三部分 XML处理指南................................................................................................114 前言.......................................................................................................................114 第一章 揭示XML文档 ...........................................................................................115 1.1 节点树 ......................................................................................................115 1.2 将XML放到数据模型 ............................................................................117 第二章 必要的XML处理 ........................................................................................119 2.1 通过例子来学习 ........................................................................................119 2.2 形式化描述 ...............................................................................................126 第三章 声明的XML处理 ........................................................................................131 3.1 基础内容 ..................................................................................................131 3.2 详细内容 ..................................................................................................133 第四部分 参考文档 .......................................................................................................136 第一章 内建函数参考文档 .....................................................................................136 1.1 处理字符串的内建函数 .............................................................................136 1.2 处理数字的内建函数 .................................................................................150 1.3 处理日期的内建函数 .................................................................................153 1.4 处理布尔值的内建函数 .............................................................................158 1.5 处理序列的内建函数 .................................................................................158 1.6 处理哈希表的内建函数 .............................................................................164 1.7 处理节点(XML)的内建函数....................................................................164 1.8 很少使用的和专家级的内建函数 ...............................................................166 第二章 指令参考文档 ............................................................................................170 2.1 if,else,elseif指令 ...................................................................................170 2.2 switch,case,default,break指令..............................................................172 2.3 list,break 指令 .........................................................................................173 2.4 include指令 ...............................................................................................174 2.5 import 指令 ...............................................................................................177 2.6 noparse指令 ..............................................................................................178 2.7 compress指令 ............................................................................................179 2.8 escape,noescape指令...............................................................................180 2.9 assign 指令 ................................................................................................182 2.10 global 指令 ..............................................................................................184 2.11 local 指令 ................................................................................................185 2.12 setting 指令 .............................................................................................185 2.13 用户自定义指令() ......................................................................187 2.14 macro,nested,return 指令 ....................................................................189 2.15 function,return 指令...............................................................................193 2.16 flush 指令 ................................................................................................195 2.17 stop 指令.................................................................................................195 2.18 ftl 指令 ....................................................................................................196 2.19 t,lt,rt 指令 ..........................................................................................197 2.20 nt 指令 ....................................................................................................198 2.21 attempt,recover 指令 .............................................................................199 2.22 visit,recurse,fallback 指令 .....................................................................200 第三章 特殊变量参考文档 .....................................................................................205 第四章 FTL的保留名称.......................................................................................207 第五章 废弃的FTL结构 .........................................................................................208 5.1 废弃的指令列表 ........................................................................................208 5.2 废弃的内建函数列表 .................................................................................208 5.3 老式的macro和call指令 ..........................................................................208 5.4 转换指令 ..................................................................................................210 5.5 老式FTL语法 ............................................................................................211 5.6 #{…}式的数字插值 ......................................................................................212 第五部分 附录 ..............................................................................................................214 附录A FAQ .............................................................................................................214 1. JSP和FreeMarker的对比 .............................................................................214 2. Velocity和FreeMarker的对比.......................................................................215 3. 为什么FreeMarker对null-s和不存在的变量很敏感,如何来处理它? ......215 4. 文档编写了特性X,但是好像FreeMarker并不知道它,或者它的行为和文档描述的不同,或者一个据称已经修改的BUG依然存在。 .....................................216 5. 为什么FreeMarker打印奇怪的数字数字格式(比如1,000,000或1 000 000而不是1000000)? ...............................................................................................216 6. 为什么FreeMarker会打印不好的小数和/或分组分隔符号(比如3.14而不是3,14).......................................................................................................................217 7. 为什么当我想用如格式打印布尔值时,FreeMarker会抛出错误,又如何来修正呢?................................................................................................................217 8. FreeMarker标签的混淆了编辑器或XML处理器,应该怎么做? .........217 9. 什么是合法的变量名? ...............................................................................217 10. 如何使用包含空格,或其他特殊字符的变量(宏)名? ............................218 11. 当我试图使用JSP客户标签时为什么会得到非法参数异常:形式参数类型不匹配?................................................................................................................218 12. 如何像jsp:include一样的方式引入其它的资源?................................219 13. 如何给普通Java方法/ TemplateMethodModelEx/ TemplateTransformModel/ TemplateDirectiveModel的实现传递普通java.lang.* / java.util.*对象的参数? ...............................................219 14. 为什么在myMap[myKey]表达式不能使用非字符串的键?那现在应该怎么做?................................................................................................................220 15. 当使用?keys/?values遍历Map(哈希表)的内容时,得到了混合真正map条目的java.util.Map的方法。当然,只是想获取map的条目。 ...............221 16. 在FreeMarker的模板如何改变序列(lists)和哈希表(maps)? ...........221 17. 关于null在FreeMarker模板语言是什么样的? ......................................222 18. 我该怎么在表达式(作为另外一个指令参数)使用指令(宏)的输出? 223 19. 在输出为什么用“?”来代替字符X? ..................................................223 20. 在模板执行完成后,怎么在模板获取计算过的值? ................................224 21. 我能允许用户上传模板吗?又如何保证安全呢? .......................................224 22. 如何在Java语言实现方法或宏而不是在模板语言?............................225 23. 为什么FreeMarker的日志压制了我的应用程序? ......................................225 24. 在基于Servlet的应用程序,如何在模板执行期间发生错误时,展示一个友好的错误提示页面,而不是堆栈轨迹? ...........................................................226 25. 我正使用一个可视化的HTML割裂模板标记的编辑器。你们可以改变模板语言的语法来兼容我的编辑器么? .........................................................................226 26. FreeMarker有多快?真的是2.X版本的要比1.X版本(经典的FreeMarker)的慢吗?................................................................................................................226 27. 我的Java类怎么才能获取到关于模板结构的信息(比如所有变量的列表)?.......................................................................................................................227 28. 你会一直提供向后的兼容性吗? ...............................................................227 29. 如果我们把FreeMarker和我们的产品一起发行,我们需要发布我们产品的源代码么? ............................................................................................................228 附录B 安装FreeMarker..........................................................................................229 附录C 构建FreeMarker..........................................................................................230 附录D 版本 ...........................................................................................................231 2.3.18版 .........................................................................................................231 2.3.17版 .........................................................................................................233 2.3.16 版.........................................................................................................235 2.3.15 版.........................................................................................................236 2.3.14 版.........................................................................................................237 2.3.13 版.........................................................................................................238 2.3.12 版.........................................................................................................238 2.3.11 版.........................................................................................................239 2.3.10 版.........................................................................................................240 2.3.9 版 ..........................................................................................................241 2.3.8 版 ..........................................................................................................242 2.3.7 版 ..........................................................................................................242 2.3.7 RC1版 .....................................................................................................243 2.3.6 版 ..........................................................................................................244 2.3.5 版 ..........................................................................................................244 2.3.4 版 ..........................................................................................................245 2.3.3 版 ..........................................................................................................246 2.3.2 版 ..........................................................................................................247 2.3.1 版 ..........................................................................................................248 2.3 版 .............................................................................................................250 2.2.8 版 ..........................................................................................................262 2.2.7 版 ..........................................................................................................263 2.2.6 版 ..........................................................................................................263 2.2.5 版 ..........................................................................................................264 2.2.4 版 ..........................................................................................................264 2.2.3 版 ..........................................................................................................265 2.2.2 版 ..........................................................................................................265 2.2.1 版 ..........................................................................................................266 2.2 版 .............................................................................................................266 2.1.5 版 ..........................................................................................................275 2.1.4 版 ..........................................................................................................275 2.1.3 版 ..........................................................................................................275 2.1.2 版 ..........................................................................................................276 2.1.1 版 ..........................................................................................................276 2.1 版 .............................................................................................................277 2.01 版 ...........................................................................................................281 2.0 版 .............................................................................................................281 2.0 RC3版........................................................................................................282 2.0 RC2版........................................................................................................283 2.0 RC1版........................................................................................................284 附录E 许可 ...........................................................................................................287 词汇表 ..........................................................................................................................288

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧