Intégrer hibernate dans une appli struts
Salut,
j'ai besoin d'aide pour arriver à intégrer hibernate à une application struts très simpliste. En fait j'ai commencé Hibernate il y a quelques jours pour ensuite modifier une grosse appli basé sur struts en y intégrant hibernate.
En gros, voilà le code très simple de l'existant tiré d'un tutorial que j'avais trouvé sur le net ya un petit moment :
struts-config.xml :
Code:
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
|
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean
name="frmPersonne"
type="org.apache.struts.validator.DynaValidatorForm"
>
<form-property name="pseudo" type="java.lang.String" initial="" />
<form-property name="mdp" type="java.lang.String" initial="" />
<form-property name="nom" type="java.lang.String" initial="" />
<form-property name="prenom" type="java.lang.String" initial="" />
<form-property name="age" type="java.lang.String" initial="" />
</form-bean>
</form-beans>
<action-mappings>
<action
path="/validerFormulaire1"
name="frmPersonne"
scope="session"
validate="true"
input="/erreurs.do"
type="FormulaireAction"
>
<forward name="reponse" path="/resultat.do" />
</action>
<action
path="/erreurs"
parameter="/vues/erreurs.personne.jsp"
type="org.apache.struts.actions.ForwardAction"
/>
<action
path="/formulaire"
parameter="/vues/formulaire.personne.jsp"
type="org.apache.struts.actions.ForwardAction"
/>
<action
path="/resultat"
parameter="/vues/resultat.personne.jsp"
type="org.apache.struts.actions.ForwardAction"
/>
</action-mappings>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"
/>
</plug-in>
<message-resources parameter="ressources.personneressources" null="false"/>
</struts-config> |
FormulaireAction.java :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
import...
public class FormulaireAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
DynaActionForm formulaire = (DynaActionForm)form;
request.setAttribute("pseudo", formulaire.get("pseudo"));
request.setAttribute("mdp", formulaire.get("mdp"));
request.setAttribute("nom",formulaire.get("nom"));
request.setAttribute("prenom", formulaire.get("prenom"));
request.setAttribute("age",formulaire.get("age"));
return mapping.findForward("reponse");
}
} |
Donc voilà, j'aimerais en fait qu'à la validation du formulaire, les données soient envoyées vers ma bdd (mysql ici en l'occurence).
J'avais pensé à faire ça dans le FormulaireAction.java, en rajoutant un truc du genre :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Personne p = new Personne();
p.setPseudo((String) request.getAttribute("pseudo"));
p.setMdp((String) request.getAttribute("mdp"));
p.setNom((String) request.getAttribute("nom"));
p.setPrenom((String) request.getAttribute("prenom"));
p.setAge((String) request.getAttribute("age"));
session.save(p);
session.getTransaction().commit(); |
Bon, je m'attendais pas à un miracle, ça ne fonctionne évidemment pas.
Mon fichier hibernate.cfg.xml :
Code:
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
|
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/formulairePerso</property>
<property name="connection.username">root</property>
<property name="connection.password"></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">create</property>-->
<mapping resource="Personne.hbm.xml"/>
</session-factory>
</hibernate-configuration> |
Personne.hbm.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Personne" table="PERSONNE">
<id name="id" column="PERSONNE_ID">
<generator class="increment" />
</id>
<property name="pseudo" table="PSEUDO" >
<property name="mdp" column="MDP" />
<property name="nom" column="NOM" />
<property name="prenom" column="PRENOM" />
<property name="age" column="AGE" />
</class>
</hibernate-mapping> |
Ca fonctionne très bien dans une appli java servlet de base sans struts.
Le problème est que j'ai vraiment du mal à voir dans ma tête comment organiser tout ça avec struts.
Les fichiers sont bien placés je pense, dans le dossier WEB-INF on retrouve struts-config.xml, web.xml, les dtd de struts et hibernate.
Dans WEB-INF/classes, on retrouve les .class des classes .java, le hibernate.cfg.xml, le Personne.hbm.xml et le log4j.properties.
Les JSP sont dans le dossier racine de l'appli.
Si je pouvais avoir un coup de patte, ça serait sympa.
Ah oui, je tourne avec du struts 1.3.x et hibernate 3 (et tomcat 6)
Merci