【发布时间】:2018-04-28 16:19:18
【问题描述】:
<html>
<head>
<title>View Shopping Cart</title>
<script type="text/javascript">
function remove(book_id)
{
document.getElementById('bookId').value = book_id;
document.forms[0].submit();
}
</script>
</head>
<body>
<table border="1" id="bookTable">
<tr>
<th>Book ID</th>
<th>Book Name</th>
<th>Author</th>
<th>Quantity</th>
<th>Remove from cart</th>
</tr>
<%
try
{
int cus_id = Integer.parseInt(session.getAttribute("cusId").toString());
ResultSet rs = SqlConnection.getData("select * from book b, shopping_cart s where s.book_id=b.book_id AND s.cus_id=" + cus_id);
if (!rs.first())
{
out.print("<h3>Nothing in the Cart!</h3>");
}
else
{
rs.previous();
while (rs.next())
{
%>
<tr>
<%
out.print("<td>" + rs.getInt("book_id") + "</td>");
out.print("<td>" + rs.getString("book_name") + "</td>");
out.print("<td>" + rs.getString("author_name") + "</td>");
out.print("<td><input type='number' name='qty' id='qty'/></td>");
out.print("<td><input type='button' onClick='remove(" + rs.getInt("book_id") + ")' value='Remove from cart'/></td>");
%>
</tr>
<%
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
%>
</table><br/><br/>
<p style="color: green;">${successful}</p> <br/>
<p style="color: red;">${error}</p> <br/>
<a href="viewBooks.jsp"><input type="button" value="View Books"></a>
<a href="welcome.jsp"><input type="button" value="Home"></a>
<form action="/ShoppingCartRemoving" method="post">
<input type="hidden" name="bookId" id="bookId"/>
</form>
</body>
</html>
这是 JSP 代码。我检查并意识到这永远不会进入 servlet。但是另一个具有相同结构的 JSP 页面可以正常工作。这很令人困惑。我正在尝试从购物车中删除商品。
JavaScript 函数是获取 bookId 并将其放入表单的隐藏输入字段中,然后提交表单。这个确切的代码在另一个 JSP 页面中运行良好。
【问题讨论】:
-
您是否在
web.xml或使用annotations配置了servlet? -
是的。这一切都得到了照顾。即使在运行时,IntelliJ 也不会因此产生任何错误。当我点击它时,“添加到购物车”按钮就消失了。这就是它所做的一切。 @SandeshGupta
标签: javascript html jsp servlets