javascript 刷新包含 JSP 代码片段的 DIV 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4313203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:58:39  来源:igfitidea点击:

Refreshing a DIV element which has JSP code snippet inside

javascriptjsphtmlreload

提问by User 1034

I have a JSP page where I am reading Session Attributes that I set in the Session.

我有一个 JSP 页面,我正在读取我在会话中设置的会话属性。

I want to read the Session attributes in regular intervals. I don't want to reload the whole page instead I am just keeping my JSP Session read attributes in my DIV and trying to reload the DIV. But it is not reloading the DIV.

我想定期读取会话属性。我不想重新加载整个页面,而是将我的 JSP 会话读取属性保留在我的 DIV 中并尝试重新加载 DIV。但它没有重新加载 DIV。

Here is my code base:

这是我的代码库:

<html>
  <head>
  // Loading CSS and Script files
  </head>
  <body>
    <div id="loadData" style='display:none;'>
         <% 
           String strStatus = String.valueOf(session.getAttribute("Status")) ;
          %>
    </div>
  </body>
  <script type="text/javascript">
    var reqStatus = '<%= strStatus %>';

    $(this).load(function(){
       setInterval(function() {
            $("#loadData").load();
          } ,1000);
     });
     $("#loadData").load(function(){
        if(reqStatus == 'Done') {
        // My Code goes here..
        }
     });
</html>

Any better ideas are also welcome.

任何更好的想法也欢迎。

采纳答案by David Tang

JSP renders once, on the server, and is then sent to the client, after which the Java code does nothing. You can't put both the HTML/javascript code and the Java code in the same file if you want them to be loaded at different times / frequencies.

JSP 在服务器上呈现一次,然后发送到客户端,之后 Java 代码什么也不做。如果您希望它们以不同的时间/频率加载,则不能将 HTML/javascript 代码和 Java 代码放在同一个文件中。

Put this into a separate .jsp file:

把它放到一个单独的 .jsp 文件中:

<%= String.valueOf(session.getAttribute("Status")) ; %>

Assume it's mapped to some url /checkStatus.jsp

假设它映射到某个 url /checkStatus.jsp

Remove the loadData div because you don't need it anymore. Replace your javascript with:

删除 loadData div,因为您不再需要它。将您的 javascript 替换为:

var reloadStatus = function () {
    $.ajax("/checkStatus.jsp", function (data) {
        if (data == "Done") {
            // Your code here
        }
    });
};

setInterval(reloadStatus, 1000);

回答by Vivin Paliath

Your JSP code is evaluated only once -- when the page first loads. When JSP code runs, HTML is generated and sent to the browser. You cannot "reload" a div like that; the JSP code will not run.

您的 JSP 代码只计算一次——当页面第一次加载时。当 JSP 代码运行时,会生成 HTML 并将其发送到浏览器。你不能像那样“重新加载”一个 div;JSP 代码将不会运行。

What you can do is put the JSP code into a separate filee and then use jQuery.loadto load that page into a div:

您可以做的是将 JSP 代码放入一个单独的文件中,然后用于jQuery.load将该页面加载到一个 div 中:

jQuery(document).ready(function() {
   setInterval(function() {
       jQuery('#loadData').load('/status.jsp');
   }, 1000);
}

status.jspwill contain just the one line:

status.jsp将只包含一行:

<%= String.valueOf(session.getAttribute("Status")) ; %>

回答by ExitToShell

The code in a JSP is compiled/executed before the resulting HTML is sent to the browser. You can't reload part of the page as-rendered and expect it to change. You would probably need to make a hidden iframe and reload that completely (easy), or make a webservice to query the params (harder).

JSP 中的代码在生成的 HTML 发送到浏览器之前被编译/执行。您不能按照渲染重新加载页面的一部分并期望它发生变化。您可能需要制作一个隐藏的 iframe 并完全重新加载它(简单),或者制作一个网络服务来查询参数(更难)。