2013-02-27 64 views
0

我有一個CRUD生成創建形式:插入多DATAS在1個實體在一個頁面

<div class="create-form"> 
    <h:form> 
     <h:inputText id="name" value="#{pointController.selected.name}" title="#{bundle.CreatePointTitle_name}" required="true" /> 

     <h:inputText id="term" value="#{pointController.selected.term}" title="#{bundle.CreatePointTitle_term}" required="true" /> 

     <p:commandButton styleClass="btn" action="#{pointController.create}" value="#{bundle.CreatePointSaveLink}" /> 
    </h:form> 
</div> 
<button>add new form</button> 

我有一個按鈕,如果點擊它會創建另一種形式中與上述相同的使用JavaScript。 (2 inputText,name and term)

我的目標是用兩種或兩種以上的形式,取決於用戶需要多少表單,用1個命令按鈕單擊它將插入數據庫中的所有內容。

例如:

first form: name = first form test, term = first form testterm 
2nd form: name = 2nd form test, term= 2nd form testterm 

點擊命令按鈕

2行後將在數據庫中的相同的表被插入。

但我不確定頁面的結構是什麼。

回答

1

無法使用JSF組件在單個請求中發送來自多個表單的數據,您應該序列化所有數據並手動發送。最好有一個List<Item>,並且每次點擊按鈕時,它將在列表上創建一個新項目並更新將顯示列表項目的UIContainer

這將是上述的開始例如:

@ManagedBean 
@ViewScoped 
public class ItemBean { 
    private List<Item> lstItem; 

    public ItemBean() { 
     lstItem = new ArrayList<Item>(); 
     addItem(); 
    } 
    //getters and setter... 

    public void addItem() { 
     lstItem.add(new Item()); 
    } 

    public void saveData() { 
     //you can inject the service as an EJB or however you think would be better... 
     ItemService itemService = new ItemService(); 
     itemService.save(lstItem); 
    } 
} 

JSF代碼(<h:body>內容只):

<h:form id="frmItems"> 
    <h:panelGrid id="pnlItems"> 
     <ui:repeat value="#{itemBean.lstItem}" var="item"> 
      Enter item name 
      <h:inputText value="#{item.name}" /> 
      <br /> 
      Enter item description 
      <h:inputText value="#{item.description}" /> 
      <br /> 
      <br /> 
     </ui:repeat> 
    </h:panelGrid> 
    <p:commandButton value="Add new item" action="#{itemBean.addItem}" 
     update="pnlItems" /> 
    <p:commandButton value="Save data" action="#{itemBean.saveData}" /> 
</h:form> 
+0

喜!謝謝!你給了我一個主意!我做了一個實體列表數組。 – galao 2013-02-27 06:35:13

+0

@galao歡迎:) – 2013-02-27 06:35:34