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 et mes premiers beans


Sujet :

JSF Java

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    114
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 114
    Par défaut jsf et mes premiers beans
    Bonjour,

    Débutant en JEE et JSF, je réalise mes premiers beans.
    Je suis sous eclipse helios, jboss 6 et jsf 2.0.
    J'ai 2 projets sous eclipse :
    - un projet JSF (qui contient mes xhtml et mon managed bean)
    - un projet EJB/JPA (qui contient un ejb entité, un ejb stateless et son interface)

    **** Mon projet EJB/JPA

    Mon entity :
    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
     
    package com.galhauban.patrimoine.batiment;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.SequenceGenerator;
    import javax.persistence.Table;
     
    @Entity
    @Table(name="BATIMENT")
    @SequenceGenerator(name = "seqbati", sequenceName = "SEQ_BATIMENT")
    public class BatimentEntity {
    	private Integer bat_id;
    	private String bat_name;
     
    	public BatimentEntity() {}
     
    	/**
             * @return the bat_id
             */
    	@Id
    	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqbati")
    	public Integer getBat_id() {
    		return bat_id;
    	}
     
    	/**
             * @param bat_id the bat_id to set
             */
    	public void setBat_id(Integer bat_id) {
    		this.bat_id = bat_id;
    	}
     
    	/**
             * @return the bat_name
             */
    	public String getBat_name() {
    		return bat_name;
    	}
     
    	/**
             * @param bat_name the bat_name to set
             */
    	public void setBat_name(String bat_name) {
    		this.bat_name = bat_name;
    	};
    }
    Mon stateless
    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
     
    package com.galhauban.patrimoine.batiment;
     
    import javax.ejb.Local;
    import javax.ejb.Stateless;
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
     
    @Stateless(name="batimentBean")
    @Local(BatimentLocal.class)
    @PersistenceContext (unitName = "EJB-Patrimoine")
    public class BatimentBean implements BatimentLocal {
    	private EntityManager em;
    	private BatimentEntity batiment;
     
    	public BatimentEntity getBatiment() {
    		return batiment;
    	}
     
    	public void setBatiment(BatimentEntity batiment) {
    		this.batiment = batiment;
    	}
     
    	public BatimentEntity searchBatiment(int nb){
    		//try {
    		batiment = em.find(BatimentEntity.class, nb);
    		em.persist(batiment);
    		return batiment;
    		/*}
    		catch(Exception ex){
    			System.out.println("Exception stateless bean (BatimentBean) : "+ ex.getMessage());
    			return null;
    		}*/
    	}
    }
    Mon interface
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    package com.galhauban.patrimoine.batiment;
     
    import javax.ejb.Local;
     
    @Local
    public interface BatimentLocal {
    	BatimentEntity searchBatiment(int nb);
     
    }
    Mon persitence.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    	<persistence-unit name="EJB-Patrimoine">
    		<class>com.galhauban.patrimoine.batiment.BatimentEntity</class>
    	</persistence-unit>
    </persistence>

    **** Mon projet JSF

    Mon managedbean
    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
     
    package com.galhauban.patrimoine.batiment;
    import java.io.Serializable;
    import javax.ejb.EJB;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
     
    @ManagedBean(name="batimentController")
    @SessionScoped
    public class BatimentController implements Serializable {
     
    	private static final long serialVersionUID = 1L;
    	private int identifiant;
     
    	private BatimentLocal building;
     
    	private BatimentEntity batiment;
     
    	public BatimentController(){};
     
    	public BatimentLocal getBuilding() {
    		return building;
    	}
     
    	public void setBuilding(BatimentLocal building) {
    		this.building = building;
    	}
     
    	public BatimentEntity getBatiment() {
    		return batiment;
    	}
     
     
    	public void setBatiment(BatimentEntity batiment) {
    		this.batiment = batiment;
    	}
     
    	public String doSearch() {
    		batiment = building.searchBatiment(identifiant);
    		return "building.found";
    	}
     
    	public int getIdentifiant() {
    		return identifiant;
    	}
     
    	public void setIdentifiant(int identifiant) {
    		this.identifiant = identifiant;
    	}
    }
    Mon formulaire xhtml
    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
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:ui="http://java.sun.com/jsf/facelets"> 
     
    <h:head></h:head> 
    <body> 
    <ui:composition template="/templates/modele.xhtml">
     
    	<ui:define name="entete">REFERENTIEL BATIMENT</ui:define>
    	<ui:define name="corps">
    			<h:form>
    					<h:outputText value="ceci est un test" />
    					<h:inputText value="#{batimentController.identifiant}" />
    					<h:commandButton action="#{batimentController.doSearch}" value="Search" style=" width : 104px;"/>
    			</h:form>
    	</ui:define>
    </ui:composition>
    </body> 
    </html>
    Mon xhtml qui doit afficher le résultat
    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
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:a4j="http://richfaces.org/a4j"
          xmlns:rich="http://richfaces.org/rich"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:ui="http://java.sun.com/jsf/facelets"> 
     
    <h:head></h:head> 
    <body>
    <ui:composition template="/templates/modele.xhtml">
     
    	<ui:define name="entete">BATIMENT detail</ui:define>
    	<ui:define name="corps">
     
    		<rich:panel style="border:0;width:60%;text-align:center">
    			<h:outputText value="#{batimentController.batiment.bat_name}" />
    		</rich:panel>
    	</ui:define>
    </ui:composition>
    </body> 
    </html>

    Mon faces-config
    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
     
    <?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_2_0.xsd"
    	version="2.0">
     
    	<navigation-rule>
    		<from-view-id>/pages/inputname.xhtml</from-view-id>
    		<navigation-case>
    			<from-outcome>greeting</from-outcome>
    			<to-view-id>/pages/greeting.xhtml</to-view-id>
    		</navigation-case>
    	</navigation-rule>
     
    	<navigation-rule>
    		<from-view-id>/pages/test.xhtml</from-view-id>
    		<navigation-case>
    			<from-outcome>building.found</from-outcome>
    			<to-view-id>/pages/batiment.xhtml</to-view-id>
    		</navigation-case>
    	</navigation-rule>
     
        <application>
            <resource-bundle>
                <base-name>resources</base-name>
                <var>msgs</var>
            </resource-bundle>
        </application>
    </faces-config>
    Mon 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
     
    <?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" version="3.0">
      <display-name>JSF-Patrimoine</display-name>
      <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
        <url-pattern>/faces/*</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
    java.naming.provider.url=jnp://127.0.0.1:1099
    java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
    Le but du jeu est donc de fournir un identifiant via le formulaire pour obtenir le nom d'un batiment.

    Le problème c'est que lorsque je déploie sous jboss via eclipse j'ai ce message d'erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    14:41:26,648 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/JSF-Patrimoine]] Exception lors de l'envoi de l'évènement contexte initialisé (context initialized) à l'instance de classe d'écoute (listener) org.jboss.web.jsf.integration.config.JBossMojarra20ConfigureListener: java.lang.NoClassDefFoundError: Lcom/galhauban/patrimoine/batiment/BatimentLocal;

    Je suis un peu paumé et je ne sais pas pourquoi il attend la classe :
    "Lcom/galhauban/patrimoine/batiment/BatimentLocal" alors que c'est
    "com/galhauban/patrimoine/batiment/BatimentLocal" il n'y a pas de L devant?

    Merci d'avance pour vos réponses
    Couse1

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    114
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 114
    Par défaut
    Je me rends compte que cela se joue au niveau du managedbean BatimentController.

    Lorsque je commente toutes les références au projet EJB je n'ai plus de messages d'erreur. Par contre, rien que le fait de déclarer :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    private BatimentLocal building;
    ou
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    private BatimentEntity batiment;
    J'ai le message d'erreur. Etant donné que le message fait référence à la notion de context et que mes connaissances sont limités en J2EE. Y aurait - il une notion de context à déclarer qq part?

    Merci
    Couse1

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    114
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 114
    Par défaut
    Dans le stack trace j'ai trouvé ce message :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Caused by: java.lang.ClassNotFoundException: com.galhauban.patrimoine.batiment.BatimentEntity from BaseClassLoader@1345af1{vfs:///C:/Users/R20190/Desktop/Applications/Patrimoine/Developpement/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_6.0_Runtime_Server1303994040191/deploy/JSF-Patrimoine.war}
    Et effectivement dans le dossier en question il n'y a pas la présence de l'ensemble du projet EJB/JPA, il n'y a que mon projet WEB. Cela semble être le dossier de runtime pour eclipse, y aurait-il une manip à réaliser pour indiquer à éclipse d'y inclure le projet EJB (et donc les jars en question)?

    Merci par avance
    Couse1

Discussions similaires

  1. Réponses: 4
    Dernier message: 10/09/2006, 10h49
  2. [JSF] Communication entre les beans
    Par Arnaud Giuliani dans le forum JSF
    Réponses: 1
    Dernier message: 01/06/2006, 23h07
  3. [JSF] modification d'un bean
    Par Shivan dans le forum JSF
    Réponses: 1
    Dernier message: 21/03/2006, 15h02
  4. Réponses: 6
    Dernier message: 02/03/2006, 07h39
  5. Mes premiers débuts avec Dev-C++ : ca bloque !
    Par fab56 dans le forum Dev-C++
    Réponses: 6
    Dernier message: 20/02/2005, 16h24

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