数据更改时自动刷新jsp页面 [英] auto refresh jsp page when data changes

查看:441
本文介绍了数据更改时自动刷新jsp页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSP页面,其中显示了数组中包含的项目(只是一个非常简单的列表).
阵列在后台可能会更改,即添加新项目或删除其中一个项目.

I have a JSP page which shows items included in an Array (Just a very simple list).
In the background the Array might change i.e. adds a new Item or remove one.

当数组更改时,如何自动刷新页面?

How can I auto refresh the page when the Array changes?

推荐答案

执行这种操作的最流行的方法有2种

There are 2 ways that are most popular to perform such operation

  • 合并一个方法,该方法将发送1或0以查看是否刷新页面
  • 继续询问该数据数组,并通过javascript填充它

选项1

  • 创建一个.jsp页面并调用它,例如updateList.jsp
  • 添加单个方法,该方法将检查是否有更多数据要填充,并输出1或0,例如:out.println(1)
  • 在页面中,并使用jQuery简化操作
  • create a .jsp page and call it, for example, updateList.jsp
  • add a single method that will check if there is more data to be filled and output 1 or 0 like: out.println(1)
  • in your page, and using jQuery to simplify things
 $.get("updateList.jsp", function(data) {
    if(data !== null && data.length > 0 && data === 1) {
        // refresh this page
        document.location = document.location.href;
    }
 });


选项2

  • 创建一个.jsp页面并调用它,例如data.jsp
  • 添加一个方法,该方法将输出一个JSON字符串,其中包含填充列表所需的所有数据
  • 在页面中,并使用jQuery和JsRender简化操作
  • create a .jsp page and call it, for example, data.jsp
  • add a single method that will output a JSON string containing all data you need to populate the list
  • in your page, and using jQuery and JsRender to simplify things
 $.get("updateList.jsp", function(data) {
    if(data !== null && data.length > 0) {
        $("#my-list").html(
            $("#my-template").render(data);
        );
    }
 });

,在您的HTML中,您将:

and in your HTML you will have:

<ul id="my-list"></ul>

<script id="my-template" type="text/x-jsrender">
    {{for items}}
      <li>{{:name}}</li>
    {{/for}}
</script>

假设您的JSON类似于:

assuming your JSON would be something like:

item: [
    { name: "Name A" },
    { name: "Name B" },
    { name: "Name C" },
]

这篇关于数据更改时自动刷新jsp页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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