简体   繁体   English

JSF-如何使用数据库行值在数据表中生成其他列

[英]JSF - How to use database row values to generate additional columns in datatable

I have a problem which should be easy but I have trouble with finding the solution. 我有一个应该很容易解决的问题,但是在寻找解决方案时遇到了麻烦。 I have a database that I want to present in a datatable. 我有一个要在数据表中显示的数据库。 However I need additional columns that get more information about the current row. 但是,我需要其他列来获取有关当前行的更多信息。 For example - the database has a set of people with id numbers. 例如-数据库中有一组具有ID号的人。 What I need is to display the Name, Last Name, ID (all columns of database), but then I want to display the address which is in a different REST webservice and to get it I need to send the ID (from database). 我需要显示名称,姓氏,ID(数据库的所有列),但是随后我想显示位于不同REST Web服务中的地址,并且要获取它,我需要从数据库中发送ID。 I hope I have described it clearly but just in case I will point out: 我希望我已经清楚地描述了它,但以防万一我会指出:

-my database has 3 columns: name, las name, id number -I need to add a 4th column to my datatable with address -to get the address I need to send the id number to a rest webservice -我的数据库有3列:名称,名称,ID号-我需要在地址表中添加第4列-要获取地址,我需要将ID号发送到其他Web服务

The only solution I was able to find so far i to add all database elements to a list or some container and then use it inside a bean. 到目前为止,我唯一能找到的解决方案是将所有数据库元素添加到列表或某个容器中,然后在Bean中使用它。 But that is unacceptable since the database is very big. 但这是不可接受的,因为数据库很大。 There must be a simpler way but it seems that I can't form an adequate question for google to get the proper results :> Can any one give some advice? 必须有一种更简单的方法,但似乎我无法为Google提出适当的问题以获取正确的结果:>有人可以提出建议吗?

So what you basically want is to do a join between the result of a query to a database and the webservice. 因此,您基本上想要在数据库查询结果和Web服务之间进行联接。

Of course you don't need to load the entire DB in some bean to do this "join", just load the data you would like to display on screen in a backing bean, load the addresses for each row, and bind the result to the datatable. 当然,您不需要将整个数据库加载到某个bean中即可执行此“连接”操作,只需加载您希望在屏幕上显示的数据在后备bean中,加载每一行的地址,并将结果绑定到数据表。

Something like this: 像这样:

@ManagedBean
@ViewScoped
public class MyBean {

    @EJB
    private someDataService;

    @EJB
    private someWebService;

    List<Person> persons;
    Map<Long, String> addresses;

    @PostConstruct
    public void retrieveData() {
        persons = someDataService.getByID(someID);
        for (Person person : persons) {
            addresses.put(person.getID(), someWebService.getByID(person.getID));
        }
    }

    // getters here
}

And the Facelet: 和方面:

<h:dataTable value="#{myBean.persons}" var="person">
    <h:column>
        #{person.name}
    </h:column>
    <h:column>
        #{person.lastName}
    </h:column>
    <h:column>
        #{myBean.addresses[person.ID]}      
    </h:column>             
</h:dataTable>

There are some choices to be made. 有一些选择。 You could also do the joining inside a single service and have it return some type that includes the address. 您也可以在单个服务中进行联接,并使其返回某种包含地址的类型。 You could also make a wrapper or a decorator. 您也可以制作包装纸或装饰纸。

It would also be more efficient if the web service could handle a list of IDs instead of requiring a separate call for each address fetched. 如果Web服务可以处理ID列表而不是为获取的每个地址要求单独的调用,也将更加有效。 You might also wanna do some local caching, etc, but those details are up to you ;) 您可能还想做一些本地缓存等,但是这些细节由您决定;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM