繁体   English   中英

如何将Java变量传递给javascript

[英]how to pass a java variable to javascript

我想将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>

Javascript在客户端执行,而Java在服务器端执行。 因此,一旦从服务器将jsp / html页面加载到浏览器中,就可以执行此操作。

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

假设在服务器端将otp1设置为request属性,则可以像下面这样在jsp上访问它,它更清晰易读

${requestScope.otp1}

采用 :

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

在你的JavaScript中

您可以在页面上脚本标记的开头添加该Java值:

var value = <%=otp1%>;

或者,如果值为String:

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

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

使用内联脚本,例如:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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