简体   繁体   中英

Passing java attribute to a javascript function array argument

Currently, I have a servlet that forwards to a jsp. The jsp has access to a session attribute "current". During the loading of the jsp, the information in "current" is passed to a javascript function that generates a graph. This is all working fine. My only problem is that i'm hard coding the graph data.

How would i go about passing the data array from the servlet to the jsp. Basically, in the creerRapport function, in the 5th argument, how do I replace that with java attributes?

Any help or ideas would be appreciated.

My current code with hard coded data.

<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;
}

In Servlet, You need to to have JSON array as String then put this String into Request scope.

String jsonArrayString = convert(...); // Output [{key:"Cumulative Return", .... }]

request.setAttribute("jsonArrayString", jsonArrayString);

In JSP:

function soumettreRapport() {

    var jsonArray = ${jsonArrayString};

    creerRapport( "${current.title}",
                  "${current.type}",                                    
                   ${current.width},
                   ${current.height}, jsonArray );

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM