如何将java变量传递给javascript

时间:2015-10-05 05:38:51

标签: javascript java variables

我想将一个java变量传递给javascript,我在jsp页面中没有body标签,然后我就不能这样通过了:

<body onload="start(<%=otp1%>);">

我需要运行这个javascript onload。有没有其他方法将java变量传递给javascript?我怎么能这样做?

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%
    String otp1 = (String) session.getAttribute("chartResult");
%>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Flot Examples: Basic Usage</title>

    <link href="../theme/bower_components/flot/examples/examples.css" rel="stylesheet" type="text/css">
    <%--   <link href="../theme/bower_components/flot/examples/examplesrtl.css" rel="stylesheet" type="text/css">--%>
    <%--    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../theme/bower_components/flot/excanvas.min.js"></script><![endif]-->--%>
    <script language="javascript" type="text/javascript" src="../theme/bower_components/flot/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="../theme/bower_components/flot/jquery.flot.js"></script><!-- jQuery -->
    <style type="text/css">
        body {
            font: 11px/1.1em "proxima-nova", Helvetica, Arial, sans-serif;
        }

        h2 {
            margin-top: 0;
        }
    </style>

   <script type="text/javascript" >
             function start(otp1){

                var d1 = [];
                for (var i = 0; i < 14; i += 0.5) {
                    d1.push([i, Math.sin(i)]);
                }

                var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];

                // A null signifies separate line segments

                var d3 = [[0, 12], [7, 12], null, [7, 2.5], [12, 2.5]];

                $.plot("#placeholder", [ d1, otp1, d3 ]);

                // Add the Flot version string to the footer

                $("#footer").prepend("Flot " + $.plot.version + " &ndash; ");
        }

    </script>


<div id="header" >
    <h2>Basic Usage</h2>
</div>

<div id="content" >
    <div class="demo-container">
        <div id="placeholder" class="demo-placeholder"></div>
    </div>

    <p>You don't have to do much to get an attractive plot. Create a placeholder, make sure it has dimensions (so Flot knows at what size to draw the plot), then call the plot function with your data.</p>

    <p>The axes are automatically scaled.</p>

</div>



<div id="footer">
    Copyright &copy; 2007 - 2014 IOLA and Ole Laursen
</div>

5 个答案:

答案 0 :(得分:1)

Javascript在客户端执行,java在服务器端执行。因此,一旦jsp / html页面从服务器加载到浏览器上,您就可以做到这一点。

您可以在jsp文件中执行此操作,但不能在js文件中执行,因为jsp文件在服务器端处理,然后将其作为html呈现给浏览器,而js文件在客户端执行,因此不会处理java代码

假设在服务器端将otp1设置为请求属性,您可以在jsp上访问它,如下所示,它更清晰,更易读

${requestScope.otp1}

答案 1 :(得分:0)

使用:

var jsVar="<%out.print(otp1);%>";

在你的javascript中

答案 2 :(得分:0)

您可以在页面上的脚本标记的开头处确定该Java值:

var value = <%=otp1%>;

或者,如果值为String:

var value = "<%=otp1%>";

答案 3 :(得分:0)

您可以将变量放在隐藏字段中,或者直接在js中建议使用slomek。作为onload-Method的替代,您可以使用jqueries ready方法。请参阅:https://api.jquery.com/ready/

答案 4 :(得分:0)

使用内联脚本例如:

 <script type="text/javascript" >

                var d1 = [];
                for (var i = 0; i < 14; i += 0.5) {
                    d1.push([i, Math.sin(i)]);
                }

                var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];

                // A null signifies separate line segments

                var d3 = [[0, 12], [7, 12], null, [7, 2.5], [12, 2.5]];

                $.plot("#placeholder", [ d1, <%=otp1%>, d3 ]);

                // Add the Flot version string to the footer

                $("#footer").prepend("Flot " + $.plot.version + " &ndash; ");

    </script>