【问题标题】:I can not get updated data on the page after Update request to the database (data updated only after restart applications)在向数据库更新请求后,我无法在页面上获取更新的数据(数据仅在重新启动应用程序后更新)
【发布时间】:2020-12-16 03:00:17
【问题描述】:

我试图通过单击调用 stopReservation 方法的 CANCEL 按钮来停止当前的预订。数据在数据库中更新,但重定向返回与原来相同的列表,没有变化。只有在重新启动后,我才能得到带有数据的 updatetd 列表。网络显示 302 取消和 200 到 allreservs,但列表中没有 chages。

     @WebServlet("/cancel")
    public class CancelReservationServlet extends HttpServlet {
    @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.getRequestDispatcher("/WEB-INF/pages/cancel.jsp").forward(req, resp);
        }
    
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, NumberFormatException {
            String reservId = req.getParameter("id");
            if (reservId.isEmpty()) {
                resp.sendRedirect(req.getContextPath() + "/cancel");
            } else {
                ReservationService reservationService = new ReservationService();
                try {
                    reservationService.stopReservation(Integer.parseInt(reservId));
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                resp.sendRedirect(req.getContextPath() + "/allreservs");
            }
        }
    }
    
    @WebServlet("/allreservs")
    public class AllReservationsServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ReservationService reservationService = new ReservationService();
            Set<ReservationDto> reservations = null;
            try {
                reservations = reservationService.getListOfReserves();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            req.setAttribute("reservations", reservations);
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("/WEB-INF/pages/allreservs.jsp");
            requestDispatcher.forward(req, resp);
        }
    }

服务类的停止方法

    public void stopReservation(Integer reservationId) throws IOException, ClassNotFoundException, SQLException {
            Connection connection = ConnectionFactory.getConnection();
            try {
                preparedStatement = connection.prepareStatement("UPDATE reservation set isActive = false where reservationId = ?");
            preparedStatement.setInt(1, reservationId);
            preparedStatement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

JSP

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>All reservation</title>
        <link href="https://fonts.googleapis.com/css2?family=Baloo+Tamma+2:wght@500;600;700&display=swap" rel="stylesheet">
        <style>
            <%@include file="/styles/style.css"%>
        </style>
    </head>
    <body>
    <p>
        <a href='<c:url value="/"/>'><- main page</a>
    </p>
    <table>
        <thead>
        <tr>
            <th>ID</th>
            <th>FULL NAME</th>
            <th>MANIPULATION</th>
            <th>DESCRIPTION</th>
            <th>START TIME</th>
            <th>END TIME</th>
            <th>IS ACTIVE</th>
            <th>ROOM NUMBER</th>
            <th colspan="2">    CANCEL   </th>
        </tr>
        </thead>
        <c:forEach items="${reservations}" var="reservations">
            <tbody>
            <tr>
                <td>${reservations.id}</td>
                <td>${reservations.fullName}</td>
                <td>${reservations.manipulationName}</td>
                <td>${reservations.description}</td>
                <td>${reservations.startTime}</td>
                <td>${reservations.endTime}</td>
                <td>${reservations.isActive}</td>
                <td>${reservations.roomNumber}</td>
                <td><form action="${pageContext.request.contextPath}/cancel" method="post">
                <td>
                    <button onclick="location.href='/cancel'">cancel</button>
                <input type="hidden" name="id" value="${reservations.id}">
                </td>
                </form></td>
            </tr>
            </tbody>
        </c:forEach>
    </table>
    </body>
    </html>

补充:

    public Set<ReservationDto> getListOfReserves() throws IOException, ClassNotFoundException, SQLException {
            Connection connection = ConnectionFactory.getConnection();
            preparedStatement = connection.prepareStatement(GET_RESERVE_DATA);
            result = preparedStatement.executeQuery();
            while (result.next()) {
                reservs.add(new ReservationDto(
                        result.getInt("reservationId"),
                        result.getString("fullName"),
                        result.getString("manipulationName"),
                        result.getString("description"),
                        result.getTimestamp("startTime"),
                        result.getTimestamp("endTime"),
                        result.getBoolean("isActive"),
                        result.getInt("roomNumber")));
            }
            return reservs;
        }
     private static final String GET_RESERVE_DATA = "SELECT rsrv.reservationId, CONCAT(empl.name, ' ', empl.surname) as fullname, " +
                "rsrv.manipulationName, rsrv.description, rsrv.startTime, " +
                "rsrv.endTime, rsrv.isActive, r.roomNumber, empl.employeeId, r.roomId FROM reservation AS rsrv " +
                "JOIN room as r ON rsrv.roomid = r.roomId " +
                "JOIN employee as empl ON rsrv.emplId = empl.employeeId ";

【问题讨论】:

  • 您能否展示您的getListOfReserves() 方法,以及它执行的查询。您可以在获取listofReserves 的位置创建一个调试点,或者尝试在 req 属性中设置它之前打印预留集以检查数据。
  • @Silverfang 添加到附加部分
  • 你在哪里声明了reservs?如果您已在函数外部声明它,请在内部执行。它应该可以解决问题。或者您可以清空该集合,以便清除以前的值。 reservs.clear()
  • @Silverfang 谢谢你,非常感谢!添加reservs.clear() 并在按下按钮后立即获取我的更新列表!酷!
  • 我在答案中添加了相同的内容。请批准答案

标签: java mysql redirect servlets prepared-statement


【解决方案1】:

在函数内部声明reservs 集合。看起来它们是该集合中的先前值,因为您必须在函数之外声明它。如果您不希望这样,您可以在向其添加任何新值之前清除该集合。

方法一

内部getListOfReserves() 声明 Set&lt;ReservationDto&gt; reservs = new HashSet&lt;&gt;();

方法二

就在迭代结果之前。用户清除以从集合中删除所有以前的值。 reservs.clear()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2023-02-07
    • 2013-03-14
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多