【问题标题】:Update an html table from a session variable从会话变量更新 html 表
【发布时间】:2014-03-07 02:44:27
【问题描述】:

我对 JSP、jQuery 和 AJAX 还很陌生。我有一个 JSP,它使用基于会话中 List 变量中的值填充的表。代码在这里:

<table id="someData" class="display" width="95%">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<c:forEach var="var" items="${sessionVar}">
<tr>
<td><c:out value="${var.ID}" /></td>
<td><c:out value="${var.name}" /></td>
<td><c:if test="${var.gender=='M'}">
<c:out value="Male" />
</c:if> <c:if test="${var.dispositionCD=='F'}">
<c:out value="Female" />
</c:if>
</td>
</tr>
</c:forEach>
</tbody>
</table>

我使用 .ajax 调用控制器,这可能会导致 sessionVar 变量发生变化。可以添加其他人,可以删除一些人,可以更改一些名称,或者根本没有更改。控制器只会刷新会话中的 List 变量。这是我在用户输入一些数据并按下按钮时运行的 ajax 调用:

        $.ajax({
            type : "POST",
            url : urlText,
            data : 'testdata',
            dataType : 'json',
            success : function(data,textStatus) {
                $('#errormessage').text(data);
            },
            error : function() {
                $("#errormessage").text("Error while processing request..Please try again");
            }
        });

那么一旦session中的List变量更新了,如何更新html表中的数据呢?我已经在 J​​SP 和 jQuery 上工作了大约一个月,所以如果这似乎是业余时间,请放轻松。 :)

【问题讨论】:

  • 我不是 JSP 方面的专家,但我看到的基本问题是您的初始渲染是在服务器端完成的,但您想在客户端更新表,而不需要页面刷新,对吗?如果所有渲染都是任一服务器端客户端,而不是两者,那会更简单。
  • 如果我是正确的并且您想使用 Javascript 更新表格,您可以发布 AJAX 响应的示例吗?
  • 对于初始渲染,控制器使用 request.getSession("false").setAttribute("sessionVar", listOfPeopleToShow)。然后,在 JSP 上,我使用 c taglib 来遍历列表并填充表。它实际上是客户端,但使用放置在服务器会话中的列表。现在我要做的是调用控制器,运行一个可能会或可能不会更新数据的函数,并将数据读入新的 ArrayList,将其置于会话中,从 ajax 调用返回,并在“成功”中函数,更新表,因为它的数据可能已经改变了。
  • 好吧,我不明白它怎么可能是客户端的。会话变量在客户端不可访问(除非您在运行时使用变通方法将会话变量写入 JS 变量,但这仍然是服务器端操作)。 JSP 标签库是服务器端组件。

标签: jquery html ajax jsp


【解决方案1】:

试试这个。 http://jsfiddle.net/tonicboy/3HGFN/

HTML:

<p id="errormessage"></p>
<table id="someData" class="display" width="95%">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Gender</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>John Doe</td>
            <td>Male</td>
        </tr>
        <tr>
            <td>2</td>
            <td>Jane Doe</td>
            <td>Female</td>
        </tr>
    </tbody>
</table>
<button type="button" id="callAjax">Call the AJAX</button>

JS:

// We will use MockJAX to simulate an AJAX response - http://code.appendto.com/plugins/jquery-mockjax.
// This is only for illustration purposes, in your real app you would have a working controller.

$.mockjax({
    url: '/people/list',
    responseTime: 750,
    responseText: {
        numHits: 4,
        success: true,
        people: [
            {
                ID: 3,
                name: 'Adam Sapple',
                gender: 'M'
            },
            {
                ID: 4,
                name: 'Bill Board',
                gender: 'M'
            },
            {
                ID: 5,
                name: 'Candy Barr',
                gender: 'F'
            },
            {
                ID: 6,
                name: 'Dan Druff',
                gender: 'M'
            }
        ]
    }
});

$('#callAjax').click(function() {
    // Call the AJAX
    $.ajax({
        type : "POST",
        url : '/people/list',
        data : 'testdata',
        dataType : 'json',
        success : function(data, textStatus) {
            var tbody = $('#someData tbody');

            // clear out the existing table rows
            tbody.empty();

            // iterate through the results
            $.each(data.people, function(idx, person) {
                // create a row for each person
                var row = $('<tr></tr>');
                var genderText = (person.gender == 'M') ? 'Male' : 'Female';

                row.append('<td>' + person.ID + '</td>');
                row.append('<td>' + person.name + '</td>');
                row.append('<td>' + genderText + '</td>');

                tbody.append(row);
            });
        },
        error : function() {
            $("#errormessage").text("Error while processing request..Please try again");
        }
    });
});

更新:

根据您的 cmets,我更新了代码。基本上,您像以前一样进行 AJAX 调用,但不是在成功时动态创建新表,而是在成功时重新加载页面。

$.ajax({
    type : "POST",
    url : '/people/list',
    data : 'testdata',
    dataType : 'json',
    success : function(data, textStatus) {
        // reload page with force reload flag (force reload may not be necessary, try it without)
        location.reload(true);
    },
    error : function() {
        $("#errormessage").text("Error while processing request..Please try again");
    }
});

【讨论】:

  • 一件事。返回的数据是一个布尔值,用于说明函数是否成功。 “功能”在控制器中,它可以是对人员表的任何编辑。更新人员表后,在控制器中设置 sessionVar 变量如下:request.getSession("false").setAttribute("sessionVar", ListOfPeopleToShow);
  • 关键是 sessionVar 没有在 ajax 调用中传回。该调用传递一个布尔值,我需要执行其他任务 - 即显示和隐藏控件。
  • 在我看来,您根本无法在客户端进行表更新,因为您无权访问数据。由于您需要刷新页面,我将在您的 $.ajax() 调用上设置一个成功处理程序,并使用它来重新加载页面,该页面已设置为读取当前会话变量并构建人员表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2011-02-08
  • 2018-02-06
  • 1970-01-01
相关资源
最近更新 更多