我想创建一个假想的商店,我可以添加/更新客户和项目购买。我的目标是让项目成为一个动态列表,因为客户可以随时挑选任何东西。
我创建了两个类: Items
,ItemList
为:
public class Item {
private String itemName;
private String amount;
private String quantity;
而且
public class ItemList{
private List<Item>;
JSP页面中
<input type = "text" name = "items[0].itemName"/>
<input type = "text" name = "items[0].amount"/>
<input type = "text" name = "items[0].quantity"/>
<input type = "text" name = "items[1].itemName"/>
<input type = "text" name = "items[1].amount"/>
<input type = "text" name = "items[1].quantity"/>
硬编码只获得两个项目,现在。
在我的控制器,
@RequestMapping("/AddBill.view")
public String addBill(@ModelAttribute("items") Item item){
System.out.println(item);
return "NewBill";
}
这曾经让我在表单中的细节。现在我正在考虑添加客户详细信息,以便我可以关联数据。
我试着给ItemList
类添加一个名称字段,但渲染时得到了空值。我试着将ItemList
放到一个名为Customer
的新类中,并在其中包含字段名称。
问题:请告诉我如何使表格的一部分变为动态,而只有少数项目只能输入一次。
更新:
我想用的形式是接近:
<input type = "text" name = "customerName"/>
<input type = "text" name = "customerContact"/>
<input type = "text" name = "items[0].itemName"/>
<input type = "text" name = "items[0].amount"/>
<input type = "text" name = "items[0].quantity"/>
<input type = "text" name = "items[1].itemName"/>
<input type = "text" name = "items[1].amount"/>
<input type = "text" name = "items[1].quantity"/>
请告诉我如何与实体映射此。我无法在Spring中映射它。为实体中的值获取Null。请告诉我什么才是上述JSP的正确实体? 在此先感谢:)
我遇到了弹簧映射问题。你能提供一个样品吗? – NewUser 2014-09-11 07:03:23
@Dibya做你的上述硬编码的代码工程.. ??如果不是什么问题..? – 2014-09-11 07:48:17
是的Ankur,我的硬编码代码正在工作。我正在尝试为其添加更多字段。 – NewUser 2014-09-11 11:49:39