循环遍历没有“值”的集合; Tapestry 中的属性
<t:loop source="pageNames" value="pageName">
<td class="${tabClass}">
<t:pagelink page="pageName">${pageName}</t:pagelink>
</td>
</t:loop>
,其中 pageName 变量是从组件中获取的:
@Property
private String _pageName;
我没有使用这样的属性。从组件中删除声明会使 Tapestry 感到难过并抛出类似于以下内容的异常
无法将“pageName”转换为组件参数绑定。类...不包含名为“pageName”的属性(在属性表达式“pageName”内)。可用属性:...
如何在不声明循环值属性的情况下使用循环标记?
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 pageName 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不必指定
t:value
,只需将其省略即可。在某些情况下,您只想使用t:index
,这完全没问题。根据评论进行编辑:
是的,没有办法绕过在组件类中声明属性。当您不对组件类中的当前迭代值执行任何操作时,它可能看起来有点不优雅,这是事实。在这种情况下,我倾向于使用 getter 和 setter 而不是 @Property 注释,以避免“未使用”编译器警告。
I don't think you have to specify
t:value
, just leave it out. In some cases, you just want to uset: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.您可以在模板中为临时变量添加 var 前缀。因此,在您的情况下,您只需说
value="var:pagename"
即可。如果 pagename 是一个基元(例如字符串或整数),并且您可以使用以下语法${var:pagename}
在循环中的任何位置访问其值,则此方法可以正常工作。但是,当您尝试访问其属性(例如pagename.name
)时,它会失败。请查看此处,获取可用绑定表达式的列表,以查看其他位置(例如消息目录),您可以从中提取值。
或者,您始终可以在页面类中使用
@SuppressWarnings("unused")
来抑制编译器警告。干杯。
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 aspagename.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.