如何在 JavaScript 中访问 Grails 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8182054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access a Grails variable in JavaScript?
提问by junaidp
I have a variable in my Grails app's BootStrap.groovy
:
我的 Grails 应用程序中有一个变量BootStrap.groovy
:
class BootStrap {
def init = { servletContext ->
def testGrails = "11"
I want to show a JavaScript alert()
if this test value is 11.
alert()
如果此测试值为 11,我想显示 JavaScript 。
My JavaScript:
我的 JavaScript:
<SCRIPT LANGUAGE="JavaScript">
if(testGrails==11) // WHAT TO WRITE WITH IN THE BRACKETS ...?
{
alert("yes");
}
How can I access a Grails class in Javascript to do the above?
如何在 Javascript 中访问 Grails 类来执行上述操作?
Thanks.
谢谢。
回答by Igor Artamonov
First of all - you have to make this variable accessible from other places. Currently it's bounded to init
scope. You can put it into global config for example (see Config.groovy
). Or set to an service. Or make an public static
variable somewhere.
首先 - 你必须让这个变量可以从其他地方访问。目前它受init
范围限制。例如,您可以将其放入全局配置中(请参阅 参考资料Config.groovy
)。或设置为服务。或者在public static
某处创建一个变量。
Example for a service:
服务示例:
class VariableHOlderService {
def testGrails
}
and
和
class BootStrap {
VariableHolderService variableHolderService
def init = { servletContext ->
VariableHolderService.testGrails = "11"
}
Second - you need to put it into request. There is two ways - use filter or controller/action. First option is useful when you want to use same variable from different GSP.
其次 - 您需要将其放入请求中。有两种方法 - 使用过滤器或控制器/动作。当您想使用来自不同 GSP 的相同变量时,第一个选项很有用。
From controller it would be:
从控制器它将是:
VariableHolderService variableHolderService
def myAction = {
render model: [testGrails:variableHolderService.testGrails]
}
and use it at GSP as
并在 GSP 使用它作为
<g:javascript>
if(${testGrails}==11)
{
alert("yes");
}
</g:javascript>
回答by socha23
You should define your variable in Config.groovy, not in Bootstrap. Then you can access it in gsp files like this:
您应该在 Config.groovy 中定义变量,而不是在 Bootstrap 中。然后你可以在 gsp 文件中访问它,如下所示:
<SCRIPT LANGUAGE="JavaScript">
if(${grailsApplication.config.testGrails}==11)
{
alert("yes");
}
回答by mathifonseca
Are you sure you need it in Bootstrap.groovy
? Is it something you calculate or can change?
你确定你需要它Bootstrap.groovy
吗?它是你计算的还是可以改变的?
If your answer is no, I found that the meta
tag is very useful for getting information in GSP files.
如果您的回答是否定的,我发现该meta
标签对于获取 GSP 文件中的信息非常有用。
For example, if you want your application name, you can get it like this:
例如,如果你想要你的应用程序名称,你可以这样得到:
<g:meta name="app.name"/>
You can get any property in your application.properties
file like that.
您可以application.properties
像这样获取文件中的任何属性。
And if you, like me, need to concatenate it to another value, here is my example. Remember that any tag can be used as a method without the g:
namespace. For example:
如果你和我一样,需要将它连接到另一个值,这是我的例子。请记住,任何标记都可以用作没有g:
命名空间的方法。例如:
<g:set var="help" value="http://localhost:8080/${meta(name:"app.name")}/help" />
Grails documentation about this is a little poor, but it is here.
Grails 关于这个的文档有点差,但它在这里。