如何使用变量在 GSP 中引用 grails 地图实例
我将名为grossScores 的地图传递到GSP 中,并且需要使用GSP 中的另一个变量来引用地图的各个实例。例如,
grossScore.Bob = 5
发布如何引用grails GSP 模型变量间接例如通过 .get(...) 很有帮助,但我仍然无法到达那里。
我试过了:
${grossScore."{$player}"}
${pageScope.getProperty("grossScore.${player}")}
${request.getAttribute("grossScore.${player}")}
有什么建议吗?
I'm passing a map named grossScores into a GSP and need to reference individual instances of the map using another variable in the GSP. For example,
grossScore.Bob = 5
The posting How to reference a grails GSP model variable indirectly e.g. via .get(...) is helpful but I still couldn't get there.
I've tried:
${grossScore."{$player}"}
${pageScope.getProperty("grossScore.${player}")}
${request.getAttribute("grossScore.${player}")}
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
假设地图的名称是grossScores,如您的问题所示。
Try:
assuming the name of the map is grossScores, as in your question.
您在第一次尝试时放错了 $ 符号。也可以引用如下值:
但是请注意,如果您在 grails 标记的属性中使用此构造,则此解决方案会产生问题,例如:
将不起作用,并且
playerScores
将不被设置。但是根本没有显示任何语法错误,该属性只是被忽略。这可能会导致很多混乱。如果你想使用这种写法,那么你需要使用不同的引号:You misplaced the $-symbol in your first try. It is also possible to reference the value like:
Note, however, that this solution will create problems, if you use this construct within an attribute of a grails tag, e.g.:
Will NOT work, and
playerScores
will NOT be set. However there is NO syntax error displayed at all, the attribute is just ignored. This can lead to a lot of confusion. If you want to use this kind of writing, then you need to use different quotes: