Search code examples
grailsgsp

How to refer static constants in gsp?


I wanna refer a static string in a java class AppConstant in a GSP file. What should I do? The AppConstant is like this

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

Assume that the java class is in the com.foo.app package. Thanks!


Solution

  • It isn't very common to do this sort of thing but to answer the question as asked, you can do something like this...

    Define the constants in a Java or Groovy classs...

    // src/groovy/com/demo/AppConstant.groovy
    package com.demo
    
    class AppConstant {
        static final SOME_CONSTANT = 'Neil Peart'
    }
    

    Then you can refer to them directly from a GSP...

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

    I hope that helps.