gpt4 book ai didi

jsp - 从 JSP 页面更新数据库条目

转载 作者:行者123 更新时间:2023-11-28 22:03:25 25 4
gpt4 key购买 nike

我的 JSP/Servlet/EclipseLink-JPA 项目出现了一些奇怪的问题。我直接从 NetBeans(EE 包)7.2.1 在 Apache Tomcat 7.0.27.0 上运行,数据库是 PostgresSQL
这是一些代码:
jsp页面负责显示产品表,index.jsp:

<%
for(Product product:ProductController.getProducts()){
%><tr>
<td><%
out.print(product.getName());
%></td>
<td><%
out.print(product.getPrice());
%></td>
<td><%
out.print(product.getUnit());
%></td>
<td><%
out.print(product.getSales().size());
%></td>
<td><a target=\"_blank\" href="viewSales.jsp?id=<%=product.getId_product()%>">sales</a> </td>
<td>
<a target="_blank" href="updateProduct.jsp?id=<%=product.getId_product()%>&name=<%=product.getName()%>&price=<%=product.getPrice()%>&unit=<%=product.getUnit()%>">edit</a>
</td>
</tr><%
}
%>

最后一个单元格中的两个链接用于查看销售情况和相应地更新产品。我们对最后一个感兴趣,导致另一个 jsp:

<form action="UpdateProduct" method="post">
<input type="text" name="id" style="display:none" value="<%=request.getParameter("id") %>">
<input type="text" name="name" size="30" placeholder="Название" value="<%=request.getParameter("name")%>"><br>
<input type="text" name="price" size="30" placeholder="Цена" value="<%=request.getParameter("price")%>"><br>
<input type="text" name="unit" size="30" placeholder="Единица" value="<%=request.getParameter("unit")%>"><br>
<input type="submit" name="save" value="Сохранить">
</form>

从这里我们将转到 servlet UpdateProduct.java:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Long id = Long.parseLong(request.getParameter("id"));
String name = request.getParameter("name");
double price = Double.parseDouble(request.getParameter("price"));
String units = request.getParameter("unit");
ProductController.updateProduct(id, name, price, units);

response.sendRedirect("");
} finally {
out.close();
}
}

ProductcController.updateProduct 工作正常(我可以直接在数据库中看到它)。

但是当我返回第一页(index.jsp)时,无论刷新多少次,我都看不到任何变化。我最接近的猜测是 Apache Tomcat(我在上面运行)以某种方式将来自第一个请求的数据保存在上下文中的某个地方,并且不会按需刷新它。另外,奇怪的是添加产品效果很好。

最佳答案

通过在显示之前刷新每个产品解决了问题

List products = em.createQuery("SELECT p FROM Product p ORDER BY p.id_product").getResultList(); 
for(Object product:products){
em.refresh(product);
}

我想可以通过保存更新的产品 ID 并仅刷新它们来改进它。

问题的根源未知,因为在其他实现相同功能但使用 swing GUI 的项目中,一切正常,无需刷新。也许这与在 Tomcat 上执行类有关。

关于jsp - 从 JSP 页面更新数据库条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13786925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com