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 & jpa


Sujet :

JSF Java

  1. #1
    Nouveau candidat au Club
    Inscrit en
    Novembre 2008
    Messages
    1
    Détails du profil
    Informations forums :
    Inscription : Novembre 2008
    Messages : 1
    Par défaut jsf & jpa
    Bonsoir a tous,
    bon j'utilise le jsf & jpa
    et voila les codes:une classe pour etudiant c'est l'image de la table etudiant

    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
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
     
    *******************etudiant*************
    @Entity
    @Table(name = "etudiant")
    @NamedQueries({@NamedQuery(name = "Etudiant.findByMatrEt", query = "SELECT e FROM Etudiant e WHERE e.matrEt = :matrEt"), @NamedQuery(name = "Etudiant.findByNomEt", query = "SELECT e FROM Etudiant e WHERE e.nomEt = :nomEt"), @NamedQuery(name = "Etudiant.findByPrenomEt", query = "SELECT e FROM Etudiant e WHERE e.prenomEt = :prenomEt"), @NamedQuery(name = "Etudiant.findByDateEt", query = "SELECT e FROM Etudiant e WHERE e.dateEt = :dateEt"), @NamedQuery(name = "Etudiant.findByLieuEt", query = "SELECT e FROM Etudiant e WHERE e.lieuEt = :lieuEt"), @NamedQuery(name = "Etudiant.findByAdressEt", query = "SELECT e FROM Etudiant e WHERE e.adressEt = :adressEt"), @NamedQuery(name = "Etudiant.findByAnneeEt", query = "SELECT e FROM Etudiant e WHERE e.anneeEt = :anneeEt"), @NamedQuery(name = "Etudiant.findBySexeEt", query = "SELECT e FROM Etudiant e WHERE e.sexeEt = :sexeEt"), @NamedQuery(name = "Etudiant.findByEmailEt", query = "SELECT e FROM Etudiant e WHERE e.emailEt = :emailEt"), @NamedQuery(name = "Etudiant.findByMpassEt", query = "SELECT e FROM Etudiant e WHERE e.mpassEt = :mpassEt")})
    public class Etudiant implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "MATR_ET", nullable = false)
    private Long matrEt;
    @Column(name = "NOM_ET", nullable = false)
    private String nomEt;
    @Column(name = "PRENOM_ET", nullable = false)
    private String prenomEt;
    @Column(name = "DATE_ET")
    @Temporal(TemporalType.DATE)
    private Date dateEt;
    @Column(name = "LIEU_ET", nullable = false)
    private String lieuEt;
    @Column(name = "ADRESS_ET", nullable = false)
    private String adressEt;
    @Column(name = "ANNEE_ET", nullable = false)
    private String anneeEt;
    @Column(name = "SEXE_ET", nullable = false)
    private char sexeEt;
    @Column(name = "EMAIL_ET")
    private String emailEt;
    @Column(name = "MPASS_ET", nullable = false)
    private String mpassEt;
     
    public Etudiant() {
    }
     
    public Etudiant(Long matrEt) {
    this.matrEt = matrEt;
    }
     
    public Etudiant(Long matrEt, String nomEt, String prenomEt, String lieuEt, String adressEt, String anneeEt, char sexeEt, String mpassEt) {
    this.matrEt = matrEt;
    this.nomEt = nomEt;
    this.prenomEt = prenomEt;
    this.lieuEt = lieuEt;
    this.adressEt = adressEt;
    this.anneeEt = anneeEt;
    this.sexeEt = sexeEt;
    this.mpassEt = mpassEt;
    }
     
    public Long getMatrEt() {
    return matrEt;
    }
     
    public void setMatrEt(Long matrEt) {
    this.matrEt = matrEt;
    }
     
    public String getNomEt() {
    return nomEt;
    }
     
    public void setNomEt(String nomEt) {
    this.nomEt = nomEt;
    }
     
    public String getPrenomEt() {
    return prenomEt;
    }
     
    public void setPrenomEt(String prenomEt) {
    this.prenomEt = prenomEt;
    }
     
    public Date getDateEt() {
    return dateEt;
    }
     
    public void setDateEt(Date dateEt) {
    this.dateEt = dateEt;
    }
     
    public String getLieuEt() {
    return lieuEt;
    }
     
    public void setLieuEt(String lieuEt) {
    this.lieuEt = lieuEt;
    }
     
    public String getAdressEt() {
    return adressEt;
    }
     
    public void setAdressEt(String adressEt) {
    this.adressEt = adressEt;
    }
     
    public String getAnneeEt() {
    return anneeEt;
    }
     
    public void setAnneeEt(String anneeEt) {
    this.anneeEt = anneeEt;
    }
     
    public char getSexeEt() {
    return sexeEt;
    }
     
    public void setSexeEt(char sexeEt) {
    this.sexeEt = sexeEt;
    }
     
    public String getEmailEt() {
    return emailEt;
    }
     
    public void setEmailEt(String emailEt) {
    this.emailEt = emailEt;
    }
     
    public String getMpassEt() {
    return mpassEt;
    }
     
    public void setMpassEt(String mpassEt) {
    this.mpassEt = mpassEt;
    }
     
    @Override
    public int hashCode() {
    int hash = 0;
    hash += (matrEt != null ? matrEt.hashCode() : 0);
    return hash;
    }
     
    @Override
    public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Etudiant)) {
    return false;
    }
    Etudiant other = (Etudiant) object;
    if ((this.matrEt == null && other.matrEt != null) || (this.matrEt != null && !this.matrEt.equals(other.matrEt))) {
    return false;
    }
    return true;
    }
     
    @Override
    public String toString() {
    // return "entites.Etudiant[matrEt=" + matrEt + "]";
    return "" + this.matrEt;
    }
     
    }
    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
     
    **************classe UserManager*******
     
    public class UserManager {
     
    public static final String ETUDIANT_SESSION_KEY = "etudiant";
     
    @PersistenceContext 
    private EntityManager em;
     
    @Resource 
    private UserTransaction utx;
     
    private Long matrEt;
     
    public Long getMatrEt() {
    return matrEt;
    }
     
    public void setMatrEt(Long matrEt) {
    this.matrEt = matrEt;
    }
    private String mpassEt;
    private String nomEt;
    private String prenomEt;
     
     
     
    public String getMpassEt() {
    return mpassEt;
    }
     
    public void setMpassEt(String mpassEt) {
    this.mpassEt = mpassEt;
    }
     
    public String getNomEt() {
    return nomEt;
    }
     
    public void setNomEt(String nomEt) {
    this.nomEt = nomEt;
    }
     
    public String getPrenomEt() {
    return prenomEt;
    }
     
    public void setPrenomEt(String prenomEt) {
    this.prenomEt = prenomEt;
    }
     
    public String validateEtudiant() { 
    FacesContext context = FacesContext.getCurrentInstance();
    Etudiant etudiant = getEtudiant();
    if (etudiant != null) {
    if (!etudiant.getMpassEt().equals(mpassEt)) {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
    "Login Failed!",
    "The password specified is not correct.");
    context.addMessage(null, message);
    return null;
    }
     
    context.getExternalContext().getSessionMap().put(ETUDIANT_SESSION_KEY, etudiant);
    return "app-main";
    } else { 
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
    "Login Failed!",
    "Username '"
    + matrEt
    +
    "' does not exist.");
    context.addMessage(null, message);
    return null;
    }
    }
     
     
    public String logout() {
    HttpSession session = (HttpSession)
    FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    if (session != null) {
    session.invalidate();
    }
    return "login";
     
    }
     
    private Etudiant getEtudiant() {
    //throw new UnsupportedOperationException("Not yet implemented");
    try {
    Etudiant etudiant = (Etudiant)
    em.createNamedQuery("Etudiant.findByMatrEt").
    setParameter("matrEt", matrEt).getSingleResult();
    return etudiant; 
    } catch (NoResultException nre) {
    return null;
    }
    }
     
     
     
     
    }

    et pour le jsf (login.jsf):
    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
    <%@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 prefix="f" uri="http://java.sun.com/jsf/core"%>
    <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
     
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
     
    <h1>Login</h1>
     
    <f:view>
    <h:messages style="color: red"
    showDetail="true"/>
    <h:form id="login">
    <h:panelGrid columns="2" border="0">
    Username: <h:inputText id="username" 
    value="#{usermanager.matrEt}"/> 
    Password: <h:inputSecret id="password"
    value="#{usermanager.mpassEt}"/>
    </h:panelGrid>
    <h:commandButton id="submit" 
    type="submit"
    value="Login"
    action="#{usermanager.validateEtudiant}"/>
    <br>
    <h:commandLink id="create"
    value="Create New Account"
    action="create"/>
    </h:form>
     
    </f:view>
     
    </body>
    </html>
    mais le problem que il faut converti le matrEt de type long a string
    et voila le rapport d'erreur

    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
     
    type Exception report
     
    message
     
    descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
     
    exception 
     
    javax.servlet.ServletException: #{usermanager.validateEtudiant}: java.lang.IllegalArgumentException: You have attempted to set a value of type class java.lang.String for parameter matrEt with expected type of class java.lang.Long from query string SELECT e FROM Etudiant e WHERE e.matrEt = :matrEt.
    root cause 
     
    javax.faces.FacesException: #{usermanager.validateEtudiant}: java.lang.IllegalArgumentException: You have attempted to set a value of type class java.lang.String for parameter matrEt with expected type of class java.lang.Long from query string SELECT e FROM Etudiant e WHERE e.matrEt = :matrEt.
    root cause 
     
    javax.faces.el.EvaluationException: java.lang.IllegalArgumentException: You have attempted to set a value of type class java.lang.String for parameter matrEt with expected type of class java.lang.Long from query string SELECT e FROM Etudiant e WHERE e.matrEt = :matrEt.
    root cause 
     
    java.lang.IllegalArgumentException: You have attempted to set a value of type class java.lang.String for parameter matrEt with expected type of class java.lang.Long from query string SELECT e FROM Etudiant e WHERE e.matrEt = :matrEt.
    note The full stack traces of the exception and its root causes are available in the Sun Java System Application Server 9.1_02 logs.
    --------------------------------------------------------------------------------

    Sun Java System Application Server 9.1_02
    ******************
    svp aider moi.merci

  2. #2
    Membre Expert Avatar de nathieb
    Homme Profil pro
    DevOps
    Inscrit en
    Mai 2004
    Messages
    1 058
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : DevOps
    Secteur : Service public

    Informations forums :
    Inscription : Mai 2004
    Messages : 1 058
    Par défaut Problème de déclaration de Type
    Bonjour,

    Je pense que java est assez explicite tu essayes de passer un pramètre de type String dans un paramètre de type Long.

    Regardes bien ta déclaration de Etudiant.findByMatrEt et son type ???

    Après regarde
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
    private Etudiant getEtudiant() {
            //throw new UnsupportedOperationException("Not yet implemented");
           try {
                Etudiant etudiant = (Etudiant)
                em.createNamedQuery("Etudiant.findByMatrEt").
                        setParameter("matrEt", matrEt).getSingleResult();
                return etudiant; 
            } catch (NoResultException nre) {
                return null;
            }
    Ton paramètre est un String ???

    Question : Tu sembles plus habitué à coder en PHP , je me trompe ?
    olivier

Discussions similaires

  1. Après le choix de JSF: Adopter JPA ou Hibernate?
    Par revo1789 dans le forum Frameworks Web
    Réponses: 4
    Dernier message: 22/07/2010, 12h08
  2. Réponses: 8
    Dernier message: 02/06/2010, 10h28
  3. question projet jsf+ejb3+jpa
    Par MattA184575 dans le forum Eclipse Java
    Réponses: 0
    Dernier message: 20/01/2009, 22h28
  4. Code Source - JEE5 (JSF, EJB3, JPA)
    Par 17mounir dans le forum JSF
    Réponses: 4
    Dernier message: 17/04/2008, 14h33

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