javascript 将json数据从servlet传递到jsp到js文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14590598/
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
passing json data from servlet to jsp to js file
提问by PogoMips
I got this servlet which creates JSON data and I want to pass this data on to a jsp page which is supposed to display the data via the InfoVis toolkit.
我得到了这个创建 JSON 数据的 servlet,我想将此数据传递到一个 jsp 页面,该页面应该通过 InfoVis 工具包显示数据。
servlet.java
servlet.java
JSONObject json = new JSONObject();
JSONArray toplevel = new JSONArray();
JSONObject sublevel;
try{
json.put("id", "node" + 0);
json.put("name", "name" + 0);
int count = 5;
for(int i=1; i < count; i++){
sublevel = new JSONObject();
sublevel.put("id", "node" + i);
sublevel.put("name", "name" + i);
toplevel.put(sublevel);
}
json.put("children", toplevel);
} catch (JSONException jse) {
}
request.setAttribute("jsonString", json.toString());
RequestDispatcher dispatcher = request.getRequestDispatcher("graph.jsp");
dispatcher.forward(request, response);
The following Code is provided by the InfoVis Toolkit and I'm not sure if it can be changed. Or at least I don't have enough experience in JS to change it.
以下代码由 InfoVis Toolkit 提供,我不确定是否可以更改。或者至少我没有足够的 JS 经验来改变它。
graph.jsp
图.jsp
<body onload="init('${jsonString}');">
spacetree.js
空间树.js
function init(jsonString){
var json = jsonString;
Originally the function call is only
原来函数调用只是
<body onload="init()">
but the init() function has the JSON variable hardcoded, which is of course not useful at all. So I'm looking for a way to make that dynamic. But since theres quotations inside the string it now totally messes up the onload=init() function call..
但是 init() 函数具有硬编码的 JSON 变量,这当然根本没有用。所以我正在寻找一种方法来实现动态。但是由于字符串中有引号,它现在完全弄乱了 onload=init() 函数调用..
回答by T.J. Crowder
The cheap and easy way is to modify the JSP so it outputs this:
廉价且简单的方法是修改 JSP,使其输出:
<script>
var theData = ${jsonString};
</script>
<body onload="init(theData);">
The downside to that is that it creates a global variable, but if you're calling init
in that way, init
is already a global, so that ship has sailed. :-)
这样做的缺点是它创建了一个全局变量,但如果你init
以这种方式调用,init
它已经是一个全局变量,所以这艘船已经航行了。:-)
回答by Pointy
You don't have to drop the JSON as a string - it's valid JavaScript syntax:
您不必将 JSON 作为字符串删除 - 它是有效的 JavaScript 语法:
<body onload='init(${jsonString})'>
When that's rendered by JSP, the end result — the HTML sent to the browser — will be something like:
当它由 JSP 呈现时,最终结果 - 发送到浏览器的 HTML - 将类似于:
<body onload='init({"something": "some value", "whatever": "your data looks like"})'>
Now the only thing you may want to do is HTML encode the JSON, since you're dropping it as an HTML attribute value:
现在您可能唯一想做的就是对 JSON 进行 HTML 编码,因为您将其作为 HTML 属性值删除:
<body onload='init(${fn:escapeXml(jsonString)})'>
Then your "init" function can expect a ready-to-use JavaScript object, with no need to call a JSON parser at all.
然后你的“init”函数可以期待一个随时可用的 JavaScript 对象,根本不需要调用 JSON 解析器。