Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Web上的更新功能在更新页面时不填充文本框_Java_Mysql_Jsp_Servlets - Fatal编程技术网

Java Web上的更新功能在更新页面时不填充文本框

Java Web上的更新功能在更新页面时不填充文本框,java,mysql,jsp,servlets,Java,Mysql,Jsp,Servlets,我有一个主页,当用户单击更新项目时,它会重定向到更新页面。 我试图根据用户单击的项目将ID值传递到更新页面,但是我无法填充文本框。 你能帮助我吗?我不知道我的逻辑在哪里失败了 更新SQL: public static void insertProduct(Product product) { Connection connection = Database.getConnection(); String sql = "INSERT INTO product (name, ty

我有一个主页,当用户单击更新项目时,它会重定向到更新页面。 我试图根据用户单击的项目将ID值传递到更新页面,但是我无法填充文本框。 你能帮助我吗?我不知道我的逻辑在哪里失败了

更新SQL:

public static void insertProduct(Product product)
{ 
    Connection connection = Database.getConnection();

    String sql = "INSERT INTO product (name, type, price) VALUES (?, ?, ?)";

    try { 
        PreparedStatement statement = connection.prepareStatement(sql);

        statement.setString(1, product.getName());
        statement.setString(2, product.getType());
        statement.setDouble(3, product.getPrice());

        statement.execute();

    } catch (SQLException e) {
        e.printStackTrace();
    } 
}
控制器:

private ProductService productService = new ProductService();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//  request.setAttribute("products", productService.getProducts());
    request.getRequestDispatcher("/WEB-INF/views/home.jsp").forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String name = request.getParameter("name");
    double price = Double.parseDouble(request.getParameter("price"));
//产品新产品=新产品(名称、价格)

HTML主页:

        <tr>
            <th>Name</th>
            <th>Price</th>
            <th>Type</th>               
        </tr>
        <c:forEach var="product" items="${products}">
            <tr>
                <td><c:out value="${product.name}" /></td>
                <td><c:out value="${product.priceAsCurrency}" /></td>
                <td><c:out value="${product.type}" /></td>
                <td>
                    <a href="/updateItem?id=<c:out value='${product.id}' />">Edit</a>
                    <a href="/delete?id=<c:out value='${product.id}' />">Delete</a>                     
                </td>
            </tr>
         <input id="productId" name="productId" type="hidden" value="${product.id}">
         </c:forEach>

名称
价格
类型
更新页面:

 <form action="updateItem" method="post">
    <table border="1" cellpadding="5">
         <input type="hidden" name="id" value="<c:out value='${product.id}' 
 />" />       
        <tr>
            <th>Name: </th>
            <td>
                <input type="text" name="name" size="45"
                        value="${product.name}"/>"
                    />
            </td>
        </tr>
        <tr>
            <th>Type: </th>
            <td>
                <input type="text" name="type" size="45"
                        value="${product.type}"/>"
                    />
            </td>
        </tr>
        <tr>
            <th>Price: </th>
            <td>
                <input type="text" name="type" size="45"
                        value="${product.price}"/>"
                    />
            </td>
        </tr>
    </table>
    <button type="submit" value="submit">Update</button>
    </form>

姓名:
"
/>
类型:
"
/>
价格:
"
/>
更新
为了进行更新,根据数据库的要求,所有字段都不能为null,所以我希望用当前数据填充它们


提前感谢。

单击“编辑链接”会发生什么情况?@benjaminc,当我单击“编辑”时,它会进入更新页面,url为/updateItem?“我单击的项目的ID号”
 <form action="updateItem" method="post">
    <table border="1" cellpadding="5">
         <input type="hidden" name="id" value="<c:out value='${product.id}' 
 />" />       
        <tr>
            <th>Name: </th>
            <td>
                <input type="text" name="name" size="45"
                        value="${product.name}"/>"
                    />
            </td>
        </tr>
        <tr>
            <th>Type: </th>
            <td>
                <input type="text" name="type" size="45"
                        value="${product.type}"/>"
                    />
            </td>
        </tr>
        <tr>
            <th>Price: </th>
            <td>
                <input type="text" name="type" size="45"
                        value="${product.price}"/>"
                    />
            </td>
        </tr>
    </table>
    <button type="submit" value="submit">Update</button>
    </form>