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 Web Java Discussion :

[Spring MVC] erreur de controleur


Sujet :

Spring Web Java

  1. #1
    Membre averti
    Inscrit en
    Juillet 2003
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Juillet 2003
    Messages : 12
    Par défaut [Spring MVC] erreur de controleur
    bonjour,
    je débute sous spring et je me fais qques tutoriels pour me faire la main sur ce framework.
    Cependant je suis confronté à un souci lorsque je veux tester mon controleur.

    J'ai besoin d'une âme charitable pour m'aider à voir le problème qui devrait être simple mais désolé je débute...

    je souhaite faire un bouchon pour pouvoir tester ma classe en renseignant par défaut des valeur dans l'objet productManager.

    Mais mon objet productManager reste vide dans mon controleur me levant par la même une exception java.lang.NullPointerException

    Merci d'avance

    springapp-servlet.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
     
    <?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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
     
        <bean id="productManager" class="springapp.service.SimpleProductManager">
            <property name="products">
                <list>
                    <ref bean="product1"/>
                    <ref bean="product2"/>
                    <ref bean="product3"/>
                </list>
            </property>
        </bean>
        <bean id="product1" class="springapp.domain.Product">
            <property name="description" value="Lamp"/>
            <property name="price" value="5.75"/>
        </bean>
        <bean id="product2" class="springapp.domain.Product">
            <property name="description" value="Table"/>
            <property name="price" value="75.25"/>
        </bean>
        <bean id="product3" class="springapp.domain.Product">
            <property name="description" value="Chair"/>
            <property name="price" value="22.79"/>
        </bean>
     
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="messages"/>
        </bean>
     
        <bean name="/hello.htm" class="springapp.web.InventoryController"/>
     
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />
    </beans>
    ma classe simpleproductmanager:
    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 springapp.service;
     
    import java.util.List;
     
    import springapp.domain.Product;
     
    public class SimpleProductManager implements ProductManager {
     
        private List<Product> products;
     
        public List<Product> getProducts() {
            return products;
        }
     
        public void setProducts(List<Product> products) {
            this.products = products;
        }
     
        public void increasePrice(int percentage) {
            if (products != null) {
                for (Product product : products) {
                    double newPrice = product.getPrice().doubleValue() * 
                                        (100 + percentage)/100;
                    product.setPrice(newPrice);
                }
            }   
        }
     }
    mon controleur:
    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
     
    package springapp.web;
     
    import org.springframework.web.servlet.mvc.Controller;
    import org.springframework.web.servlet.ModelAndView;
     
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import java.io.IOException;
    import java.util.Map;
    import java.util.HashMap;
     
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
     
    import springapp.service.ProductManager;
     
    public class InventoryController implements Controller {
     
        protected final Log logger = LogFactory.getLog(getClass());
     
        private ProductManager productManager;
     
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
     
            String now = (new java.util.Date()).toString();
            logger.info("returning hello view with " + now);
     
            Map<String, Object> myModel = new HashMap<String, Object>();
            myModel.put("now", now);
            myModel.put("products", this.productManager.getProducts());
     
            return new ModelAndView("hello", "model", myModel);
        }
     
     
        public void setProductManager(ProductManager productManager) {
            this.productManager = productManager;
        }
     
    }

  2. #2
    Membre averti
    Inscrit en
    Juillet 2003
    Messages
    12
    Détails du profil
    Informations forums :
    Inscription : Juillet 2003
    Messages : 12
    Par défaut
    arghh...
    j'ai trouvé alors que je me suis pris la tête plus de 3 heures!

    il manquait la référence au productManager dans springapp-servlet.xml à ce niveau

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    <bean name="/hello.htm" class="springapp.web.InventoryController">
            <property name="productManager" ref="productManager"/>
        </bean>

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

Discussions similaires

  1. Réponses: 4
    Dernier message: 23/10/2007, 09h14
  2. [SPRING MVC]Erreur sur un Integer dans un Validator
    Par manuzinho dans le forum Spring Web
    Réponses: 2
    Dernier message: 31/08/2007, 15h58
  3. [Spring MVC] erreur dans popup !
    Par Tail dans le forum Spring Web
    Réponses: 1
    Dernier message: 02/07/2007, 14h31
  4. [ Spring MVC ] [ JBoss ] Erreur à l'instanciation..
    Par legzo dans le forum Wildfly/JBoss
    Réponses: 1
    Dernier message: 30/05/2006, 12h37
  5. [Spring MVC][Validator] Ne stop pas si il y a une erreurs
    Par Hikage dans le forum Spring Web
    Réponses: 3
    Dernier message: 15/02/2006, 09h43

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