【问题标题】:jstl search record will be kept add on at the last recordjstl 搜索记录将保留在最后一条记录上
【发布时间】:2012-02-25 01:09:30
【问题描述】:

我正在使用 JSTL 来显示我从数据库中搜索到的 JSP 中的记录。

例如,从数据库返回的这些记录是:

<tr>
    <td class="leavehistory">2012-01-31 18:16:36.0</td><td class="leavehistory">Present</td>
</tr>

<tr>
    <td class="leavehistory">2012-01-31 18:16:38.0</td><td class="leavehistory">Present</td>
</tr>

这是我的 JSP:

<c:forEach items="${attendRecord}" var="attendRecord">
   <tr>
       <td class="leavehistory">${attendRecord.attendDt}</td><td class="leavehistory">${attendRecord.status}</td>
   </tr>
</c:forEach>

这是我的 Servlet:

ArrayList<Attendance>  arrSub = new ArrayList<Attendance>();

while (rs.next()){
    Attendance att = new Attendance();
    att.setAttendDt(rs.getString(1));
    att.setStatus(rs.getString(2));
    att.setClsType(rs.getString(3));
    att.setSubID(rs.getString(4));
    arrSub.add(att);
}

request.getSession().setAttribute("attendRecord", arrSub);

在我执行另一个搜索的地方,结果将是这样的:

<tr>
    <td class="leavehistory">2012-01-31 18:16:36.0</td><td class="leavehistory">Present</td>
</tr>

<tr>
    <td class="leavehistory">2012-01-31 18:16:38.0</td><td class="leavehistory">Present</td>
</tr>

<tr>
    <td class="leavehistory">2012-01-31 18:16:36.0</td><td class="leavehistory">Late</td>
</tr>

<tr>
    <td class="leavehistory">2012-01-31 18:16:38.0</td><td class="leavehistory">Late</td>
</tr>

这是我用来调用我的 servlet 的代码:

<script>

        function reviewAtt(){
            var f = document.review;

            f.hdSearch.value = f.cardID.value +";"+document.getElementById("clsType").value+";"+document.getElementById("sub").value;
            f.method = "POST";
            f.target = "_self";
            f.action = "../build/web/WEB-INF/classes/leave/searchAttendServlet";
            f.submit();
        }

    </script>

然后我继续执行另一个搜索动作,最新的搜索结果应该替换旧记录并显示最新的结果。但它不会,最新的结果将添加到旧记录的下面。我只想显示最新的搜索结果。 旧的搜索结果将被淘汰。

有什么办法可以解决这个问题吗?

【问题讨论】:

  • 你没有说明你是如何在arrSub中设置数据的
  • 它在我的代码中,我只是误将它粘贴在这里。对不起。
  • 如果您正在创建新的ArrayList 并添加每个请求,那么它不应该附加搜索结果,请添加更多详细信息如何调用 servlet
  • 我已经更新了调用servlet的代码。

标签: java html jsp servlets jstl


【解决方案1】:

这个问题不是由 JSP/JSTL 引起的。这是由代码中某处的错误引起的。但是到目前为止发布的代码看起来不错。我敢打赌,你只是过度简化了代码,它神奇地让它看起来很好。

至少,症状表明您将请求范围的值存储为 servlet 类的实例变量,这使其成为线程不安全的(即它在所有会话中的所有请求之间共享)。像这样的:

public class SearchAttendServlet extends HttpServlet {

    ArrayList<Attendance> arrSub = new ArrayList<Attendance>();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // ...
    }

}

这是不对的。

也将其存储为会话属性是不对的,但这不一定是您的问题的原因(除非您在向会话添加新项目之前明确地从会话中检索了相同的列表,但到目前为止发布的代码并没有'不要以任何方式表明这一点)。

您应该在线程本地范围内创建列表并将其存储在请求范围内。

public class SearchAttendServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<Attendance> arrSub = new ArrayList<Attendance>();

        // ...

        request.setAttribute("attendRecord", arrSub);
        request.getRequestDispatcher("/WEB-INF/yourpage.jsp").forward(request, response);
    }

}

另见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 2017-05-08
    • 2011-03-07
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    相关资源
    最近更新 更多