微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

对数据库的更新请求后,我无法在页面上获取更新的数据仅在重新启动应用程序之后更新数据

如何解决对数据库的更新请求后,我无法在页面上获取更新的数据仅在重新启动应用程序之后更新数据

我试图通过单击CANCEL按钮来停止当前的预订,该按钮调用stopReservation方法。数据已在数据库中更新,但重定向返回的列表与以前相同,没有更改。仅在重启后,我才会获得包含数据的updatetd列表。网络节目302取消,所有节目200保留,但列表中没有变化。

     @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,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,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,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 ";

解决方法

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

方法1

getListOfReserves()内部声明 Set<ReservationDto> reservs = new HashSet<>();

方法2

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。