Bonjours,

Je m'entraine en JSF, je n'ai pas de grande connaissance. j'essaye de faire une hiérarchie récursive. Voici mon code:

FileSystemBean:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
package managedBean;
 
 
public class FileSystemBean {
    private static String item = "0";
 
    private FileSystemNode[] srcRoots;
 
    public synchronized FileSystemNode[] getSourceRoots() {
        if (srcRoots == null) {
          srcRoots = new FileSystemNode(item,"0").getNodes();
        }
 
        return srcRoots;
    }
}
FileSystemeNode:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package managedBean;
 
import session.ConnexionBean;
 
public class FileSystemNode {
    private String item;
    private String mode;
 
    private static FileSystemNode[] CHILDREN_ABSENT = new FileSystemNode[0];
 
    private FileSystemNode[] children;
 
    private String nom_item;
 
    public FileSystemNode() {
 
    }
 
    public FileSystemNode(String item,String mode) {
        this.mode=mode;
        ConnexionBean co = new ConnexionBean();
        this.item =co.loadNewHiel(item,mode);
        if (children == null) {
              String[] items=this.item.split("\n");
 
                children = new FileSystemNode[items.length-1];
 
                for (int i = 0; i < items.length-1; i++) //-1 pour HIELFIN
                {
 
                    String[] champs=items[i].split("\t");
                    if(champs[5].equals("0"))
                    {
                      children[i] = new FileSystemNode(champs[0],this.mode);
 
                      System.out.println(champs[2]);
                    }
                    else
                    {
                      children[i] = new FileSystemNode();
                    }
                    children[i].setNom_item(champs[2]);
               }
        }
    }
 
    public synchronized FileSystemNode[] getNodes() {
        return children;
    }
 
    public String toString() {
        return nom_item;
    }
 
    public void setNom_item(String nom_item) {
        this.nom_item = nom_item;
    }
 
}
la fonction loadNewHiel me renvoi une ligne pour chaque fils de l'item passer en paramètre. Avec des informations séparées par des tabulation. Cette fonction marche bien.


PostVackPhaseListener:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package managedBean;
 
import java.util.Map;
 
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
 
public class PostbackPhaseListener implements PhaseListener {
 
    public static final String POSTBACK_ATTRIBUTE_NAME = PostbackPhaseListener.class.getName();
 
    public void afterPhase(PhaseEvent event) {
    }
 
    public void beforePhase(PhaseEvent event) {
        FacesContext facesContext = event.getFacesContext();
        Map requestMap = facesContext.getExternalContext().getRequestMap();
        requestMap.put(POSTBACK_ATTRIBUTE_NAME, Boolean.TRUE);
    }
 
    public PhaseId getPhaseId() {
        return PhaseId.APPLY_REQUEST_VALUES;
    }
 
    public static boolean isPostback() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (facesContext != null) {
            ExternalContext externalContext = facesContext.getExternalContext();
            if (externalContext != null) {
                return Boolean.TRUE.equals(
                        externalContext.getRequestMap().get(POSTBACK_ATTRIBUTE_NAME));
            }
        }
 
        return false;
    }
 
}
TreeDemoStateAvisor:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package managedBean;
 
import org.richfaces.component.UITree;
import org.richfaces.component.state.TreeStateAdvisor;
import org.richfaces.model.TreeRowKey;
 
public class TreeDemoStateAdvisor implements TreeStateAdvisor {
 
    public Boolean adviseNodeOpened(UITree tree) {
        if (!PostbackPhaseListener.isPostback()) {
            Object key = tree.getRowKey();
            TreeRowKey treeRowKey = (TreeRowKey) key;
            if (treeRowKey == null || treeRowKey.depth() <= 2) {
                return Boolean.TRUE;
            }
        }
 
        return null;
    }
 
    public Boolean adviseNodeSelected(UITree tree) {
        return null;
    }
 
}
index.xhtml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:a4j="http://richfaces.org/a4j"
      xmlns:rich="http://richfaces.org/rich"
      xmlns:c="http://java.sun.com/jstl/core" >
    <head>
        <title>TODO supply a title</title>
    </head>
    <body>
 
        <ui:composition>
    <h:form>
        <rich:tree style="width:300px" switchType="ajax" stateAdvisor="#{TreeDemoStateAdvisor}">
            <rich:recursiveTreeNodesAdaptor roots="#{fileSystemBean.sourceRoots}" var="item" nodes="#{item.nodes}" />
        </rich:tree>
    </h:form>
 
</ui:composition>
 
    </body>
</html>

Quand je lance l'application la page index m'envoie ceci:


Le souci c'est que la récursivité ne marche je devrai obtenir ceci:



De plus quand je clique sur les répertoires les fils de celui-ci ne disparait pas.

Je ne vois pas pourquoi la récursivité ne marche ni la fonction pour réduire les nœuds