在页面的1个实体中插入多个数据 [英] Inserting multiple datas in 1 entity in a page

查看:74
本文介绍了在页面的1个实体中插入多个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CRUD生成的创建表单:

i have a CRUD generated create form:

<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,名称和术语)

i have a button that if clicked it will create another form same as above using javascript. (2 inputText, name and term)

我的目标是使用2个或更多表格,取决于用户想要多少表格,单击1个commandButton会将所有内容插入数据库.

my goal is, with 2 or more forms, depending how many forms the user wants, with 1 commandButton that is clicked it will insert everything in the database.

示例:

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

点击命令按钮后

2行将插入数据库的同一表中.

2 rows will be inserted in the same table in the database.

但是我不确定页面的结构是什么.

but i'm not sure what would be the structure for page for this.

推荐答案

您不能使用JSF组件在单个请求中从多种形式发送数据,您应该序列化所有数据并手动发送.最好有一个List<Item>,每次单击该按钮时,它将在列表中创建一个新项目,并更新一个UIContainer以显示列表中的项目.

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代码(仅<h:body>内容):

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>

这篇关于在页面的1个实体中插入多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆