IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JSF Java Discussion :

JSF / Hibernate / MySql


Sujet :

JSF Java

  1. #1
    Membre averti
    Inscrit en
    Octobre 2009
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 23
    Par défaut JSF / Hibernate / MySql
    Bonsoir , je suis débutant en jsf et en hibernate et je veut développer une page web d'authentification ,elle contient deux champs login et mot de passe , donc je doit vérifier ces données entrées par l'user avec ceux qui se trouvent dans ma base de donnée(mysql) .
    Mon problème est comment je fait pour récupérer mes donnée de base de donnée pour les comparer avec celle entrée , pour savoir si j'envoie l'user vers son espace d'utilisateur ou vers la meme page d'authentification en signalant qu'il a fait une erreur de saisie .
    Voila comment j'ai fait :
    Le java beans PersonneBeans:

    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
    package com.thescreencast.web;
     
    import java.util.Iterator;
    import java.util.List;
     
    import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Session;
     
    public class PerssonneBean {
    	private String login;
    	/**
             * 
             */
    	private String password;
    	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;
    	}
       public String add() throws HibernateException{
    		 Session session = null;
    		try {
    			session = HibernateUtil.currentSession();
    		} catch (HibernateException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    	   List list = session.find("from Candidat");
    	   Iterator it = list.iterator();
    		/* Iterator it = list.iterator();
    		 while(it.hasNext())
    		 {
    		   Candidat c = (Candidat)it.next();
    		   System.out.println(c.getIdCandidat()+"-->" +c.getNom());
    		 }*/
    	  // if(login.equals("ff")& password.equals("ff"))
    		//if(it.hasNext())   
     
    	  // return "erreur";
    		return "succeC";
       }
     
     
    }

    Code xml : 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
    Voila la page jsf d'authentification authentificationC.jsp :
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
    <%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
    <!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>
    <f:view>
     
    	<h:form>
    		<h:panelGrid border="1" columns="2">
    			<h:outputText value="E-mail:"></h:outputText>
    			<h:inputText value="#{perssonneBean.login}" style="width: 188px"></h:inputText>
    			<h:outputText value="Mot de passe :"></h:outputText>
    			<h:inputText value="#{perssonneBean.password}" style="width: 189px"></h:inputText>
     
    		</h:panelGrid>
    		<h:commandButton value="valider" action="#{perssonneBean.add}"></h:commandButton>
    	</h:form>
    </f:view>
    </body>
    </html>

    ---------------------->faces-config.xml :
    Code xml : 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
    <?xml version="1.0" encoding="UTF-8"?>
     
    <faces-config
        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-facesconfig_1_2.xsd"
        version="1.2">
     
    	<managed-bean>
    		<managed-bean-name>candidat</managed-bean-name>
    		<managed-bean-class>com.thescreencast.web.Candidat</managed-bean-class>
    		<managed-bean-scope>session</managed-bean-scope>
    	</managed-bean>
    	<managed-bean>
    		<managed-bean-name>perssonneBean</managed-bean-name>
    		<managed-bean-class>com.thescreencast.web.PerssonneBean</managed-bean-class>
    		<managed-bean-scope>session</managed-bean-scope>
    	</managed-bean>
    	<navigation-rule>
    		<display-name>accueil</display-name>
    		<from-view-id>/accueil.jsp</from-view-id>
    		<navigation-case>
    			<from-outcome>candidat</from-outcome>
    			<to-view-id>/authentificationC.jsp</to-view-id>
    		</navigation-case>
    	</navigation-rule>
    	<navigation-rule>
    		<display-name>accueil</display-name>
    		<from-view-id>/accueil.jsp</from-view-id>
    		<navigation-case>
    			<from-outcome>entreprise</from-outcome>
    			<to-view-id>/authentificationE.jsp</to-view-id>
    		</navigation-case>
    	</navigation-rule>
    	<navigation-rule>
    		<display-name>authentificationE</display-name>
    		<from-view-id>/authentificationE.jsp</from-view-id>
    		<navigation-case>
    			<from-outcome>succeE</from-outcome>
    			<to-view-id>/espaceE.jsp</to-view-id>
    		</navigation-case>
    	</navigation-rule>
    	<navigation-rule>
    		<display-name>authentificationE</display-name>
    		<from-view-id>/authentificationE.jsp</from-view-id>
    		<navigation-case>
    			<from-outcome>erreur</from-outcome>
    			<to-view-id>/erreur.jsp</to-view-id>
    		</navigation-case>
    	</navigation-rule>
    	<navigation-rule>
    		<display-name>authentificationC</display-name>
    		<from-view-id>/authentificationC.jsp</from-view-id>
    		<navigation-case>
    			<from-outcome>succeC</from-outcome>
    			<to-view-id>/espaceC.jsp</to-view-id>
    		</navigation-case>
    	</navigation-rule>
    	<navigation-rule>
    		<display-name>authentificationC</display-name>
    		<from-view-id>/authentificationC.jsp</from-view-id>
    		<navigation-case>
    		            <from-outcome>erreur</from-outcome>
    			        <to-view-id>/erreur.jsp</to-view-id>
    		</navigation-case>
    	</navigation-rule>
     
    </faces-config>


    S'il vous plait si vous pouvez m'aider dite moi ce que je doit faire , merci d'avance pour votre aide .

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    tout simplement
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    boolean userOk = session.createQuery ("from Candidat c where c.username=:user and c.pasword=:password").setString("user",login).setString("password",password).uniqueResult()!=null;

  3. #3
    Membre averti
    Inscrit en
    Octobre 2009
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 23
    Par défaut encore un oroblème avec al connexion à ma base de donneé
    Désolé pour la manière que j'ai écrit mon message sur le forum et merci Tchize pour votre réponse , mais j'ai encore un problème quand j'exécute la page d'authentification le voila :

    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
     
    5 déc. 2010 14:01:15 org.apache.catalina.core.AprLifecycleListener init
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre6\bin;.;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre6/bin/client;C:/Program Files/Java/jre6/bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\QuickTime\QTSystem\;C:\Sun\AppServer\bin
    5 déc. 2010 14:01:15 org.apache.tomcat.util.digester.SetPropertiesRule begin
    ATTENTION: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:MonProjetJsf' did not find a matching property.
    5 déc. 2010 14:01:15 org.apache.tomcat.util.digester.SetPropertiesRule begin
    ATTENTION: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:MyProjectJpaServlet' did not find a matching property.
    5 déc. 2010 14:01:15 org.apache.coyote.http11.Http11Protocol init
    INFO: Initialisation de Coyote HTTP/1.1 sur http-8080
    5 déc. 2010 14:01:15 org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 890 ms
    5 déc. 2010 14:01:15 org.apache.catalina.core.StandardService start
    INFO: Démarrage du service Catalina
    5 déc. 2010 14:01:15 org.apache.catalina.core.StandardEngine start
    INFO: Starting Servlet Engine: Apache Tomcat/6.0.29
    Test Param: null
    5 déc. 2010 14:01:17 com.sun.faces.config.ConfigureListener contextInitialized
    INFO: Initializing Mojarra (1.2_12-b01-FCS) for context '/MonProjetJsf'
    5 déc. 2010 14:01:17 org.apache.coyote.http11.Http11Protocol start
    INFO: Démarrage de Coyote HTTP/1.1 sur http-8080
    5 déc. 2010 14:01:18 org.apache.jk.common.ChannelSocket init
    INFO: JK: ajp13 listening on /0.0.0.0:8009
    5 déc. 2010 14:01:18 org.apache.jk.server.JkMain start
    INFO: Jk running ID=0 time=1/48  config=null
    5 déc. 2010 14:01:18 org.apache.catalina.startup.Catalina start
    INFO: Server startup in 2604 ms
    5 déc. 2010 14:01:19 org.apache.catalina.core.StandardWrapperValve invoke
    GRAVE: "Servlet.service()" pour la servlet Faces Servlet a généré une exception
    java.lang.ClassNotFoundException: net.sf.hibernate.HibernateException
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1645)
    	at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1491)
    	at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    	at java.lang.Class.getDeclaredConstructors0(Native Method)
    	at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    	at java.lang.Class.getConstructor0(Unknown Source)
    	at java.lang.Class.getConstructor(Unknown Source)
    	at com.sun.faces.mgbean.BeanBuilder.loadBeanClass(BeanBuilder.java:426)
    	at com.sun.faces.mgbean.BeanBuilder.bake(BeanBuilder.java:378)
    	at com.sun.faces.mgbean.ManagedBeanBuilder.bake(ManagedBeanBuilder.java:90)
    	at com.sun.faces.mgbean.BeanManager.preProcessBean(BeanManager.java:311)
    	at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:201)
    	at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:86)
    	at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54)
    	at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:72)
    	at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:68)
    	at org.apache.el.parser.AstValue.getValue(AstValue.java:112)
    	at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
    	at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:101)
    	at javax.faces.component.UIOutput.getValue(UIOutput.java:184)
    	at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:201)
    	at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:284)
    	at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:154)
    	at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:861)
    	at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:242)
    	at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:180)
    	at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:127)
    	at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:930)
    	at javax.faces.render.Renderer.encodeChildren(Renderer.java:148)
    	at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:930)
    	at javax.faces.component.UIComponent.encodeAll(UIComponent.java:933)
    	at com.sun.faces.application.ViewHandlerImpl.doRenderView(ViewHandlerImpl.java:266)
    	at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:197)
    	at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:110)
    	at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
    	at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    	at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    	at java.lang.Thread.run(Unknown Source)

    Voila aussi le code de mon javaBenas :
    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
     
    package com.thescreencast.web;
     
    import java.util.Iterator;
    import java.util.List;
     
    import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Session;
     
    public class PerssonneBean {
    	private String login;
    	/**
             * 
             */
    	private String password;
    	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;
    	}
       public String add() throws HibernateException{
    		 Session session = null;
    		try {
    			session = HibernateUtil.currentSession();
    		} catch (HibernateException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		boolean userOk = session.createQuery ("from candidat c where c.login=:login and c.password=:password").setString("login",login).setString("password",password).uniqueResult()!=null;
     
     
    		if ( userOk )
    		return "succeC";
    		return "erreur";
       }
     
     
    }

    J'utilise hibernate comme persistance , donc quand je l'ai tester dans une classe java pour tester la connexion avec la base de donnée j'ai pas eu aucun problème (affichage ,insertion,..) mais mon mon problème est comment utiliser les session pour afficher ou insérer les donnée en utilisant jsf et les javabeans .
    Je vous remercie d'avance pour votre aide .

  4. #4
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482
    Par défaut
    les packages net.sf.hibernate sont dépréciés depuis quelques années. Utilisez les packages org.hibernate. Enfin, votre exception viens du fait que vous n'avez pas inclu les libirairies hibernante dans vote WEB-INF/lib

  5. #5
    Membre averti
    Inscrit en
    Octobre 2009
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 23
    Par défaut
    merci Tchize pour votre réponse , vous avez raison l'erreur été à cause de manque de librairie dans la dossier lib de web-inf , mon problème est résolu et voila comment j'ai fait dans le javabeans :

    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
     
    package com.thescreencast.web;
     
    import java.util.Iterator;
    import java.util.List;
     
    import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Query;
    import net.sf.hibernate.Session;
     
    public class PerssonneBean {
    	private String login;
    	/**
             * 
             */
    	private String password;
    	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;
    	}
     
       public String add() throws HibernateException{
    		 Session session = null;
    		try {
    			session = HibernateUtil.currentSession();
    		} catch (HibernateException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    	   Query list = session.createQuery("from Candidat where login=:user and password=:password").setString("user", login).setString("password",password);
    	   Iterator it = list.iterate();
    		/* Iterator it = list.iterator();
    		 while(it.hasNext())
    		 {
    		   Candidat c = (Candidat)it.next();
    		   System.out.println(c.getIdCandidat()+"-->" +c.getNom());
    		 }*/
    	  // if(login.equals("ff")& password.equals("ff"))
    		//if(it.hasNext())   
     
    	  // return "erreur";
    	   if(it.hasNext())
    		   return "succeC";
     
    	   else return "erreur";
       }
     
     
    }
    Je vous remercie encore , mais j'ai encore quelque problème en ce qui concerne la relation entre les pages jsf et mon code de javabeans , pour faire par exemple l'affichage des données de ma base de donnée dans ma page jsf ......
    Je vous remercie d'avance

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 22/01/2013, 00h07
  2. [hibernate][mysql] erreurs
    Par nikalkal dans le forum Hibernate
    Réponses: 1
    Dernier message: 25/04/2006, 17h40
  3. [Hibernate][MySql] probleme de connexion ...
    Par Gromitou dans le forum Hibernate
    Réponses: 6
    Dernier message: 28/02/2006, 15h23
  4. [Hibernate][mysql] SocketPermission
    Par vlord dans le forum Hibernate
    Réponses: 1
    Dernier message: 07/01/2006, 12h25
  5. Hibernate + mySql + Eclipse
    Par Goupil dans le forum Eclipse Java
    Réponses: 2
    Dernier message: 14/02/2005, 14h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo