刷新其中包含JSP代码段的DIV元素 [英] Refreshing a DIV element which has JSP code snippet inside

查看:227
本文介绍了刷新其中包含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);

这篇关于刷新其中包含JSP代码段的DIV元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆