Java 将所选对象从JSP发送到Servlet
我是JavaEE的初学者,我已经开始实现一个小型在线书店购物车示例来学习和应用基本概念。 当用户搜索一本书时,它会给出一个建议书的列表,然后用户通过单击“添加到购物车”按钮开始将这些书添加到购物车 我使用了隐藏输入类型来发送它 下面是我的JSP代码Java 将所选对象从JSP发送到Servlet,java,jsp,servlets,Java,Jsp,Servlets,我是JavaEE的初学者,我已经开始实现一个小型在线书店购物车示例来学习和应用基本概念。 当用户搜索一本书时,它会给出一个建议书的列表,然后用户通过单击“添加到购物车”按钮开始将这些书添加到购物车 我使用了隐藏输入类型来发送它 下面是我的JSP代码 <% List<BookDetails> newlist = new ArrayList<BookDetails>(); newlist = (List)session.getAttribu
<%
List<BookDetails> newlist = new ArrayList<BookDetails>();
newlist = (List)session.getAttribute("currentSession");
%>
<table>
<form name="DisplayResult" action="addToCartServlet">
<tr>
<td><b>Book</b></td><td><b>Price</b></td>
</tr>
<%
for (int i = 0; i < newlist.size(); i++)
{
BookDetails book1 =newlist.get(i);
%>
<tr>
<td><%=book1.getBookName()%></td>
<td><%=book1.getPrice()%></td>
<td>
<input type="hidden" name="ISBN" value="<%=newlist.get(i).getISBN()%>">
<input type="submit" name="action" value="Add to Cart">
</td>
</tr>
<% }%>
</form>
</table>
书价
我通过servlet访问它,如下所示。
字符串isbn=request.getParameter(“isbn”)
但每次点击任何按钮时,它只需要第一个搜索结果值。
如何获得每本书的唯一ISBN 您需要每行一个表单来为每行传递不同的数据
见
<form:input path="contacts[${status.index}].book" />
<tr>
<td align="center">${status.count}</td>
<td><input name="contacts[${status.index}].book" value="${contact.book}"/></td>
<td><input name="contacts[${status.index}].price" value="${contact.price}"/></td>
</tr>
contacts[0].book // mapped to first item in contacts list
contacts[1].book// mapped to second item in contacts list
contacts[2].book// mapped to third item in contacts list
Its将按如下方式生成每行:
<form:input path="contacts[${status.index}].book" />
<tr>
<td align="center">${status.count}</td>
<td><input name="contacts[${status.index}].book" value="${contact.book}"/></td>
<td><input name="contacts[${status.index}].price" value="${contact.price}"/></td>
</tr>
contacts[0].book // mapped to first item in contacts list
contacts[1].book// mapped to second item in contacts list
contacts[2].book// mapped to third item in contacts list
行的说明是编码格式:
<form:input path="contacts[${status.index}].book" />
然后将其转换为以下HTML代码:
<input name="contacts[0].book" />
<input name="contacts[1].book" />
<input name="contacts[2].book" />
它将其转换为以下内容:
<input name="contacts0.book" />
<input name="contacts1.book" />
<input name="contacts2.book" />
请格式化您的代码。不惜一切代价避免使用Scriptlet。不惜一切代价避免使用Scriptlet。!我不认为索里奥斯的警告是严厉的,因为它需要。