2014-01-06 36 views
0

目前,我有一个转发给jsp的servlet。 jsp可以访问会话属性“current”。 在加载jsp期间,“当前”中的信息被传递给一个生成图的javascript函数。 这一切工作正常。我唯一的问题是我很难编码图形数据。将java属性传递给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; 
} 

回答

1

在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); 

} 
+0

这正是它。谢谢。 – LatinCanuck