Interfacer Struts + Hibernate
Bonjour,
Je developpe une petite appli pour tester Struts et Hibernate. J'ai une page de login :
Voici le code de AccueilForm dans la Vue:
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
|
public class AccueilForm extends ActionForm {
/** Déclaration des variables ***************************************************/
private String login = null;
private String password = null;
/** Accesseurs ******************************************************************/
public String getLogin() {
return login;
}
public void setLogin( String login ){
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword( String password ){
this.password = password;
}
} |
Voici le code de AccueilAction dans le controleur :
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
|
public class AccueilAction extends Action {
/** Méthode ActionForward */
public ActionForward execute (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response ){
AccueilForm accueilForm = ( AccueilForm ) form;
Login login = new Login();
try {
BeanUtils.copyProperties( login, accueilForm );
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
request.setAttribute(mapping.getAttribute(), login);
if ( login.getLogin().equals("login") && login.getPassword().equals("password") ) {
return mapping.findForward("succes_accueil_mediatheque");
}
else {
return mapping.findForward("erreur_accueil_mediatheque");
}
}
} |
C'est au niveau de la Class Login que je dois implementer Hibernate. ET là je ne voisp as bien comment faire. Quelqu'un pourrait m'aiguiller ?
Merci d'avance.
Exemple simple : verifier un couple login/password dans la base
Bon j'ai pas mal avancé dans la mise en place d'Hibernate. Mais là je bute. Du coup je reviens demander de l'aide :
Voici ma Class User correspondant à ma table user dans ma DB :
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 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
|
// Generated 4 juin 2008 18:22:46 by Hibernate Tools 3.2.1.GA
/**
* package
*/
package modele.dao;
/**
* imports
*/
import java.util.HashSet;
import java.util.Set;
/**
* User generated by hbm2java
*/
public class User implements java.io.Serializable {
private Long idUser;
private String nomUser;
private String prenomUser;
private String emailUser;
private String loginUser;
private String passwordUser;
private Set empruntes = new HashSet(0);
private Set medias = new HashSet(0);
public User() {
}
public User(String nomUser, String prenomUser, String emailUser,
String loginUser, String passwordUser) {
this.nomUser = nomUser;
this.prenomUser = prenomUser;
this.emailUser = emailUser;
this.loginUser = loginUser;
this.passwordUser = passwordUser;
}
public User(String nomUser, String prenomUser, String emailUser,
String loginUser, String passwordUser, Set empruntes, Set medias) {
this.nomUser = nomUser;
this.prenomUser = prenomUser;
this.emailUser = emailUser;
this.loginUser = loginUser;
this.passwordUser = passwordUser;
this.empruntes = empruntes;
this.medias = medias;
}
public Long getIdUser() {
return this.idUser;
}
public void setIdUser(Long idUser) {
this.idUser = idUser;
}
public String getNomUser() {
return this.nomUser;
}
public void setNomUser(String nomUser) {
this.nomUser = nomUser;
}
public String getPrenomUser() {
return this.prenomUser;
}
public void setPrenomUser(String prenomUser) {
this.prenomUser = prenomUser;
}
public String getEmailUser() {
return this.emailUser;
}
public void setEmailUser(String emailUser) {
this.emailUser = emailUser;
}
public String getLoginUser() {
return this.loginUser;
}
public void setLoginUser(String loginUser) {
this.loginUser = loginUser;
}
public String getPasswordUser() {
return this.passwordUser;
}
public void setPasswordUser(String passwordUser) {
this.passwordUser = passwordUser;
}
public Set getEmpruntes() {
return this.empruntes;
}
public void setEmpruntes(Set empruntes) {
this.empruntes = empruntes;
}
public Set getMedias() {
return this.medias;
}
public void setMedias(Set medias) {
this.medias = medias;
}
} |
Voici maintenant ma Class HibernateUtil :
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 63 64 65 66 67
|
/**
* package
*/
package utilitaires.hibernate;
/**
* imports
*/
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* La classe HibernateUtil ( SessionFactory ) fournit des objets
* Session permettant de manipuler les données.
* @author
*/
public class HibernateUtil {
// Déclaration de la SessionFactory
private static final SessionFactory sessionFactory;
static {
try {
// On crée ici la SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch ( HibernateException hE ) {
throw new RuntimeException( " Problème de configuration " + hE.getMessage(), hE );
}
}
// Déclaration du ThreadLocal
public static final ThreadLocal session = new ThreadLocal();
/**
* Méthode currentSession()
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session s = ( Session ) session.get();
// Ouvre un nouvelle Session si ce thread n'en a aucune
if ( s == null ) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
/**
* Méthode closeSession()
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session s = ( Session )session.get();
session.set(null);
if ( s != null ) {
s.close();
}
}
} |
J'ai crée une Class LoginHibernate pour tester l'implémentation d'hibernate. En commentaires, les points ou je plante :
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
|
/**
* package
*/
package utilitaires.hibernate;
/**
* imports
*/
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import modele.dao.User;
/**
* @author
*
*/
public class LoginHibernate {
public boolean valideIdentification (String login, String password ) {
String loginVue = login;
String passwordVue = password;
String loginBase = null;
String passwordBase = null;
boolean identification = false;
Transaction tX = null;
Session session = HibernateUtil.currentSession();
try {
tX = session.beginTransaction();
// Je ne sais pas ici comment récupérer les attributs login et password du user depuis la base ....
// On contrôle si le couple login - password existe bien
if ( loginBase == loginVue && passwordBase == passwordVue ) {
identification = true;
}
else {
identification = false;
}
}
catch ( HibernateException hE ) {
hE.printStackTrace();
}
return identification;
}
} |
Merci de votre aide ....
PS : Pas évident Hibernate dans les débuts ....