2013-02-27 33 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