Closure返回其引用/指针而不是返回值

时间:2016-03-02 19:34:25

标签: grails groovy

当我输出一个属性时,我想我得到了闭包的引用/指针而不是预期的返回值。

例如,对于以下属性:${g.goalsAgainstAverage}

<td header="stathdr5 stathdr${g.leagueId} stathdrgt${g.leagueId}_${g.gameTypeId}">${g.goalsAgainstAverage}</td>

将输出:

<td header="stathdr5 stathdr1 stathdrgt1_3">com.mgs.StatService$__tt__getSeasonStats_closure10$_closure24$_closure25@61df269d</td>

以下是生成值的服务(请参阅calcGoalsAgainstAverage):

import grails.transaction.Transactional

@Transactional
class StatService {

    def serviceMethod() {

    }

    def getSeasonStats(Long userId, Long seasonId){

        def seasonRaw = Game.createCriteria().list{
            ...
        }

        def seasonStats = seasonRaw
        .groupBy { it.leagueId }
        .collect { leagueId, records -> 
            [
                ...
                gameTypes: records.collect { 
                    [
                        ...
                        goalsAgainstAverage: {calcGoalsAgainstAverage(it.secondsPlayed, it.minutesPlayed, it.gameInMinutes, it.goalsAgainst)}
                    ] 
                }
            ]
        }
        return seasonStats
    }

    double calcGoalsAgainstAverage(int secondsPlayed, int minutesPlayed, int gameInMinutes, int goalsAgainst){
        ...
    }
}

1 个答案:

答案 0 :(得分:4)

你回来了......

要么改变:

goalsAgainstAverage: {calcGoalsAgainstAverage(it.secondsPlayed, it.minutesPlayed, it.gameInMinutes, it.goalsAgainst)}

goalsAgainstAverage: calcGoalsAgainstAverage(it.secondsPlayed, it.minutesPlayed, it.gameInMinutes, it.goalsAgainst)

或者,改变

${g.goalsAgainstAverage}

${g.goalsAgainstAverage()}
相关问题