2

JSP/サーブレット/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><%
            }
        %>

最後のセルの 2 つのリンクは、販売を表示し、それに応じて製品を更新するためのものです。最後の 1 つに関心があり、別の 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>

ここから、サーブレット 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(私はそれを実行しています)が何らかの形で最初のリクエストからのデータをコンテキストのどこかに保存し、オンデマンドで更新しないことです。また、奇妙なことは、製品の追加がうまく機能することです。

4

1 に答える 1

0

Problem was solved by refreshing each product before displaying it

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

I guess it can be improved by saving updated product's id and refreshing only them.

Origin of the problem is unknow, because in other project that implement same thing but with swing GUI everything worked fine w/o refresh. Maybe it's something with executing classes at Tomcat.

于 2012-12-09T14:19:46.463 に答える