如何在jsf中显示多对多的jpa集合? [英] How to display many to many jpa collection in jsf?

查看:51
本文介绍了如何在jsf中显示多对多的jpa集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户表和组表之间建立了多对多联接表. 所以我在每个实体(用户和组)中都有一个收藏集

i made many to many join table between users and groups tables . so i have a collection in each entitie ( Users and groups )

@ManyToMany(mappedBy = "usersCollection")
    private Collection<Groups> groupsCollection;

我想在Jsf中显示群组集合,这就是我所做的:

and i want to display groups collection in Jsf thats what i did :

<p:dataTable var="user" value="#{usergestion.tableusers}">  
                           <p:column headerText="username">  
                               <h:outputText value="#{user.username}" />  
                           </p:column>  

                           <p:column headerText="nom">  
                               <h:outputText value="#{user.nom}" />  
                           </p:column>  

                           <p:column headerText="prenom">  
                               <h:outputText value="#{user.prenom}" />  
                           </p:column>

                            <p:column headerText="groupe"> 

                                <h:outputText value="#{user.groupsCollection.get(0)}"  />

                           </p:column>

那我得到的是:

我如何仅获得nombre而不是com.database.Groups [idGroups = 2] ???

how i can get just the nombre not com.database.Groups[ idGroups=2 ] ???

解决方案:

我使用过:<h:outputText value="#{user.groupsCollection.get(0).groupname}" />

推荐答案

通常使用迭代组件,例如<ui:repeat><h:dataTable>来迭代集合.您可以将其完美地嵌套在另一个迭代组件中.

You normally use an iterating component such as <ui:repeat> or <h:dataTable> to iterate over a collection. You can perfectly nest it inside another iterating component.

例如

<p:column headerText="groupe"> 
    <ui:repeat value="#{user.groupsCollection}" var="groups">
        <h:outputText value="#{groups.groupname}" /><br/>
    </ui:repeat>
</p:column>


无关与具体问题无关,您在命名约定方面有些欠佳.一组应该由名为Group而不是Groups的类表示.我建议重命名一个和另一个,以便使代码变得更加自我记录:


Unrelated to the concrete problem, you've there some poor naming convention. One group should be represented by a class named Group, not Groups. I suggest to rename the one and other so that the code becomes so much more self documenting:

<p:column headerText="groups"> 
    <ui:repeat value="#{user.groups}" var="group">
        <h:outputText value="#{group.name}" /><br/>
    </ui:repeat>
</p:column>

字段/变量名称中的复数s应该已经表明它与集合有关.

The plural s in the field/variable name should already be indicative that it concerns a collection.

这篇关于如何在jsf中显示多对多的jpa集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆