目前,我有一个转发到 jsp 的 servlet。 jsp 可以访问 session 属性“current”。 在加载 jsp 期间,“current”中的信息被传递到生成图表的 javascript 函数。 这一切工作正常。我唯一的问题是我正在对图形数据进行硬编码。
我如何将数据数组从 servlet 传递到 jsp。基本上,在 creerRapport 函数的第五个参数中, 如何用 java 属性替换它?
任何帮助或想法将不胜感激。
我当前的代码包含硬编码数据。
<body onload="soumettreRapport();">
<script type="text/javascript">
function soumettreRapport() {
creerRapport( "${current.title}",
"${current.type}",
${current.width},
${current.height},
[
{
key: "Cumulative Return",
values: [
{
"label" : "2001" ,
"value" : -29.76
} ,
{
"label" : "2002" ,
"value" : 0
} ,
{
"label" : "2003 ,
"value" : 32.80
}
]
}
]
);
return false;
}
最佳答案
在 Servlet 中,您需要将 JSON 数组作为字符串,然后将此字符串放入请求范围。
String jsonArrayString = convert(...); // Output [{key:"Cumulative Return", .... }]
request.setAttribute("jsonArrayString", jsonArrayString);
在 JSP 中:
function soumettreRapport() {
var jsonArray = ${jsonArrayString};
creerRapport( "${current.title}",
"${current.type}",
${current.width},
${current.height}, jsonArray );
}
关于java - 将 java 属性传递给 javascript 函数数组参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940884/