开发者

Loop through a collection without a 'value' property in Tapestry

The Tapestry loop documentation shows the following example

<t:loop source="pageNames" value="pageName">
    <td class="${tabClass}">
        <t:pagelink page="pageName">${pageName}</t:pagelink>
    </td>
</t:loop>

where the 开发者_如何学GopageName variable is picked up from the Component:

@Property
private String _pageName;

I have no use for such a property. Removing the declaration from the Component makes Tapestry sad and throw an Exception similar to

Could not convert 'pageName' into a component parameter binding. Class ... does not contain a property named 'pageName' (within property expression 'pageName'). Available properties: ...

How can I use a loop tag without declaring a property for the loop value?


I don't think you have to specify t:value, just leave it out. In some cases, you just want to use t:index, that's totally fine.

Edit based on comment:

Yes, there is no way around declaring a property in the component class. It can look slightly inelegant when you don't do anything with the current iteration value inside the component class, that's true. I tend to use getters and setters instead of the @Property annotation in such cases to avoid the "unused" compiler warning.


You can prefix the temporary variable in the template with the var prefix. So in your case, you would just say value="var:pagename". This works fine if pagename is a primitive such as a string or integer and you can access its value anywhere in the loop using the following syntax ${var:pagename}. It fails however when you try to access its properties, such as pagename.name.

Have a look here for a list of available binding expressions to see other places (such as message catalogs) where you can pull values from.

Or you can always use @SuppressWarnings("unused") in your page class over the property to supress the compiler warning.

Cheers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜