rendongtongfei
2011-08-10 22:48
浏览 250
已采纳

freemarker sequence 问题

数据库中存放
[code="java"]
[{"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"}
]
[/code]
直接 [code="java"]<#assign images=数据库值 >[/code]
然后 [code="java"]<#list images as image>image.image</#list>[/code]正确。

而自己写了个macro 传入
比如:[code="java"]<#macro images>
<#list images as image>image.image</#list>
</#macro>[/code]
就不争取了,报错信息为:
[code="java"]Expected collection or sequence. images evaluated instead to freemarker.template.SimpleScalar on line 509, column 11 in WEB-INF/macros/front/front.ftl.[/code]

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • myali88 2011-08-12 14:32
    已采纳

    [code="java"]
    <#assign
    imgs = json?eval

    <#list imgs as image>${image.image}</#list>

    <#macro showProductImage images>
    <#list images as image>${image.image}</#list>

    </#macro>

    <@showProductImage images = imgs />
    [/code]
    终于找到问题了,上面的“json”我是从服务器端返回的,直接赋值时,“imgs”还是字符串,所以要加上“?eval”,这样就把imgs转成了list,后面遍历就没问题了。你试试。

    打赏 评论
  • myali88 2011-08-10 23:13

    定义就报错了?还是调用?你把images作为参数出入宏试试,是不是宏定义内,无法访问#assign定义的变量?

    打赏 评论
  • myali88 2011-08-10 23:27

    [code="java"]
    <#macro images>
    <#list images as image>image.image</#list>
    </#macro>
    [/code]
    宏定义里面第一个是宏的名字,后面才是参数。你上面的定义根本就没有为宏定义参数。而你调用时取有传入参数:
    [code="java"] <@images images="${images}" /> [/code]
    这不是有问题吗?

    打赏 评论
  • myali88 2011-08-11 15:31

    [code="java"]
    <#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>

    </#macro>

    <@showProductImage images = images />

    [/code]
    这样我测试过没问题。

    [quote]
    [code="java"]
    <#macro showProductImage imgas>
    <#list images as image>image.image</#list>
    </#macro>
    [/code][/quote]
    你这里的“imgas”和“images”写错了吧,对不上啊!

    打赏 评论
  • myali88 2011-08-11 21:11

    [quote]兄弟 是写错了,你要是知道你改下测试下,别总挑我的毛病啊,javaeye又不能修改。[/quote]
    老兄,不是挑毛病,是怕因为你书写错误导致了错误,提醒你一下。我给的例子,你试过没,可以吧?

    打赏 评论

相关推荐 更多相似问题