Salut ,
j´ai un probleme que je n´arrive pas a resourdre depuis deux semaines(je crois que beaucoup d´utilisateur utilisant primefaces et jsf auront ce meme probleme un jour).
je veux afficher le contenu de n´importe quel fichier csv dans une dataTable.(primefaces)
j´ai utilisé un parseur pour decouper le contenu de mon fichier en String que je mets immediatement dans une liste de String.

je ne sais pas comment mettre cette liste de String dans une datatable.

Exemple de contenu du fichier Employee.csv que j´aimerai avoir dans ma Datatable .

Job,gain,Age
"Informaticien",1200,34
"COmmercant",1000,42


Ma liste de String aura les elements [Job,gain,Age,"Informaticien",1200,34",COmmercant",1000,42]


Mon Bean:
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
60
61
 
@Named
@ViewScoped
public class EmployeeBean {
 
List<Employee>list;
Employee em;
 private UploadedFile file;
 public UploadedFile getFile() {
        return file;
    }
 
    public void setFile(final UploadedFile theFile) {
        file = theFile;
 
    }
 
public List<Employee> getList(){
return em;
}
public void setList(List<Employee> list){
this.list=list;
}
//mon parseur
public List<String> parseur() {
 
              List<String> a=new ArrayList<>();
        try {
            // get the uploaded file
            InputStream inputStream = file.getInputstream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 
            // define the byte size
            byte bufferZone[] = new byte[1024];
            int index;
 
            // read CSV
            while ((index = inputStream.read(bufferZone, 0, (int) file.getSize())) != -1) {
                byteArrayOutputStream.write(bufferZone, 0, index);
            }
            // assign it to string
            String cvs = new String(byteArrayOutputStream.toByteArray());
 
            StringTokenizer st = new StringTokenizer(cvs, ",");
 
            while (st.hasMoreTokens()) {
 
              a.add(st.nextToken());
 
            }
 
        } catch (Exception e) {
 
            e.printStackTrace();
 
        }
       return a;
 
    }
 
}
Ma classe Employee
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
 
 
public class Employee{
 
String job;
int gain;
int age;
 
 public String getJob() {
        return job;
    }
 
    public void setJob(String job) {
        this.job = job;
    }
 
    public int getGain() {
        return gain;
    }
 
    public void setGain(int gain) {
        this.gain = gain;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }




Primeface utilise les colonnes mais moi j´ai une Liste de String (List<String>).
Mon FICHIER 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
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
 
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
 
    <h:head></h:head>
 
 
.....
....
 
     <h:form enctype="multipart/form-data">
                    <h:panelGrid columns="1">
 
                                               <p:fileUpload id="up" value="#{employeeBean.file}" mode="simple"  label="choisir"  skinSimple="true" />
 
                        <p:commandButton styleClass="btn btn-default" ajax="false" value="telecharger"
                                         update="page-container" action="#{employeeBean.parseur}"/>
 
                  </h:panelGrid> 
                </h:form>
 
 
<h:form>
 
<p:dataTable var="e" value="#{employeeBean.list}">
    <p:column headerText="Job">
        <h:outputText value="#{e.job}" />
    </p:column>
 
    <p:column headerText="Gain">
        <h:outputText value="#{e.gain}" />
    </p:column>
 
    <p:column headerText="Age">
        <h:outputText value="#e.age}" />
    </p:column>
 
</p:dataTable>
 
 
</h:form>
 
.....
....
Toute aide est la Bienvenue.

Cordialement

Peanuts2.0