Inserting multiple datas in 1 entity in a page

前端 未结 1 841
旧巷少年郎
旧巷少年郎 2020-12-22 11:35

i have a CRUD generated create form:

相关标签:
1条回答
  • 2020-12-22 12:18

    You can't send data from many forms in a single request using JSF components, you should serialize all the data and send it manually. It would be better to have a List<Item> and every time you click in the button it will create a new item on the list and update an UIContainer that will display the items of the list.

    This would be a start example of the above:

    @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 code (<h:body> content only):

    <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 讨论(0)
提交回复
热议问题