Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 在页面中的一个实体中插入多个数据_Jsf - Fatal编程技术网

Jsf 在页面中的一个实体中插入多个数据

Jsf 在页面中的一个实体中插入多个数据,jsf,Jsf,我有一个CRUD生成的创建表单: <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="#{p

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

<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>
单击命令按钮后

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


但是我不确定这个页面的结构是什么。

您不能使用JSF组件在一个请求中发送来自多个表单的数据,您应该序列化所有数据并手动发送。最好有一个
列表
,每次单击按钮时,它都会在列表上创建一个新项目,并更新一个显示列表项目的
ui容器

这将是上述内容的一个开始示例:

@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);
    }
}
@ManagedBean
@视域
公共类ItemBean{
私人物品清单;
公共项bean(){
lstItem=newarraylist();
addItem();
}
//接球手和接球手。。。
公共无效附加项(){
添加(新项());
}
公共void saveData(){
//您可以将服务作为EJB注入,或者以您认为更好的方式注入。。。
ItemService ItemService=新的ItemService();
itemService.save(lstItem);
}
}
JSF代码(仅限内容):


输入项目名称

输入项目说明


嗨!谢谢你给了我一个主意!我制作了一个实体的列表数组。
@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);
    }
}
<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>