【问题标题】:How to show data from other db table in h:dataTable in JSF?如何在 JSF 中的 h:dataTable 中显示来自其他 db 表的数据?
【发布时间】:2011-07-26 16:25:22
【问题描述】:

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

【问题讨论】:

  • 实际上,我正在尝试在“commandLink”中找到类似“action”的内容。我希望每一行都有一个动作,所以我可以显示我自己的自定义列数据。

标签: java jsf


【解决方案1】:

您可以在开始时将所有必需的数据加载到您自己的数据模型中,或者您可以实现自己的列表以在数据表中使用。该列表可以根据您的条件加载行数据。比如:

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() 方法中进行提取,并在访问时加载每一行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2020-11-03
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多