I can not get updated data on the page after Update request to the database (data updated only after restart applications)

huangapple go评论70阅读模式
英文:

I can not get updated data on the page after Update request to the database (data updated only after restart applications)

问题

@Web服务("/cancel")
public class CancelReservationServlet extends HttpServlet {
@Override
    protected void doPost(HttpServletRequest请求HttpServletResponse resp) throws ServletExceptionIOExceptionNumberFormatException {
        String reservId = req.getParameter("id");
        if (reservId.isEmpty()) {
            resp.sendRedirect(req.getContextPath() + "/cancel");
        } else {
            ReservationService reservationService = new ReservationService();
            尝试 {
                reservationService.stopReservation(Integer.parseInt(reservId));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            resp.sendRedirect(req.getContextPath() + "/allreservs");
        }
    }
}
@Web服务("/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;
        尝试 {
            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();
    尝试 {
        preparedStatement = connection.prepareStatement("UPDATE reservation set isActive = false where reservationId = ?");
        preparedStatement.setInt(1, reservationId);
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

JSP部分

<%@ 标签库前缀="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>所有预订</title>
    <link href="https://fonts.googleapis.com/css2?family=Baloo+Tamma+2:wght@500;600;700&amp;display=swap" rel="stylesheet">
    <style>
        <%@include file="/styles/style.css"%>
    </style>
</head>
<body>
<p>
    <a href='<c:url value="/"/>'>&lt;- 主页</a>
</p>
<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>全名</th>
        <th>操作</th>
        <th>描述</th>
        <th>开始时间</th>
        <th>结束时间</th>
        <th>是否活动</th>
        <th>房间号</th>
        <th colspan="2">取消</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'">取消</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 ";
英文:

I trying to stop current reservations, by clicking on CANCEL button, which call stopReservation method. Data updated in database, but redirect return the same list as was, without chages. Only after restart I get updatetd list with data. Network show 302 to cancel and 200 to allreservs, but there no chages in list.

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

STOP METHOD FROM SERVICE CLASS

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

JSP

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

additional:

    public Set&lt;ReservationDto&gt; 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(&quot;reservationId&quot;),
                        result.getString(&quot;fullName&quot;),
                        result.getString(&quot;manipulationName&quot;),
                        result.getString(&quot;description&quot;),
                        result.getTimestamp(&quot;startTime&quot;),
                        result.getTimestamp(&quot;endTime&quot;),
                        result.getBoolean(&quot;isActive&quot;),
                        result.getInt(&quot;roomNumber&quot;)));
            }
            return reservs;
        }
     private static final String GET_RESERVE_DATA = &quot;SELECT rsrv.reservationId, CONCAT(empl.name, &#39; &#39;, empl.surname) as fullname, &quot; +
                &quot;rsrv.manipulationName, rsrv.description, rsrv.startTime, &quot; +
                &quot;rsrv.endTime, rsrv.isActive, r.roomNumber, empl.employeeId, r.roomId FROM reservation AS rsrv &quot; +
                &quot;JOIN room as r ON rsrv.roomid = r.roomId &quot; +
                &quot;JOIN employee as empl ON rsrv.emplId = empl.employeeId &quot;;

答案1

得分: 0

在函数内部声明 reservs 集合。因为看起来在这个集合中有之前的值,这是因为你必须已经在函数外部声明了它。如果你不想要这样,你可以在添加新值之前清空集合。

方法1

getListOfReserves() 函数内部的声明中添加 Set<ReservationDto> reservs = new HashSet<>();

方法2

在遍历结果之前,使用 reservs.clear() 来从集合中移除所有先前的值。

英文:

Declare the reservs set inside the function. As it seems that their are previous values in this set, since you must have declared it outside the function. If you don't want that, you can clear the set before adding any new values to it.

Method 1

Inside getListOfReserves() declare Set&lt;ReservationDto&gt; reservs = new HashSet&lt;&gt;();

Method 2

Just before iterating the result. User clear to remove all the previous values from the set. reservs.clear()

huangapple
  • 本文由 发表于 2020年8月27日 16:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63612394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定