如何间接引用 grails GSP 模型变量,例如通过 .get(...)

发布于 2024-09-25 08:28:54 字数 723 浏览 11 评论 0原文

我正在使用 GSP 基于 MailService 插件发送电子邮件。 sendMail 闭包通过(除其他外) body(view:..., model:myModel)

我知道我可以使用 ${itemName} 访问 myModel 映射的每个项目普惠制中的代码>。然而,由于我有时想像 'item'+i 一样动态构建项目名称,因此我需要一些周围的方法来访问该变量。

我已经尝试过 ${model.get('item'+i)${params.get('item'+i),但 model 为 null 并且 params是一个空地图。我也尝试过 pageScope,但尽管我可以通过 ${pageScope.itemName 访问项目,但我无法使用 ${pageScope.get('item'+i)}因为 pageScope 不是 Map。

可能有多种解决方案可以解决这个问题;我很高兴能有一个简单的;-)。我看到的一种解决方案是将 myModel 作为唯一参数传递,然后始终使用 myModel.get(...),但这意味着我必须更改所有现有的 GSP 以始终引用 myModel而不是直接访问项目(具有固定名称);因此,如果有一种方法可以让我不必更改传递给 GSP 的模型,那么这将是我最喜欢的。

如果有人也可以在这种情况下就模型和参数的差异说几句话,这将会额外有帮助!

I'm using a GSP for sending out emails based on the MailService plug-in. The sendMail closure passes (amongst others) body(view:..., model:myModel)

I know that I can access every item of the myModel Map just using ${itemName} in the GSP. However as I sometimes want to build the item name dynamically like 'item'+i, I need to have some surrounding method to access the variable.

I already tried ${model.get('item'+i), and ${params.get('item'+i), but model is null and params is an empty Map. I also tried pageScope, but though I can access an item via ${pageScope.itemName, I can not use ${pageScope.get('item'+i)} because pageScope is not a Map.

Probably there are multiple solutions to solve this; I'd be glad about an easy one ;-). One solution I see is to pass myModel as the only parameter and then always use myModel.get(...), however this would mean that I had to change all my existing GSPs to always refer to myModel instead of accessing items (with fixed names) directly; so if there's a way where I don't have to change the model passed to the GSP, this would be my favorite.

If someone could also say a few words about the difference of model and params in this context, this would be additionally helpful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

朕就是辣么酷 2024-10-02 08:28:54

我现在已经使用 ${pageScope.getProperty(...)} 对其进行了管理。

I've managed it now using ${pageScope.getProperty(...)}.

不再让梦枯萎 2024-10-02 08:28:54

没有“模型”范围或变量。相反,模型映射中的对象被设置为请求属性,以使它们可供 GSP 使用。这是 Spring 的一项功能,可以使用 JSTL 轻松访问 JSP 中的变量,并且由于 GSP 语法与 JSTL 非常相似,因此在 Grails 中的工作方式相同。

所以你可以使用这个:

${request.getAttribute('item'+i)}

使用动态名称访问模型变量。

There's no 'model' scope or variable. Instead objects in the model map are set as Request attributes to make them available to the GSP. This is a Spring feature which makes it easy to access variables in JSPs using JSTL and since the GSP syntax is very similar to JSTL it works the same way in Grails.

So you can use this:

${request.getAttribute('item'+i)}

to access model variables using dynamic names.

芯好空 2024-10-02 08:28:54

您可以使用 ${fieldValue(bean: book, field: 'title')}

参见:http://grails.github.io/grails-doc/latest/ref/Tags/fieldValue.html

You can use ${fieldValue(bean: book, field: 'title')}

See: http://grails.github.io/grails-doc/latest/ref/Tags/fieldValue.html

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