问题描述
数据库中存放[{"image":"5674e621e57a477eafacc38ae2b33123.jpg","smallImage":"small-5674e621e57a477eafacc38ae2b33123.jpg","bigImage":"big-5674e621e57a477eafacc38ae2b33123.jpg","fullImage":"full-5674e621e57a477eafacc38ae2b33123.jpg"},{"image":"b2d914623be94cbca0c686ab2c4c1066.jpg","smallImage":"small-b2d914623be94cbca0c686ab2c4c1066.jpg","bigImage":"big-b2d914623be94cbca0c686ab2c4c1066.jpg","fullImage":"full-b2d914623be94cbca0c686ab2c4c1066.jpg"},{"image":"48a1c02d80dd49cab1fcacbe888a6fc6.jpg","smallImage":"small-48a1c02d80dd49cab1fcacbe888a6fc6.jpg","bigImage":"big-48a1c02d80dd49cab1fcacbe888a6fc6.jpg","fullImage":"full-48a1c02d80dd49cab1fcacbe888a6fc6.jpg"},{"image":"41c25d1a547b4e7583ed0a1798ea30c5.jpg","smallImage":"small-41c25d1a547b4e7583ed0a1798ea30c5.jpg","bigImage":"big-41c25d1a547b4e7583ed0a1798ea30c5.jpg","fullImage":"full-41c25d1a547b4e7583ed0a1798ea30c5.jpg"},{"image":"f41da2318e02464386797f7893bf48ec.jpg","smallImage":"small-f41da2318e02464386797f7893bf48ec.jpg","bigImage":"big-f41da2318e02464386797f7893bf48ec.jpg","fullImage":"full-f41da2318e02464386797f7893bf48ec.jpg"}]直接 <#assign images=数据库值 >然后 <#list images as image>image.image</#list>正确。而自己写了个macro 传入比如:<#macro images> <#list images as image>image.image</#list> </#macro>就不争取了,报错信息为:Expected collection or sequence. images evaluated instead to freemarker.template.SimpleScalar on line 509, column 11 in WEB-INF/macros/front/front.ftl. 问题补充:myali88 写道
解决方案
<#assign imgs = json?eval><#list imgs as image>${image.image}</#list><br/><#macro showProductImage images> <#list images as image>${image.image}</#list><br/></#macro> <@showProductImage images = imgs />终于找到问题了,上面的“json”我是从服务器端返回的,直接赋值时,“imgs”还是字符串,所以要加上“?eval”,这样就把imgs转成了list,后面遍历就没问题了。你试试。
解决方案二:
引用兄弟 是写错了,你要是知道你改下测试下,别总挑我的毛病啊,javaeye又不能修改。老兄,不是挑毛病,是怕因为你书写错误导致了错误,提醒你一下。我给的例子,你试过没,可以吧?
解决方案三:
<#assign images = [{"image":"5674e621e57a477eafacc38ae2b33123.jpg","smallImage":"small-5674e621e57a477eafacc38ae2b33123.jpg","bigImage":"big-5674e621e57a477eafacc38ae2b33123.jpg","fullImage":"full-5674e621e57a477eafacc38ae2b33123.jpg"}, {"image":"b2d914623be94cbca0c686ab2c4c1066.jpg","smallImage":"small-b2d914623be94cbca0c686ab2c4c1066.jpg","bigImage":"big-b2d914623be94cbca0c686ab2c4c1066.jpg","fullImage":"full-b2d914623be94cbca0c686ab2c4c1066.jpg"}, {"image":"48a1c02d80dd49cab1fcacbe888a6fc6.jpg","smallImage":"small-48a1c02d80dd49cab1fcacbe888a6fc6.jpg","bigImage":"big-48a1c02d80dd49cab1fcacbe888a6fc6.jpg","fullImage":"full-48a1c02d80dd49cab1fcacbe888a6fc6.jpg"}, {"image":"41c25d1a547b4e7583ed0a1798ea30c5.jpg","smallImage":"small-41c25d1a547b4e7583ed0a1798ea30c5.jpg","bigImage":"big-41c25d1a547b4e7583ed0a1798ea30c5.jpg","fullImage":"full-41c25d1a547b4e7583ed0a1798ea30c5.jpg"}, {"image":"f41da2318e02464386797f7893bf48ec.jpg","smallImage":"small-f41da2318e02464386797f7893bf48ec.jpg","bigImage":"big-f41da2318e02464386797f7893bf48ec.jpg","fullImage":"full-f41da2318e02464386797f7893bf48ec.jpg"} ]><#macro showProductImage images> <#list images as image>${image.image}</#list><br/></#macro><@showProductImage images = images /> 这样我测试过没问题。引用<#macro showProductImage imgas> <#list images as image>image.image</#list> </#macro>你这里的“imgas”和“images”写错了吧,对不上啊!
解决方案四:
<#macro images> <#list images as image>image.image</#list> </#macro>宏定义里面第一个是宏的名字,后面才是参数。你上面的定义根本就没有为宏定义参数。而你调用时取有传入参数: <@images images="${images}" /> 这不是有问题吗?
解决方案五:
定义就报错了?还是调用?你把images作为参数出入宏试试,是不是宏定义内,无法访问#assign定义的变量?