Librairies & Outils utilisés :
1- JavaServer Faces 2.0
2- RichFaces 3.3.3
3- Tomcat 6.0.18
Web.xml :
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 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <context-param> <param-name>org.richfaces.SKIN</param-name> <param-value>japanCherry</param-value> </context-param> <context-param> <param-name>org.richfaces.CONTROL_SKINNING</param-name> <param-value>enable</param-value> </context-param> <filter> <display-name>RichFaces Filter</display-name> <filter-name>richfaces</filter-name> <filter-class>org.ajax4jsf.Filter</filter-class> </filter> <filter-mapping> <filter-name>richfaces</filter-name> <servlet-name>Faces Servlet</servlet-name> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher> </filter-mapping> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
index.jsp :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <%@page pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>index Of...</title> </head> <body> <jsp:forward page="Forms/sondageForm.jsf"/> </body> </html>
sondageForm.jsp :
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 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@taglib uri="http://richfaces.org/rich" prefix="rich" %> <%@taglib uri="http://richfaces.org/a4j" prefix="ajax" %> <f:view> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Sondage</title> </head> <body> <ajax:form> <rich:panel header="Sondage" style="width: 220px; height: 310px;"> <h:selectOneRadio id="ANIMAL" layout="pageDirection" value="#{animalBean.name}"> <f:selectItem itemLabel="Chat" itemValue="Chat" /> <f:selectItem itemLabel="Chien" itemValue="Chien" /> <f:selectItem itemLabel="Lapin" itemValue="Lapin" /> <f:selectItem itemLabel="Ours" itemValue="Ours" /> <f:selectItem itemLabel="Dauphin" itemValue="Dauphin" /> <f:selectItem itemLabel="Lion" itemValue="Lion" /> <f:selectItem itemLabel="Singe" itemValue="Singe" /> <f:selectItem itemLabel="Cheval" itemValue="Cheval" /> <f:selectItem itemLabel="Tigre" itemValue="Tigre" /> <f:selectItem itemLabel="Aucun" itemValue="Aucun" /> </h:selectOneRadio><br/> <ajax:commandButton value="Valider" action="#{animalBean.doValidate}" /><rich:spacer width="15px" /> <ajax:commandButton value="Rétablir" action="#{animalBean.doReset}" reRender="ANIMAL" /> </rich:panel> </ajax:form> </body> </html> </f:view>
sondageView.jsp :
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 <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@taglib uri="http://richfaces.org/rich" prefix="rich" %> <f:view> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Résultats</title> </head> <body> <center><br/><br/><h:outputText value="RÉSULTAT DU SONDAGE" style="font-family:verdana; font-weight: bold; font-size:40px;" /></center><br/><br/> <center> <rich:dataTable value="#{animalBean.lab.list}" var="animalBean" columns="3" style="width: 310px;"> <rich:column width="130px"><f:facet name="header"><h:outputText value="ANIMAL" /></f:facet><h:outputText value="#{animalBean.name}" /></rich:column> <rich:column width="80px"><f:facet name="header"><h:outputText value="VOTE" /></f:facet><center><h:outputText value="#{animalBean.nbVotes}" /></center></rich:column> <rich:column width="100px"><f:facet name="header"><h:outputText value="TAUX" /></f:facet><div align="right"><h:outputText value="#{animalBean.percent}" /></div></rich:column> <f:facet name="footer"><f:verbatim escape="false"><center>MERCI POUR VOTRE PARTICIPATION ..</center></f:verbatim></f:facet> </rich:dataTable> </center> </body> </html> </f:view>
AnimalBean.java :
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 /** * @author Gyles **/ package gyles.beans.ext; import java.io.Serializable; import gyles.utils.ext.JSFUtilities; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean @RequestScoped public class AnimalBean extends Object implements Serializable { private int nbVotes; private String percent; private ListAnimalBean lab; private String name = "Aucun"; public AnimalBean() { } public AnimalBean(String name, int nbVotes, String percent) { this.name = name; this.nbVotes = nbVotes; this.percent = percent; } public String doValidate() { if (lab == null) lab = new ListAnimalBean(); for (int i = 0; i < JSFUtilities.getAnimalNames().length; i++) { if (name.equals(JSFUtilities.getAnimalNames()[i])) { lab.getList().set(i, new AnimalBean(name, lab.getList().get(i).getNbVotes() + 1, "0,00")); break; } } for (int i = 0; i < JSFUtilities.getAnimalNames().length; i++) { lab.getList().set(i, new AnimalBean(lab.getList().get(i).getName(), lab.getList().get(i).getNbVotes(), JSFUtilities.calcPercent(lab.getList().get(i).getNbVotes(), lab.getTotal()) + " %")); } JSFUtilities.saveFile(lab.getList()); return "Views/sondageView"; } public String doReset() { name = "Aucun"; return null; } public void setName(String name) { this.name = name; } public void setNbVotes(int nbVotes) { this.nbVotes = nbVotes; } public void setPercent(String percent) { this.percent = percent; } public void setLab(ListAnimalBean lab) { this.lab = lab; } public String getName() { return name; } public int getNbVotes() { return nbVotes; } public String getPercent() { return percent; } public ListAnimalBean getLab() { return lab; } }
ListAnimalBean.java :
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 /** * @author Gyles **/ package gyles.beans.ext; import java.util.List; import java.util.ArrayList; import gyles.utils.ext.JSFUtilities; public class ListAnimalBean extends Object { private int total; private List<AnimalBean> list; public ListAnimalBean() { if (list == null) { if (JSFUtilities.isExistedFile()) { list = JSFUtilities.loadFile(); }else { list = new ArrayList<AnimalBean>(); for (int i = 0; i < JSFUtilities.getAnimalNames().length; i++) { list.add(new AnimalBean(JSFUtilities.getAnimalNames()[i], 0, "0,00")); } } } } public void setList(List<AnimalBean> list) { this.list = list; } public void setTotal(int total) { this.total = total; } public List<AnimalBean> getList() { return list; } public int getTotal() { total = 0; for (int i = 0; i < list.size(); i++) { total += ((AnimalBean)list.get(i)).getNbVotes(); } return total; } }
JSFUtilities.java :
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 /** * @author Gyles **/ package gyles.utils.ext; import java.io.File; import java.util.List; import java.util.ArrayList; import java.io.IOException; import java.text.DecimalFormat; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import gyles.beans.ext.AnimalBean; import java.io.FileNotFoundException; public final class JSFUtilities extends Object { private JSFUtilities() { } public static void saveFile(List<AnimalBean> list) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File(".SONDAGE"))); oos.writeObject(list); oos.flush(); oos.close(); }catch(IOException exception) { exception.printStackTrace(); } } public static List<AnimalBean> loadFile() { List<AnimalBean> list = null; try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File(".SONDAGE"))); list = (ArrayList<AnimalBean>)ois.readObject(); ois.close(); }catch(FileNotFoundException fnfe) { fnfe.printStackTrace(); }catch(ClassNotFoundException cnfe) { cnfe.printStackTrace(); }catch(IOException exception) { exception.printStackTrace(); } return list; } public static boolean isExistedFile() { return new File(".SONDAGE").exists() ? true : false; } public static String calcPercent(int nbVotes, int total) { return new DecimalFormat("#0.00").format((double)((double)nbVotes * 100.0) / (double)total); } public static String[] getAnimalNames() { return new String[] {"Chat","Chien","Lapin","Ours","Dauphin", "Lion","Singe","Cheval","Tigre","Aucun"}; } }
Partager