我正在尝试在 PrimeFaces 库中使用 Tree 组件,但由于数据量很大,我必须懒惰地加载它。我按照教程进行操作(http://blog.disy.net/primefaces-lazy-tree/),但加载需要 15 秒(而不是 23 秒)。我在本教程或我的代码中遗漏了什么吗?我不能这样,因为 15 秒对用户来说太长了。
代码:
public interface TreePodeFindWithParent {
List<VArboParObjectifsParents> findActiviteWithParent(Integer parentId);
}
延迟加载类中的方法
public class PodeLazyTreeNode extends DefaultTreeNode
{
...
...
public PodeLazyTreeNode(VArboParObjectifsParents data, TreePodeService service)
{
super(vArboParObjectifsParents.class.getSimpleName(), data, null);
this.service = service;
}
...
private void ensureChildrenFetched()
{
if (!childrenFetched)
{
childrenFetched = true;
if ((VArboParObjectifsParents)getData() != null)
{
Integer parentId = ((VArboParObjectifsParents)getData()).getIdRoot();
List<PodeLazyTreeNode> childNodes = service.findActiviteWithParent(parentId).stream().map(item
-> new PodeLazyTreeNode(item, service)).collect(Collectors.toList());
super.getChildren().addAll(childNodes);
}
}
}
服务中的方法
public class TreePodeService implements Serializable, TreePodeWithParent
{
...
...
@Inject
private PodeArboParObjectifsParentsDao podeArboObjParentDao;
...
@Override
public List<VArboParObjectifsParents> findActiviteWithParent(Integer parentId) {
// TODO Auto-generated method stub
return podeArboObjParentDao.findByIdParent(parentId);
}
DAO(使用 Apache DeltaSpike 的数据模块完成请求):
@Repository(forEntity=VArboParObjectifsParents.class)
public interface PodeArboParObjectifsParentsDao extends EntityRepository<VArboParObjectifsParents, Integer>
{
List<VArboParObjectifsParents> findByIdParent(Integer idParent);
List<VArboParObjetcifsParents> findByIdTypeActivite(Integer idType);
}
视图中方法的调用:
@PostConstruct
public void initView()
{
initArbo();
}
public void initArbo()
{
List<VArboParObjectifsParents> vArbos = treePodeService.getPodeArboObjParentDao().findByIdTypeActivite(1);
this.root = new PodeLazyTreeNode(null, treePodeService);
for (int i = 0; i < vArbos.size(); i++)
{
root.getChildren().add(new PodeLazyTreeNode(vArbos.get(i), treePodeService));
}
}
用户界面:
<p:tree value="#{testView.root}" var="_node" >
<p:treeNode type="VArboParObjectifsParents">
<h:outputText value="#{_node}"/>
</p:treeNode>
</p:tree>