如何间接地引用Grails GSP模型变量通过.get(...) [英] How to reference a grails GSP model variable indirectly e.g. via .get(...)
问题描述
我正在使用GSP发送基于MailService插件的电子邮件。 sendMail关闭通过(其中包括) body(view:...,model:myModel)
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)
我知道我可以使用GSP中的 $ {itemName}
访问myModel Map中的每个项目。然而,由于我有时想像'item'+ i
动态地构建项目名称,所以我需要一些周边的方法来访问变量。
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.
我已经尝试过 $ {model.get('item'+ i)
和 $ {params.get 'item'+ i)
,但是model为null,params是一个空的Map。我也尝试过pageScope,但是虽然我可以通过 $ {pageScope.itemName
访问一个项目,但我不能使用 $ {pageScope.get('item '+ i)}
因为pageScope不是地图。
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.
可能有多种解决方案可以解决这个问题。我会很高兴一个容易的一个;-)。我看到的一个解决方案是将myModel作为唯一参数,然后始终使用 myModel.get(...)
,但这意味着我不得不更改我现有的所有GSP始终引用myModel而不是直接访问项目(具有固定名称);所以如果有一种方式,我不必改变传递给GSP的模式,这将是我最喜欢的。
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!
推荐答案
我现在使用 $ {pageScope.getProperty(...)}
这篇关于如何间接地引用Grails GSP模型变量通过.get(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!