如何在javascript文件中访问Spring MVC模型对象? [英] How to access Spring MVC model object in javascript file?

查看:174
本文介绍了如何在javascript文件中访问Spring MVC模型对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring 3 MVC而且我有以下课程。

I am using spring 3 MVC and i have below classes.

外部系统将使用以下URL调用我的应用程序:

External system would call my application using below URL:

http://somehost/root/param1/param2/param3

我有一个spring MVC控制器方法如下:

I have a spring MVC controller method as below:

public ModelAndView showPage(@PathVariable("param1") String paramOne, @PathVariable("param2") String paramTwo, @PathVariable("param3") String paramThree, HttpServletResponse response) {  
        SomeModel model = new SomeModel(paramOne, paramTwo, paramThree);
       return new ModelAndView("SomeJsp", "model", model);
    } 



SomeModel.java



SomeModel.java

public class SomeModel{
 private String paramOne;
 private String paramTwo;
 private String paramThree;
//constructor
 //setters and getters

}



SomeJsp.jsp



SomeJsp.jsp

//In this Jsp i have a div with few elements. Div is not visible by default.
//This jsp has externalJavascript included.
//I enable div and set the data into div elements using jquery.
<script src="<c:url value="/resources/js/extjs.js" />" type="text/javascript"></script>



externalJs.js



externalJs.js

$(document).ready(function() {

    //Here i need the model returned using ModelAndView
//I can get data from model and set into div elements.


});

在外部java脚本文件中,是否可以获取模型内容?如果可能的话,我该怎么做?

In external java script file, is it possible to get the model content? If possible, how can i do that?

谢谢!

推荐答案

JavaScript在客户端运行。您的模型在客户端不存在,它只在您呈现.jsp时存在于服务器端。

JavaScript is run on the client side. Your model does not exist on the client side, it only exists on the server side while you are rendering your .jsp.

如果您希望模型中的数据可用对于客户端代码(即javascript),您需要将其存储在呈现页面中的某个位置。例如,您可以使用Jsp编写JavaScript,将模型分配给JavaScript变量。

If you want data from the model to be available to client side code (ie. javascript), you will need to store it somewhere in the rendered page. For example, you can use your Jsp to write JavaScript assigning your model to JavaScript variables.

更新:

一个简单的例子

<%-- SomeJsp.jsp --%>
<script>var paramOne =<c:out value="${paramOne}"/></script>

这篇关于如何在javascript文件中访问Spring MVC模型对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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