如何在编辑/保存表单中保留选定的实体?
我目前正在使用 JBoss 学习 Java EE,但我遇到了一个简单的问题。尽管这应该很常见,但我没有在互联网上找到解决方案。
我想显示联系人列表,如果选择了联系人,则会打开详细信息页面,并且可以编辑联系人信息。
该联系人是一个 JPA 实体。创建新联系人并显示联系人列表和详细信息页面效果很好。但是我在更新联系信息时遇到了麻烦。
如果单击联系人,我会打开详细信息页面并将 id 作为获取参数传递。现在执行以下 JSF 代码:
<f:metadata>
<f:viewParam name="contactID" value="#{contactUpdate.contactID}" />
</f:metadata>
<h:form>
<h:outputLabel for="firstName" value="First Name:" />
<h:inputText id="firstName" value="#{contactUpdate.contact.firstName}" />
<h:commandButton id="register" action="#{contactUpdate.updateContact()}" value="Update" />
</h:form>
这就是 bean(删除了 getter 和 setter):
@Stateful
@Named
@RequestScoped
public class ContactUpdate {
@Inject
private EntityManager em;
private Contact contact;
private long contactID;
public void updateContact(){
EntityTransaction transaction = em.getTransaction();
transaction.begin();
transaction.commit();
}
public void setContactID(long contactID) {
this.contactID = contactID;
setContact(em.find(Contact.class, contactID));
}
}
如果我更新它,则联系人为 null
。我认为请求范围太短并且已经清除。是否可以在提交时设置 contactID
?
我认为也可以使用 @ViewScoped
,但这样做
不再起作用。
I am currently learning Java EE using JBoss and I am kind of stuck with a simple problem. Even though it should be common I did not find a solution in the internet.
I want to display a list of contacts and if a contact is selected a details page opens and the contact information can be edited.
The contact is a JPA Entity. Creating new contacts and displaying a list of contacts and a details page works fine. However I have trouble updating the contact information.
If a contact is clicked I open a details page and pass the id as get parameter. Now the following JSF code is executed:
<f:metadata>
<f:viewParam name="contactID" value="#{contactUpdate.contactID}" />
</f:metadata>
<h:form>
<h:outputLabel for="firstName" value="First Name:" />
<h:inputText id="firstName" value="#{contactUpdate.contact.firstName}" />
<h:commandButton id="register" action="#{contactUpdate.updateContact()}" value="Update" />
</h:form>
That is the bean (getter and setters removed):
@Stateful
@Named
@RequestScoped
public class ContactUpdate {
@Inject
private EntityManager em;
private Contact contact;
private long contactID;
public void updateContact(){
EntityTransaction transaction = em.getTransaction();
transaction.begin();
transaction.commit();
}
public void setContactID(long contactID) {
this.contactID = contactID;
setContact(em.find(Contact.class, contactID));
}
}
If I update it, the contact is null
. I assume that the request scope is too short and already cleared. Is it possible to set the contactID
upon commit?
I thought it would also be possible to use @ViewScoped
, but doing so the <f:viewParam>
does not work anymore.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您正在视图参数的 setter 中准备联系人,所以您需要将联系人 ID 作为请求参数传回,以便请求作用域 bean 可以正确准备它:
但最好不在setter中执行任何业务逻辑(并且绝对也不在getters中)并且请改用
。这样,@ViewScoped
将按预期工作。另请参阅:
Because you're preparing the contact in a setter of a view parameter, you need to pass the contact ID back as request parameter so that the request scoped bean can prepare it properly:
But better would be to not do any business logic in setters (and definitely also not in getters) and use
<f:event type="preRenderView">
instead. This way the@ViewScoped
will work as expected.See also: