0

I am actually following a tutorial to discover Spring framework. I am a beginer with Web technology.

I actually get a problem in my file dispatcher-servlet.xml. I believe that my <context:component-scan base-package="com.tuto.mehdi" /> is not working cause I have an Error 404 when I launch my project :

My error

My architecture

My console error :

AVERTISSEMENT: No mapping found for HTTP request with URI [/tutoriel-web-spring/] in DispatcherServlet with name 'servlet-dispatcher' avr. 06, 2016 2:26:34 PM org.springframework.web.servlet.PageNotFound noHandlerFound

AVERTISSEMENT: No mapping found for HTTP request with URI [/tutoriel-web-spring/bonjour] in DispatcherServlet with name 'servlet-dispatcher'


That's my code. I believe that I did good but I am not able to identify the problem. Hope you could help me!

WEB.XML

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- variable de contexte définie dans le WEB-INF-->
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <!-- Le listener « ContextLoaderListener » charge la configuration Spring 
    à partir de la variable de contexte « contextConfigLocation ». -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Declaration de la servlet de Spring et de son mapping -->
    <servlet>
        <servlet-name>servlet-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>servlet-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

bonjourController.java (java class not create as servlet)

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
/*
 * indique que le contrôleur traite les requêtes GET dont l'URI est « /bonjour »
 * */
@RequestMapping("/bonjour")
public class BonjourController {

    @RequestMapping(method = RequestMethod.GET)
    public String afficherBonjour(final ModelMap pModel) {
        pModel.addAttribute("personne", "Regis");
        return "bonjour";
    }
}

bonjour.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!-- déclration de la "taglib" Spring -->
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!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><spring:message code="titre.bonjour"/> : ${ personne }</title>
</head>
<body>
    <spring:message code="libelle.bonjour.lemonde" arguments="${personne}"/>
</body>
</html>

dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 <!-- Déclaration du bean "messageSource" de classe ReloadableResourceBundleMessagesource  -->
 <!-- permettra de charger les messages internationalisées dans des fichiers messages_xx.properties (voir classpath) -->
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="ISO-8859-1" />
    </bean>
    
    <!-- active la configuration par annotations -->
    <context:component-scan base-package="com.tuto.mehdi" />
    <!-- la déclaration du bean InternalResourceViewResolver indique où chercher la ressource -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/vues/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    
</beans>
mehdmehd
  • 31
  • 1
  • 6
  • 1
    Could you add the contents of dispatcher-servlet.xml? – uncaught_exception Apr 06 '16 at 13:42
  • 1
    For starters stop loading the same configuration twice. You are loading all of your beans twice, once by the `ContextLoaderListener` and next by the `DispatcherServlet`. Either split your configuration or ditch the `ContextLoaderListener`. Also use a more recent version of the `web.xml` with 2.3 (which is from a decade ago or so) you don't have working EL. See http://www.mkyong.com/web-development/the-web-xml-deployment-descriptor-examples/ (I suggest at least the 3.0 version). – M. Deinum Apr 06 '16 at 13:52
  • Where are you mapping `/tutoriel-web-spring`? If you want to use that, I think you only need to change your `@RequestMapping` to `@RequestMapping("/tutoriel-web-spring/bonjour")`. Or change/add another `servlet-mapping` – Christopher Schneider Apr 06 '16 at 14:02
  • God! sorry I forgot the most important code part! Thank you M.Denium for your reply. Didn't understand that is the same thing. Believed I had to have the contextLoaderListener to map with DispatcherServlet. Then I could delete all from (line 8) to (line 16)? About the versioning. you'r talking about 3.0, but in my tutorial, the autor already use EL. web.xml 3.0 is not about redirection with @blabla? Thank you again, – mehdmehd Apr 06 '16 at 14:04
  • Christopher, I just want to call my .jsp when my url contain /bonjour. That's not the way to do? – mehdmehd Apr 06 '16 at 14:06
  • The `servlet-mapping` says to call the dispatcher if the url contains `/`. It does that, and then tries to find a request mapping with everything after the `/`. Your only mapping is `/bonjour`, but it is trying to find `/tutoriel-web-spring/bonjour` – Christopher Schneider Apr 06 '16 at 15:22
  • I tryed to add this --- BonjourController com.tuto.mehdi.controller.BonjourController BonjourController /bonjour --- But still not working. Anyway, the first mapping had to work alone... – mehdmehd Apr 06 '16 at 19:32

0 Answers0