I'm passing a map named grossScores into a GSP and need to refe开发者_StackOverflow社区rence 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?
Try:
${grossScores.get(player)}
assuming the name of the map is grossScores, as in your question.
You misplaced the $-symbol in your first try. It is also possible to reference the value like:
${grossScore."${player}"}
Note, however, that this solution will create problems, if you use this construct within an attribute of a grails tag, e.g.:
<g:set var="playerScores" value="${grossScore."${player}"}" />
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:
<g:set var="playerScores" value='${grossScore."${player}"}' />
精彩评论