JSF + HashMap 及其键

发布于 2024-09-06 21:41:31 字数 924 浏览 2 评论 0原文

我有一个包含 2 个表 CDSong 的数据库。然后会话 bean 访问这两个表的实体类。在我的支持 bean 中,我只有一个 String cdHashMap。 cds 它将保存从我的 sessionbean 返回的 CD 列表,所以在 JSF 中我会做这样的事情。

<h:selectOneMenu id="cd" value="#{backingBean.cd}">
     <f:selectItem itemLabel="Select CD" itemValue="" />
     <f:selectItems value="#{backingBean.cds}" />
</h:selectOneMenu>

这成功地将 cd 列表加载到下拉列表中,如果我选择一张 cd,cd 变量将保存我选择的 CD 的名称。如果 CDName 是唯一的,则此方法非常有效。但不幸的是事实并非如此。所以我想要的是 HashMapcds 其中ID 是表CD 中的PK。但是现在我如何设置它,所以当我单击 CD 下拉列表中的某个项目时,我会在我的 backingbean 中得到 ID ,这样我就可以在我的会话 bean

CD cd = EntityManager.find(CD.class, the id that I get back from JSF page)

本质上我想获取我刚刚单击的 cd 对象,请记住,可能会有重复。如果我的设计不好,请指出。请帮忙。提前致谢

I have a database with 2 tables CD and Song. Session bean access then entity classes of those two tables. In my backing bean, I just have a String cd and HashMap<CDName, CDName> cds that will hold the list of CD return back from my sessionbean, so in JSF I would do something like this.

<h:selectOneMenu id="cd" value="#{backingBean.cd}">
     <f:selectItem itemLabel="Select CD" itemValue="" />
     <f:selectItems value="#{backingBean.cds}" />
</h:selectOneMenu>

This successfully load a list of cd onto the drop down list, and if I select a cd, cd variables would hold the name of the CD I select. This work great if CDName is unique. But unfortunately it is not. So what I want is HashMap<ID, CDName> cds where ID is the PK in table CD. But now how I can set it up, so when I click on a item from the CD drop down list, I get the ID back in my backingbean, so that I can do something like this, in my session bean

CD cd = EntityManager.find(CD.class, the id that I get back from JSF page)

essentially I want to obtain the cd object that I just click on, keep it in mind, there might be duplication. If my design is bad, please point out. Help please. Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

柠檬色的秋千 2024-09-13 21:41:31

它将在 #{backingBean.cd} 后面的属性中设置,如代码示例所示。

所以,基本上:

CD cd = em.find(CD.class, this.cd);

或者,您也可以使用 HashMap 来代替,并使用 javax.faces.convert.Converter ,它基本上执行以下操作:

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    return em.find(CD.class, value);
}

public String getAsString(FacesContext context, UIComponent component, Object value) {
    return String.valueOf(((CD) value).getId());
}

另请参阅:


也就是说,HashMap 本质上是无序的。您确定您不需要 TreeMap (按键自动排序)或 LinkedHashMap (插入顺序)吗?

It will be set in the property behind #{backingBean.cd} as illustrated in your code example.

So, basically:

CD cd = em.find(CD.class, this.cd);

Alternatively, you can also have a HashMap<CD, CDName> instead and use a javax.faces.convert.Converter which does basically the following:

public Object getAsObject(FacesContext context, UIComponent component, String value) {
    return em.find(CD.class, value);
}

public String getAsString(FacesContext context, UIComponent component, Object value) {
    return String.valueOf(((CD) value).getId());
}

See also:


That said, a HashMap is by nature unordered. Are you sure you don't rather need a TreeMap (automatic sort by key) or a LinkedHashMap (insertion order)?

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