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