java – 读取表html行值到servlet到另一个jsp页面进行编辑

我想获取行的值,然后将其发送到
Java servlet,然后由另一个页面读取,并将这些值插入到文本框中,供用户编辑并将其写回文本文件.

所以它被ProductIO读取,它读取文本文件.
然后它进入一个jsp表

   <c:forEach var="product" items="${products}">
                <tr>
                    <td ><c:out value='${product.code}'/></td>
                    <td ><c:out value='${product.description}'/></td>
                    <td >${product.priceCurrencyFormat}</td>
                    <td><form action="editproduct" method="post">
                            <input type="submit" value = "Edit">
                        </form>
                    </td>
                    <td><form action="deleteproduct" method="post">
                            <input type="submit" value = "Delete">
                        </form>
                    </td>
                </tr>
              </c:forEach>

用户单击删除或编辑按钮,然后将该操作发送到deleteproduct servlet或editproduct servlet(仅询问有关编辑的内容)

编辑产品servlet

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession session = request.getSession();
    String url = "/editproduct.jsp";
    getServletContext()
            .getRequestDispatcher(url)
            .forward(request, response);
    String action = request.getParameter("action");
    if (action == null) {
        action = "editproduct";  // default action
    } else if (action.equals("editproduct")) {
        String productCode = request.getParameter("productCode");
        String descString = request.getParameter("description");

        //HttpSession session = request.getSession();
        Product product = (Product) session.getAttribute("cart");
        if (product == null) {
            product = new Product();
        }
        getServletContext()
                .getRequestDispatcher(url)
                .forward(request, response);
    }
}

哪三个值放在editProduct.jsp页面上的三个文本框中(其中我有一个问题是将值插入到文本框中,以便将其写回具有新信息的文本文件)

            <form action="Product" method="post" >
        <input type="hidden" name="action" value="add">        
        <label>Code:</label>
        <input type="text" name="code" value='<%=request.getAttribute("code")%>' 
               required><br>
        <label >Description:</label>
        <input type="text" name="desc" value='<%=request.getAttribute("description")%>' 
               required ><br>
        <label >Price:</label>
        <input type="text" name="price" value='<%=request.getAttribute("price")%>'  
               required ><br>        
        <label>&nbsp;</label>
        <!--<input type="submit" value="Update Product" class="margin_left">-->
        <!--<input type="submit" value="View Product" class="margin_left">-->
        <button type="submit">Update</button><br>

如果需要,我可以分享更多代码.

最佳答案 您没有使用Servlet中的任何属性调用request.setAttribute().我假设你想添加类似的东西

request.setAttribute("code", productCode);
request.setAttribute("description", descString);
request.setAttribute("price", product.getPrice());

在转发请求之前.

点赞