Bonjour,
Je suis débutante en Struts2 et je travaille sur une application qui intègre Hibernate4 et Struts2.
j'essaye d'afficher dans ma page jsp une liste déroulante qui se charge à partir du Base de données,mais sans résultats.
Par contre, si je charge la liste manuellement, ça marche très bien.
Voici l'erreur :
org.hibernate.HibernateException: /Hibernate.cfg.xml not found
org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)
org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2033)
org.hibernate.cfg.Configuration.configure(Configuration.java:2014)
ensa.pfe.util.HibernateUtil.configureSessionFactory(HibernateUtil.java:14)
ensa.pfe.util.HibernateUtil.getSessionFactory(HibernateUtil.java:21)
ensa.pfe.service.Impl.CodificationServiceImpl.getCodByType(CodificationServiceImpl.java:49)
ensa.pfe.action.GestionVehicules.prepare(GestionVehicules.java:35)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:167)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:139)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:193)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:189)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
ma page jsp :
hibernateUtile.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 <%@taglib uri="/struts-tags" prefix="s"%> <%@page import="org.omg.CORBA.PRIVATE_MEMBER"%> <%@ page language="java" contentType="text/html; charset=utf-8" 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <s:form action="Ajout" namespace="/" > <s:textfield name="immatricul" label="immatriculation" value="%{immatricul}"></s:textfield> <s:textfield name="kilometrage" label="kilometrage" value="%{kilometrage}"></s:textfield> <s:textfield name="date" label="date" value="%{date}"></s:textfield> <s:textfield name="etat" label="etat" value="%{etat}"></s:textfield> <s:select name="marques" label="Marque" headerValue="Select" list="marques"></s:select> <s:submit name="Valider" value="Valider"></s:submit> </s:form> </body> </html>
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 import org.hibernate.*; import org.hibernate.cfg.*; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtil { private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; private static SessionFactory configureSessionFactory() throws HibernateException { Configuration configuration = new Configuration(); configuration.configure("/Hibernate.cfg.xml"); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; } public static SessionFactory getSessionFactory() { return configureSessionFactory(); } }
struts.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 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>PfeParcAuto</display-name> <welcome-file-list> <welcome-file>Index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> ensa.pfe.util.HibernateUtil </listener-class> </listener> </web-app>
Hibernate.cfg.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 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!--<default-action-ref name="index"/>--> <package name="default" namespace="/" extends="struts-default"> <action name="GestionVehicules" class="ensa.pfe.action.GestionVehicules" method="Gestionv"> <result name="success">pages/AjoutVehicule.jsp</result> </action> <action name="test" class="ensa.pfe.action.test" method="execute"> <result name="success">pages/Index.jsp</result> </action> </package> </struts>
l'action :
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 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db_parc</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">1111</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="ensa/pfe/dao/Vehicule.hbm.xml"/> <mapping resource="ensa/pfe/dao/Operation.hbm.xml"/> <mapping resource="ensa/pfe/dao/Alerte.hbm.xml"/> <mapping resource="ensa/pfe/dao/Message.hbm.xml"/> <mapping resource="ensa/pfe/dao/Codification.hbm.xml"/> <mapping resource="ensa/pfe/dao/Tiers.hbm.xml"/> <mapping resource="ensa/pfe/dao/Utilisateur.hbm.xml"/> <mapping resource="ensa/pfe/dao/Chauffeur.hbm.xml"/> </session-factory> </hibernate-configuration>
Le service d’implémentation :
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 import java.util.ArrayList; import java.util.Date; import java.util.List; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.Preparable; public class GestionVehicules extends ActionSupport implements Preparable{ private String immatricul; private Double kilometrage; private Date date; private String etat; private Double puissance; private Double poids_vide; private List<String> marques; private CodificationService codificationService; private ChauffeurService chauffeurService; @Override public void prepare() throws Exception { codificationService =new CodificationServiceImpl(); marques = new ArrayList<String>(); marques = codificationService.getCodByType("Marque"); for (int j = 0; j < marques.size(); j++) { System.out.println(marques.get(j)); } /*marques = new ArrayList<String>(); marques.add("1"); marques.add("2"); marques.add("3");*/ } public String Gestionv() throws Exception { return "success"; } public String getImmatricul() { return immatricul; } public void setImmatricul(String immatricul) { this.immatricul = immatricul; } public Double getKilometrage() { return kilometrage; } public void setKilometrage(Double kilometrage) { this.kilometrage = kilometrage; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getEtat() { return etat; } public void setEtat(String etat) { this.etat = etat; } public Double getPuissance() { return puissance; } public void setPuissance(Double puissance) { this.puissance = puissance; } public Double getPoids_vide() { return poids_vide; } public void setPoids_vide(Double poids_vide) { this.poids_vide = poids_vide; } public List<String> getMarques() { return marques; } public void setMarques(List<String> marques) { this.marques = marques; } public CodificationService getCodificationService() { return codificationService; } public void setCodificationService(CodificationService codificationService) { this.codificationService = codificationService; } public ChauffeurService getChauffeurService() { return chauffeurService; } public void setChauffeurService(ChauffeurService chauffeurService) { this.chauffeurService = chauffeurService; } }
Il pense que le problème vient de l’intégration du struts avec hibernate parce que la liste déroulante chargée manuellement s'affiche sans aucun problème.
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 import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import ensa.pfe.dao.Codification; import ensa.pfe.service.CodificationService; import ensa.pfe.util.HibernateUtil; public class CodificationServiceImpl implements CodificationService { @Override public void AddCodification(Codification cod) { try { Session s = HibernateUtil.getSessionFactory().getCurrentSession(); s.beginTransaction(); s.save(cod); s.getTransaction().commit(); } catch (Exception ex) { ex.printStackTrace(); } } @Override public void deletCodification(Integer idCod) { Session s = HibernateUtil.getSessionFactory().getCurrentSession(); s.beginTransaction(); Codification v = (Codification) s.load(Codification.class, idCod); s.delete(v); s.getTransaction().commit(); } @Override public Codification updateCodification(Codification cod) { // TODO Auto-generated method stub return null; } @SuppressWarnings("unchecked") @Override public List<String> getCodByType(String type) { List<Codification> l; List<String> S = new ArrayList<String>(); Session s = HibernateUtil.getSessionFactory().getCurrentSession(); s.beginTransaction(); l= s.createQuery("from Codification v where v.type= '"+ type + "'").list(); for (int i = 0; i < l.size(); i++) S.add(l.get(i).getDesignation()); return S; } }
J'ai besoin de votre aide , Merci d'avance.
Partager