如何在编辑/保存表单中保留选定的实体?

发布于 2024-12-16 12:56:28 字数 1332 浏览 2 评论 0原文

我目前正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

药祭#氼 2024-12-23 12:56:28

因为您正在视图参数的 setter 中准备联系人,所以您需要将联系人 ID 作为请求参数传回,以便请求作用域 bean 可以正确准备它:

<h:commandButton id="register" action="#{contactUpdate.updateContact()}" value="Update">
    <f:param name="contactID" value="#{contactUpdate.contactID}" />
</h:commandButton>

但最好在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:

<h:commandButton id="register" action="#{contactUpdate.updateContact()}" value="Update">
    <f:param name="contactID" value="#{contactUpdate.contactID}" />
</h:commandButton>

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文