Spring:Configuration

1.Ajouter les dépendances dans le fichier pom.xml

Les dépendances c'est un ensembles des bibliothèques jar qui contient les modules de Spring

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">

<display-name>test Web Application</display-name>

<!-- applicationcontext.xml :Fichier à Créer : sera utilisé par Spring afin de créer et configurer
des beans (objet) en runtime-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationcontext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--DispatcherServlet: est la servlet qui controlle l'application:
1.faire le mapping entre les requete http et les methode à éxécuter dans les controlleures
2.envoyer la réponse au client
Spring-servlet.xml:Fichier à Créer : est le ficher de configuration DispatcherServlet
-->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/Spring-servlet.xml</param-value>
</init-param>
</servlet>
<!-- le dispatcher :créer en haut va controller toutes les entrées et les sortie
du projet à partir de la racine / -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

3.applicationcontext.xml

<!--?xml version="1.0" encoding="UTF-8"?-->
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- sera utlisé par spring pour créer des bean -->
</beans:beans>

4.Spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- les annotation seront utilisées pour créer
-des controlleurs @Controller
-des repositories @Repository
-des services @Services
-....
-->
<annotation-driven />
<!--l'emplacement du package contenant les classe de type controller-->
<context:component-scan base-package="com.ecomerce.controllers" />
<!--l'emplacemenet des resources public :javascript , css...-->
<resources mapping="/resources/**" location="/resources/" />
<!--InternalResourceViewResolver:une servlet qui chercher les vues demandés par
le Dispacher
/views/:le dossier des vues
.jsp: l'extension des vues
si on chercher la vue hello=> hello.jsp
-->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>









Cours et TPs