javascript 在 jquery 中访问 Spring MVC 模型对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28973620/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:51:21  来源:igfitidea点击:

Access Spring MVC model object within jquery

javascriptjqueryspringspring-mvcjaxb

提问by Neero

This is how I return my model object from the controller.

这就是我从控制器返回模型对象的方式。

model.addObject("gameSchList", gameSchList);
return model;

I got my response as a JSON output because I used Spring JAXB implementation. This is the output I got from my Spring controller.

因为我使用了 Spring JAXB 实现,所以我得到了 JSON 输出的响应。这是我从 Spring 控制器得到的输出。

{
   "gameSchList":
   [
       {
           "team":
           [
               {
                   "alias": "A",
                   "logo": "A_LOGO"
               },
               {
                   "alias": "B",
                   "logo": "B_LOGO"
               }
           ],
           "cName": "AZ",
           "id": "58",
           "type": "game",
           "channel": "713",
           "time": "Sun 13:00PM",
           "status": "pregame"
       }
   ]

}

}

Now I want to access this gameSchList within document ready function. This is an external js file which I added to my homepage.

现在我想在文档就绪函数中访问这个 gameSchList。这是我添加到主页的外部 js 文件。

Now within that gameinfo.js, I created a ready function and tried to access gameSchList using the below code. I need this requirement within js file without using jstl.

现在在那个 gameinfo.js 中,我创建了一个就绪函数并尝试使用以下代码访问 gameSchList。我在 js 文件中需要这个要求而不使用 jstl。

$(document).ready(function(){
    var list = ${gameSchList};
    $.each(list, function( index, value ) {
        alert( index + ": " + value );
    });
});

When I call this controller it redirects to my homepage and executes this particular gameinfo.js, but it gives me this error:

当我调用这个控制器时,它会重定向到我的主页并执行这个特定的 gameinfo.js,但它给了我这个错误:

SyntaxError: missing ; before statement

语法错误:缺少;声明前

I tried it several ways, but no luck for me.

我尝试了几种方法,但对我来说没有运气。

var list = ${model.gameSchList};
var list = "${model.gameSchList}";

回答by minion

Here is one solution, that you can try. In your jsp declare the below which gets evaluated on server side (make sure its in jsp and not on external js file).

这是一种解决方案,您可以尝试一下。在您的 jsp 中声明以下在服务器端进行评估的内容(确保它在 jsp 中而不是在外部 js 文件中)。

<script type="text/javascript">
 var responseObject= ${gameSchList};
</script>

Now you have access to responseObject on js and you can use it on your document.ready function. Hope this helps.

现在您可以访问 js 上的 responseObject,并且可以在您的 document.ready 函数中使用它。希望这可以帮助。

回答by Denys Zotov

On JSP page, which compiled on server we can declared JavaScript object only in JSON format in the with function which will convert the JSON content to JavaScript object when browser downloaded and compiled this script:

在服务器上编译的 JSP 页面上,我们只能在 with 函数中声明 JSON 格式的 JavaScript 对象,该函数将在浏览器下载并编译此脚本时将 JSON 内容转换为 JavaScript 对象:

<script>
 var object = JSON.parse('${data}');
<script/>

other way it download JavaScript object through Ajax request from Server, the browser automatically compile JSON content received from server to the JavaScirpt object:

另一种方式是通过来自服务器的 Ajax 请求下载 JavaScript 对象,浏览器自动将从服务器接收的 JSON 内容编译为 JavaScirpt 对象:

.....
 sendRequest("moderatorBuildingEntrancePage/updateBuildingLabel/ajax",sendData,
     function(event, request, settings) {
           var recievedObject = event.result 
....