Grails GSP在脚本中访问变量 [英] Grails GSP accessing variables within script

查看:137
本文介绍了Grails GSP在脚本中访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了这个,但似乎无法找到正确的解决方案。我会贬低这一点,看看我错了。



我正在使用Grails作为项目,我非常喜欢它。我认为这是很难解决的是在GSP的。



有人可以告诉我给:

控制器:

  def index()
{
def message =这是一个测试
[message:message]
}

view

 <!DOCTYPE html> 
< html lang =en>
< head>
< script type =text / javascript>
$(document).ready(function(){

如何访问变量

});

< body>< / body>
< / html>

我试图提供一些第三方JavaScript库,并且遇到了一个难题。是的,我可以手动ajax或使用第三方库来调用ajax thingy。但是,我认为这是一个Grails的方式,所以我不必做太多繁重的工作。我的意思是说,你是否有机会在脚本上面访问它,并使所有的东西全球化?



我使用的示例库是 JQwidgets 库,它确实有ajax调用,但我宁愿将数组等通过Grails中的命名操作直接传递给它。

可以这样写:

 <!DOCTYPE html> 
< html lang =en>
< head>
< script type =text / javascript>
$(document).ready(function(){
var message =$ {message};
console.log(我来自GSP,message);
$(input [selector])。val(message); //只是一个哑例子
});
< / script>
< / head>
< body>
< / body>
< / html>


I have searched for this but cannot seem to find the right solution. I am going to dumb down this a bit and see where I am going wrong.

I am using Grails for a project and I am really enjoying it. Where I think it is hard to get around is in the GSP's.

Can someone tell me given:

Controller:

def index()
{
   def message = "This is a test"
   [message: message]
}

Within the view

<!DOCTYPE html>
<html lang="en">
<head>
    <script type="text/javascript">
        $(document).ready(function() {

             HOW DO YOU ACCESS THAT VARIABLE IN HERE 

        });

<body></body>
</html>

I am trying to feed some 3rd party javascript libs and I am having an issue trying to figure this out. Yes, I can manually ajax it or use the 3rd party library to call an ajax thingy. However, I thought there was a grails way for this so that I do not have to do so much heavy lifting. I mean are you pros out there accessing it above the script {} and making all things global? Just trying to figure out what the correct way of doing this is.

Example library I am using is the JQwidgets library which does have ajax calls, but I would rather pass in arrays and such directly into via named actions in Grails.

解决方案

You can write like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <script type="text/javascript">
        $(document).ready(function() {
            var message = "${message}";
            console.log("I'm from GSP", message);
            $("input[selector]").val(message);    // Just a dumb example
        });
    </script>
</head>
<body>
</body>
</html>

这篇关于Grails GSP在脚本中访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆