groovy.langMissingMethodException 在调用 groovy 函数

groovy.langMissingMethodException in calling groovy function

我有一个类似于此的 Grails GSP page

<!DOCTYPE html>
<html lang="en">
    <head>
        ...
        <script type="text/javascript">
            var currency = ${Currency.getList() as JSON};
        </script>
    </head>
    ...
</html>

还有一个类似于此的 groovy 文件:

package enums
import java.util.List;

public final class Currency {
    private static final List CURRENCIES =  ['PHP','USD','HKD'];
    static List getList(){
        return CURRENCIES
    }
}

现在显示 error 消息:

groovy.lang.MissingMethodException

No signature of method: static java.util.Currency.getList() is applicable for argument types: () values: [] Possible solutions: getAt(java.lang.String), getClass(), split(groovy.lang.Closure), notify(), wait()

我该如何解决这个问题? groovy 文件放在 project/src 目录中,不允许移动(如果有帮助的话)。

由于您从 HTML 内部调用静态 groovy 函数,我怀疑您需要将修饰符 "public" 添加到静态方法中,因此:

public static List getList()

编辑:以上不是问题,但异常抱怨来自 java.util 包的 class 货币,而不是来自您自己的包 "enums".

根据错误消息 您正在使用与 java.util 包不同的 Currency class


因此请尝试使用您自己的 class,即 enums.Currency.getList() 而不是 java.util.Currencty.getList()


更新:

同时导入 JSON class。根据您的评论,我的回答对您来说似乎不清楚。所以你的代码将是这样的:

package enums
import java.util.List;
import grails.converters.JSON;

public final class Currency {
    private static final List CURRENCIES =  ['PHP','USD','HKD'];
    static List getList(){
        return CURRENCIES
    }
}