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

Spring Java Discussion :

Premier exemple Spring + hibernate + JSF _ Problème d"instanciation de service NullPointerException


Sujet :

Spring Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Candidat au Club
    Femme Profil pro
    Développeur Java
    Inscrit en
    Août 2014
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2014
    Messages : 2
    Par défaut Premier exemple Spring + hibernate + JSF _ Problème d"instanciation de service NullPointerException
    Bonjour Tout le monde, ça fait une semaine que je suis bloquée dans une étape qui me sembbe banale, c'est pour cette raison que je poste ici en demandant votre aide.
    en fait je crée mon premier projet web hibernate + spring + jsf en utilisant les annotations et là c'est un tout petit exemple avec une seule entité "Process" et un service qui est sensé afficher toutes mes enregistrements dans la base (après avoir vu une erreur qui s'affiche à mon écran en indiquant que mon service est null, j'ai fait un retour null dans le dao juste pour cadrer le problème et avancer petit à petit)


    L'erreur qui s'affiche est :
    Etat HTTP 500 -

    type Rapport d'exception

    message

    description Le serveur a rencontré une erreur interne () qui l'a empêché de satisfaire la requête.

    exception

    javax.servlet.ServletException: java.lang.NullPointerException
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
    org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
    org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
    org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
    org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)

    cause mère

    javax.faces.el.EvaluationException: java.lang.NullPointerException
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329)
    org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304)
    org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261)
    org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:474)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
    org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
    org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
    org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)

    cause mère

    java.lang.NullPointerException
    fr.arbo.beans.ProcessBean.afficher(ProcessBean.java:55)

    Voici mon code
    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
    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
    <?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_2_5.xsd"
        id="WebApp_ID" version="2.5">
     
        <display-name>arbo</display-name>
     
     
     
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
     
     
     
     
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <listener>
            <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
        </listener>
     
     
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/applicationContext.xml
            </param-value>
        </context-param>
     
    <context-param>  
         <param-name>javax.faces.DISABLE_FACELET_JSF_VIEWHANDLER</param-name>  
         <param-value>true</param-value>  
    </context-param> 
     
     
      <servlet>
            <servlet-name>Faces Servlet</servlet-name>
            <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
     
     
        <context-param>
            <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
            <param-value>client</param-value>
        </context-param>
     
     
    <servlet-mapping>
       <servlet-name>Faces Servlet</servlet-name>
       <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
     
     
    <servlet-mapping>
       <servlet-name>Faces Servlet</servlet-name>
       <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
     
      
    <servlet-mapping>
       <servlet-name>Faces Servlet</servlet-name>
       <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
      
        <filter>
        <filter-name>RichFaces Filter</filter-name>
    <filter-class>org.ajax4jsf.Filter</filter-class>    
        </filter>
        
        
        <filter-mapping>
        <filter-name>RichFaces Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        </filter-mapping>
        
        
    </web-app>
    faces-config.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
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <?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">
     
     
     
        <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
        </application>
     
     
        <application>   
          <view-handler>org.ajax4jsf.application.AjaxViewHandler</view-handler>
         </application>
     
     
        <application>
        <locale-config>
          <default-locale>fr</default-locale>
        </locale-config>
      </application>
     
     
     
        <managed-bean>
            <managed-bean-name>processBean</managed-bean-name>
            <managed-bean-class>fr.arbo.beans.ProcessBean</managed-bean-class>
            <managed-bean-scope>request</managed-bean-scope>
        </managed-bean>
     
     
     
    <navigation-rule>
    <from-view-id>/index.jsp</from-view-id>
    </navigation-rule>
     
      </faces-config>

    applicationContext.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
    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
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:task="http://www.springframework.org/schema/task"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                              http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
                              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                              http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
     
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:mvc="http://www.springframework.org/schema/mvc">
     
        <!-- config file -->
        <bean id="placeholderConfig"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="WEB-INF/conf/conf.properties" />
        </bean>
     
     
        <!-- data source access -->
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName">
                <value>${jdbc.driverClassName}</value>
            </property>
            <property name="url">
                <value>${jdbc.url}</value>
            </property>
            <property name="username">
                <value>${jdbc.username}</value>
            </property>
            <property name="password">
                <value>${jdbc.password}</value>
            </property>
        </bean>
     
     
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                    <prop key="hibernate.show_sql">false</prop>
                    <prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
                </props>
            </property>
     
        </bean>
     
      <!-- Hibernate transaction manager-->
            <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory">
     
            </property>
            </bean>
     
            <bean id="hibernateInterceptor"
                class="org.springframework.orm.hibernate3.HibernateInterceptor">
            <property name="sessionFactory">
              <ref bean="sessionFactory"/>
            </property>
          </bean>      
     
        <bean id="processDao" class="fr.arbo.dao.hibernate.ProcessDaoImpl">
        </bean>
     
     
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config/>
    <context:component-scan base-package="fr.arbo"/>
     
     
    </beans>
    ProcessDao.java

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    package fr.arbo.dao;
     
    import java.util.Date;
    import java.util.List;
    import fr.arbo.entity.Process;
     
    import fr.arbo.entity.Process;
     
    public interface ProcessDao{
        public List<Process> findAll();
    }

    ProcessDaoImpl.java
    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
     
    package fr.arbo.dao.hibernate;
     
    import java.util.List;
     
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.orm.hibernate3.HibernateTemplate;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.stereotype.Repository;
    import org.springframework.transaction.annotation.Transactional;
     
     
    import fr.arbo.dao.ProcessDao;
    import fr.arbo.entity.Process;
     
     
    public class ProcessDaoImpl implements ProcessDao {
     
     
        @Override
        public List<Process> findAll() {
            return null; 
        }
     
     
     
    }
    ProcessService.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    package fr.arbo.service;
     
    import java.util.List;
     
    import fr.arbo.entity.Process;
     
    public interface ProcessService {
        public List<Process> findAll();
    }

    ProcessServiceImpl.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    package fr.arbo.service;
     
    import java.util.List;
     
    import fr.arbo.entity.Process;
     
    public interface ProcessService {
        public List<Process> findAll();
    }

    et finalement ProcessBean.java
    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
    package fr.arbo.beans;
    
    import java.io.Serializable;
    import fr.arbo.entity.*;
    import fr.arbo.entity.Process;
    
    import java.util.List;
    import java.util.ArrayList;
    
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    
    import org.hibernate.mapping.Array;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
    
    
    import fr.arbo.service.ProcessService;
    
    
    @ManagedBean(name="processBean")
    @Scope("session")
    public class ProcessBean implements Serializable {
        
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        @Autowired
        private transient ProcessService ps;
        
        List<Process> maListe =  new ArrayList<Process>(); 
        
        @PostConstruct
        public void init() {
            System.out.println("init");
            maListe = new ArrayList<Process>(); 
        }
    
        
        
        public ProcessBean(){
            System.out.println("test");
        }
        
        
        
        
        public void afficher(){
        System.out.println(" =====>> methode afficher <<=====");
        
        maListe = ps.findAll(); 
    
        }
    
        public List<Process> getMaListe() {
            return maListe;
        }
    
        public void setMaListe(List<Process> maListe) {
            this.maListe = maListe;
        }
    
    
        public ProcessService getPs() {
            return ps;
        }
    
    
    
    
    
        public void setPs(ProcessService ps) {
            this.ps = ps;
        }
    
    
    
    
    
    
    }


    MA page 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
     
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%> 
    <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%> 
    <%@taglib uri="http://richfaces.org/rich" prefix="rich"%>
    <%@taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
    <html>
    <body>
    <p>Mon premier exemple</p>
    <f:view>
    <h:form>
    <h:outputText value="Appeler la methode afficher" />
    <h:commandLink value="Lien" action="#{processBean.afficher}" >
    </h:commandLink>
    </h:form>
    </f:view>
    </body> 
    </html>
    Merci d'avance

  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
    C'est con, tu a posté plein de trucs, sauf ProcessBean, qui est là où a lieu l'erreur....

  3. #3
    Candidat au Club
    Femme Profil pro
    Développeur Java
    Inscrit en
    Août 2014
    Messages
    2
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Algérie

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : Enseignement

    Informations forums :
    Inscription : Août 2014
    Messages : 2
    Par défaut
    Vous avez raison,mais c'est une faute du contenu, voila j'ai modifié le message

  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
    Ok,

    ton problème, c'est que tu as définis ProcessBean dans JSF (faces-config) et non pas dans spring (spring.xml, @Component, ou similaire). Du coup spring n'interviens pas dans son chargement et donc n'injecte rien dedans (@Autowired).

    De là, tu as deux possibilités selon moi

    1) garder le faces config, mais alors utilise un managed-property pour lui dire d'injecter le ps avec une valeur particulière. Personnellement, je n'aime pas c'est long et moche
    2) transférer ton ProcessBean dans le contexte de Spring, comme ce context est visible depuis jsf, ca ne changera rien aux pages JSF mais au moins ta config sera centralisée dans spring.

Discussions similaires

  1. Réponses: 1
    Dernier message: 06/08/2014, 09h23
  2. Intégration Spring Hibernate JSF avec Maven
    Par theboss0 dans le forum Frameworks Web
    Réponses: 8
    Dernier message: 11/03/2014, 17h21
  3. J2EE Spring Hibernate JSF
    Par infoLife dans le forum JSF
    Réponses: 4
    Dernier message: 23/01/2012, 09h43
  4. [Integration] [Spring/Hibernate/JSF] Session is closed!
    Par mr1azl dans le forum Spring
    Réponses: 4
    Dernier message: 15/03/2011, 08h39
  5. Spring Hibernate JSF JasperReports
    Par wajdopovitch dans le forum Spring
    Réponses: 4
    Dernier message: 04/07/2009, 13h45

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