简体   繁体   English

grails gsp g:设置反问题

[英]grails gsp g:set counter problem

I'm trying to sum up a set of values using the g:set example from the Grails 1.3.7 docs but it looks like g:set is treating the integers as strings. 我试图使用Grails 1.3.7文档中的g:set示例来总结一组值,但它看起来像g:set将整数视为字符串。

With values for ${grossScore.score} of [ 5, 5, 4, 4 ] instead of the total being 18, the total is 5544. 使用[{5,5,4,4]的$ {grossScore.score}值而不是总数为18,总数为5544。

<g:set var="totalScore" value="0"/>
<g:each var="grossScore" in="${Score.findAllByScorecard(cardGross)}">
    <g:set var="totalScore" value="${totalScore + grossScore.score}"/>
</g:each>

Another option is to ensure that totalScore is an integer, like this: 另一个选择是确保totalScore是一个整数,如下所示:

<g:set var="totalScore" value="${0}"/>

I believe this will force totalScore to be an Integer, so you won't have to worry about concatenation instead of addition. 我相信这会强制totalScore成为一个整数,所以你不必担心连接而不是添加。

If you KNOW that the grossScore.score value is an integer, you can probably just swap the two in your addition: 如果你知道grossScore.score值是一个整数,你可以在你的补充中交换这两个:

<g:set var="totalScore" value="${grossScore.score + totalScore}"/>

Usually, the left-hand of an operation determines the type of operation, assuming such exists. 通常,操作的左手确定操作的类型,假设存在这种操作。 Since you have totalScore on the left, and it's just an Object (default for ag:set), the default Object.plus() operation is used, which is very similar to String. 由于左侧有totalScore,而且它只是一个Object(ag:set的默认值),因此使用默认的Object.plus()操作,这与String非常相似。

If you make grossScore.score the left hand side, then it should try to use Integer.plus(), which should give you what you want. 如果你把grossScore.score放在左边,那么它应该尝试使用Integer.plus(),它可以给你你想要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM