如何在JSF中的h:dataTable中显示来自其他db表的数据?

时间:2011-03-31 07:45:35

标签: java jsf

我有一个绑定到数据库表的数据表。在每一行中,我想根据当前行ID显示来自其他表的数据(我会这样做)。我怎样才能做到这一点?例如,如果我有用户的表,我想在每个表行中显示用户的所有组。如何创建在每个表行上调用的方法?

1 个答案:

答案 0 :(得分:0)

您可以将开头的所有必需数据加载到您自己的数据模型中,也可以实现自己的List以在数据表中使用。列表可以根据您的标准加载行数据。类似的东西:

public class MyList extends AbstractList<MyPageEntry> {
    public static Logger log = Logger.getLogger(MyList.class.getName());

    private int total = 0;
    private int pageSize = 100;

    private int pageNumber = 0;
    private List<MyPage> pages = new ArrayList<MyPage>();
    private final DBConnection connection;

    public PageableDebugList(SpiderConnection connection, String sourcename, String debugFilterValue) {
        super();
        this.connection = connection;
        fetchPage(0);
    }

    private synchronized void fetchPage(int pageNumber) {
        int start = pageNumber * pageSize;
        try {
                            // possibly add other criteria here 
            MyPage page = connection.getPage(start, pageSize,...);
            while (pages.size() < pageNumber + 1) {
                pages.add(null);
            }
            pages.set(pageNumber, page);

            total = page.getTotalSize();    

            if (false) {
                preFetchNext(pageNumber + 1, 1);
            }
            log.fine(page.getSize() + " " + total);
        } catch (Exception ex) {
            log.warning("Error fetching page - " + ex);
        }
    }

    public void reset() {
        pages = new ArrayList<Page>();
    }   

    private boolean hasPage() {
        return pages.size() >= pageNumber + 1 && pages.get(pageNumber) != null;
    }

    public boolean isRowAvailable(int rowIndex) {
        if (!hasPage())
            return false;
        if (total > rowIndex && rowIndex >= 0)
            return true;
        else
            return false;
    }

    @Override
    public synchronized MyPageEntry get(int index) {
        pageNumber = index / pageSize;
        if (!hasPage()) {
            try {
                fetchPage(Math.abs(pageNumber));
            } catch (Exception e) {
                log.warning(e.toString());
            }
        }
        int localIndex = index % pageSize;
        if (isRowAvailable(index)) {
            List<PageEntry> resultList = pages.get(pageNumber).getEntries();
            if (localIndex < resultList.size()) {
                MyPageEntry result = resultList.get(localIndex);
                return result;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }   

    @Override
    public int size() {
        return total;
    }   

    private void preFetchNext(final int start, final int count) {
        Thread t = new Thread() {
            @Override
                public void run() {
                for (int i = start; i < start + count; i++) {
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        log.warning(e.toString());
                    }       

                    try {
                        fetchPage(i);
                    } catch (Exception e) {
                        log.warning(e.toString());
                    }
                }
            }
        };
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public List<DebugLinkPage> getPages() {
        return pages;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageSize() {
        return pageSize;
    }

}

在这种情况下,一次加载整个页面。但是你也可以在get()方法中进行提取,并在访问它时加载每一行。