循环遍历没有“值”的集合; Tapestry 中的属性

发布于 2024-09-19 09:36:38 字数 771 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-09-26 09:36:38

我认为您不必指定 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 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.

才能让你更想念 2024-09-26 09:36:38

您可以在模板中为临时变量添加 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文