2014-09-11 39 views
1

背景添加动态形式一对多的关系

我想创建一个假想的商店,我可以添加/更新客户项目购买。我的目标是让项目成为一个动态列表,因为客户可以随时挑选任何东西。

我创建了两个类: ItemsItemList为:

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的正确实体? 在此先感谢:)

回答

0

代码有错误。

@RequestMapping("/AddBill.view") 
public String addBill(@ModelAttribute("items") Item item){ 
    System.out.println(item); 
    return "NewBill"; 
} 

通过spring docs后,我得到了春天如何做绑定。总结是:

名词解释

+------------+-------------------------------------------------------------------------------+ 
|Expression |Explanation                 | 
+------------+-------------------------------------------------------------------------------+ 
|name  |Indicates the property name corresponding to the methods getName() or isName() | 
|   |and setName(..)                | 
+------------+-------------------------------------------------------------------------------+ 
|account.name| Indicates the nested property name of the property account corresponding e.g.| 
|   |to the methods getAccount().setName() or getAccount().getName()    | 
+------------+-------------------------------------------------------------------------------+ 
|account[2] |Indicates the third element of the indexed property account. Indexed properties| 
|   |can be of type array, list or other naturally ordered collection    | 
+------------+-------------------------------------------------------------------------------+ 
|account[key]|Indicates the value of the map entry indexed by the key of the Map property | 
|   |account                  | 
+------------+-------------------------------------------------------------------------------+ 

看完这个,我固定我提交的参数名称匹配输入字段的名称。

1

当您在谈论动态内容时,它很容易构建。

1.)一次进入如捕捉Customer Name等的格式的部分,会说出现在一段中。

2)形式的部分是一个有点collection,说itemList哪些客户可以通过添加新的提高,所以这将是某种grid/table结构与+ and -按钮,添加一个新的行或删除任何行。您只需维护列表的索引,每次用户添加一个新行,放入索引,并且如果用户删除该行,则相应地进行维护。

Spring将相应地映射此列表。

+0

我遇到了弹簧映射问题。你能提供一个样品吗? – NewUser 2014-09-11 07:03:23

+2

@Dibya做你的上述硬编码的代码工程.. ??如果不是什么问题..? – 2014-09-11 07:48:17

+0

是的Ankur,我的硬编码代码正在工作。我正在尝试为其添加更多字段。 – NewUser 2014-09-11 11:49:39