【发布时间】:2013-09-13 16:31:05
【问题描述】:
我的表单参数没有将数据加载到控制器中,我在控制台中收到此消息:
INSERT INTO Products (code, cateogyr_code, product_category, product_name, description, price) VALUES ('0','0','null','null','null')'0.0')
表格如下:
<form action="controller" method="POST">
<input type="hidden" name="action" value="add">
<table>
<tr><td>Code</td><td><input name="code"></td></tr>
<tr><td>Name</td><td><input name="product_name"></td></tr>
<tr><td>Category Code</td><td><input name="category_code"></td></tr>
<tr><td>Category</td><td><input name="product_category"></td></tr>
<tr><td>Description</td><td><input name="description"></td></tr>
<tr><td>Price</td><td><input name="price" ></td></tr>
<tr><td colspan="2"><input type="submit" value="Save"></td></tr> </table> </form>
这是我的控制器方法:
else if (action.equals("add")) {
Product newProduct= new Product();
dao.addProduct(newProduct);
List<Product> products = dao.findAll();
address = "listproduct.jsp";
request.setAttribute("products", products);;
这是我的sql方法
public void addProduct(Product product) {
String sql = "INSERT INTO Products " +
"(code, category_code, product_category, product_name, description, price)" +
" VALUES (" +
"'" + product.getCode() + "'," +
"'" + product.getCategory_code() + "'," +
"'" + product.getCategory() + "'," +
"'" + product.getName() + "'," +
"'" + product.getDescription() + "')"+
"'" + product.getPrice() + "')";
System.out.println(sql);
}
有人可以帮我解决这个问题吗?
【问题讨论】:
-
请求是否到达控制器?参数是否正确传递?它在哪里失败?
-
在jsp页面失败,reqeust肯定是到达控制器和controllerImpl类,但是表单参数没有通过(它们都是“null”),你可以在错误
-
“正如你在错误中看到的那样” - 好吧,我真的看不到错误...... :)
-
我的意思是控制台消息,它显示分配的值是 'null' 和 0。
-
在提交表单时尝试使用名为 tamperData 的 Firefox 扩展程序,以准确查看提交了哪些参数以及提交到了哪个 URL。
标签: java sql jsp servlets jdbc