我一直在审查在线发布的一些示例,它们都执行简单的 CRUD。
1 jsf page = 1 entity = 1 table.
大多数时候,这就是我所看到的。但是,如果您只有 1 个 jsf 页面和 1 个表单,并且需要向 3 个实体提供数据,该怎么办?具有姓名、公司和爱好等表单字段。
它们的值需要放入实体
person.name、work.company_name 和 other_info.hobby。
这是通过绑定(bind)自动完成的吗?或者我们需要进行一些手动赋值?请澄清一下,我现在有点困惑
最佳答案
我不确定我是否看到了这个问题。您可以将它们设置为同一个支持 bean 的属性:
@ManagedBean
@ViewScoped
public class Profile {
private Person person;
private Work work;
private OtherInfo otherInfo;
// ...
}
与
<h:inputText value="#{profile.person.name}" />
<h:inputText value="#{profile.work.companyName}" />
<h:inputText value="#{profile.otherInfo.hobby}" />
或者,如果 Work
和 OtherInfo
与 Person
具有 @OneToOne
关系(在现实世界中,它们无疑具有):
@ManagedBean
@ViewScoped
public class Profile {
private Person person; // Has in turn Work and OtherInfo properties.
// ...
}
与
<h:inputText value="#{profile.person.name}" />
<h:inputText value="#{profile.person.work.companyName}" />
<h:inputText value="#{profile.person.otherInfo.hobby}" />
关于java - 从单个 html 表单(jsf 页面)更新不同的 jpa 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8426389/