开发者

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. 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). 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 开发者_C百科to my datatable with address -to get the address I need to send the id number to a rest webservice

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. 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?


So what you basically want is to do a join between the result of a query to a database and the webservice.

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.

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. You might also wanna do some local caching, etc, but those details are up to you ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜