刷新其中包含JSP代码段的DIV元素

刷新其中包含JSP代码段的DIV元素

问题描述:

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

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

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

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.

这是我的代码库:

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.

JSP在服务器上呈现一次,然后发送到客户端,之后Java代码不执行任何操作。如果您希望以不同的时间/频率加载HTML / JavaScript代码和Java代码,您不能将它们放在同一个文件中。

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文件:

Put this into a separate .jsp file:

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

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

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

删除loadData div,因为您不再需要它了。用你的javascript代替:

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

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

setInterval(reloadStatus, 1000);