如何在gsp中引用静态常量?

时间:2014-05-21 23:35:53

标签: grails gsp

我想在GSP文件中的java类AppConstant中引用静态字符串。我该怎么办? AppConstant就像这样

public class AppConstant {
    public static final String ROLE_ADMIN = "ROLE_ADMIN";
}

假设java类位于com.foo.app包中。谢谢!

1 个答案:

答案 0 :(得分:4)

做这种事情并不常见,但要回答问题,你可以这样做......

在Java或Groovy类中定义常量......

// src/groovy/com/demo/AppConstant.groovy
package com.demo

class AppConstant {
    static final SOME_CONSTANT = 'Neil Peart'
}

然后你可以直接从GSP中引用它们......

<html>
    <head>
        <meta name="layout" content="main"/>
        <title>Constant Demo</title>
    </head>
    <body>
        ${com.demo.AppConstant.SOME_CONSTANT}
    </body>
</html>

我希望有所帮助。