开发者

How to write Javascript from scriptlet to file?

开发者 https://www.devze.com 2023-03-15 08:16 出处:网络
I have a series of JSPs, each of which contains a scriptlet for generating code for visualisations using Google\'s Chart Tools. These are called dynamically at run time to display visualisations.

I have a series of JSPs, each of which contains a scriptlet for generating code for visualisations using Google's Chart Tools. These are called dynamically at run time to display visualisations. The following edited code is typical:

    <-- HTML of JSP here -->

    <script type = "text/javascript" src = "http://www.google.com/jsapi"></script>

    <script type = "text/javascript">

       google.load("visualization", "1", {packages:["corechart"]});
       google.setOnLoadCallback(drawChart);

       out.println("data.addRows(" + pv.getNoRows()  + ");");

       function drawChart() {
            var data = new google.visualization.DataTable();

        <%   
            int row = 0;
            List<List<String>> datasetVisData = pv.getDatasetVisData();
            ArrayList<String> datasetVisTemporalAxisLabels = pv.getDatasetVisTemporalAxisLabels();
            for (List<String> list : datasetVisualisationData) {                    
                out.println("data.setValue(" + row + ", " + 0 + ", '" + datasetVisTemporalAxisLabels.get(row) + "');");                   
                for (int column = 0; column < list.size() - 1; column++) {                       
                    out.println("data.setValue(" + row + ", " + (column + 1) + ", " + list.get(column) + ");");
                }
                row++;
            }
        %>
     }

     </script>

     <-- HTML of JSP here -->

This scriptlet produces Javascript as follows:

data.addRows(2);
data.setValue(0, 0, '22 06 2011 @ 10 00');
data.setValue(0, 1, 10);
data.setValue(0, 2, 10);
data.setValue(0, 3, 10);
data.setValue(0, 4, 15);
data.setValue(0, 5, 10);
data.setValue(1, 0, '22 06 2011 @ 12 00');
data.setValue(1, 1, 5);
data.setValue(1, 2, 4);
data.setValue(1, 3, 3);
data.setValue(1, 4, 2);
data.setValue(1, 5, 2);

And so on so that the request can be made to Google to render the visualisation at run time.

But my question takes the above is a slightly different direction, i.e. what if I want to divert the output of the above to a .html file which can be stored in the file system and then incorporated into a JSP request made later?

Can anyone suggest what might be a useful way to do this? The only thing I can think of at the moment is to generate the HTML and embedded Javascript within Java code and write the output to a text file, e.g.

    System.out.println("<p></form>\n" +
    System.out.println("<p><input type = \"submit\" value = \"Plot options\"></form>\n" +
                       "</fieldset><p>\n<p>&nbsp;</p>");
    System.out.println("<script language = \"javascript\">");
    System.out.println("document.plotcriteria.reset();");
    System.out.println("</script>");

Can anyone suggest a better alternative? Is it possible to divert the output of a JSP to file system rather than client browser?

Thanks

开发者_开发百科

Mr Morgan.


I assume the reason you want JSP is because you want a template.

Take a look at freemarker, it much more fit to purpose to what you want to do. http://freemarker.sourceforge.net/

Could you use a JSP? yes you could compile the JSP to a Java class using the Jasper compiler ( http://tomcat.apache.org/tomcat-5.5-doc/jasper/docs/api/org/apache/jasper/compiler/Compiler.html ) You now have a java servlet, on which you invoke the service or doGet method, passing in a ServletResponse implementation where getOutputStream() returns a file stream.

0

精彩评论

暂无评论...
验证码 换一张
取 消