<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://blog.netapsys.fr/index.php/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
  <title>Netapsys Blog - hibernate</title>
  <link>http://blog.netapsys.fr/index.php/</link>
  <description></description>
  <language>fr</language>
  <pubDate>Tue, 06 Jan 2009 10:51:28 +0100</pubDate>
  <copyright>Netapsys 2008</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Introduction à Spring MVC</title>
    <link>http://blog.netapsys.fr/index.php/post/2008/04/13/Introduction-A-Spring-MVC</link>
    <guid isPermaLink="false">urn:md5:049e27f066ec5763779ac5c92b235efd</guid>
    <pubDate>Sun, 13 Apr 2008 01:14:00 +0200</pubDate>
    <dc:creator>Michael Courcy</dc:creator>
        <category>Java</category>
        <category>hibernate</category><category>java</category><category>MVC</category><category>spring MVC</category><category>spring web flow</category>    
    <description>&lt;p&gt;Dans la guerre furieuse des frameworks MVC il y en a un qui se démarque par sa simplicité, sa facilité de prise en main et sa parfaite intégration à Spring&amp;nbsp;: Spring MVC.&lt;/p&gt;


&lt;p&gt;Certes Spring MVC n'est pas un framework événementiel comme pourraient l'être jsf, wicket, tapestry ou struts 2. Mais il possède de nombreux points d'extensions et utilisé avec Spring Web Flow il devient un framework très puissant.&lt;/p&gt;


&lt;p&gt;Je vous propose ici une  introduction des plus simples qui va consister à afficher une page Hello Spring MVC avec le framework Spring MVC.&lt;/p&gt;


&lt;p&gt;Puis nous enrichirons progressivement notre application exemple avec de nouveaux tutoriels pour aboutir à l'utilisation de Spring Web Flow avec la gestion d'une transaction longue Hibernate.&lt;/p&gt;    &lt;p&gt;Le projet est un projet Maven, tout ce qu'il y a de plus simple que vous pouvez télécharger &lt;a href=&quot;http://blog.netapsys.fr/public/code/spring-mvc-webapp.zip&quot; hreflang=&quot;fr&quot;&gt;ici&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Sinon je vous propose (et vous conseille) de le construire pas à pas avec moi&amp;nbsp;:&lt;/p&gt;


&lt;h2&gt;Etape 1&amp;nbsp;: création du projet&lt;/h2&gt;


&lt;p&gt;Créons un projet web avec maven&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;bash&quot;&gt;mvn archetype:create -&lt;span style=&quot;color: #007800;&quot;&gt;DarchetypeGroupId=&lt;/span&gt;org.apache.maven.archetypes -&lt;span style=&quot;color: #007800;&quot;&gt;DarchetypeArtifactId=&lt;/span&gt;maven-archetype-webapp -&lt;span style=&quot;color: #007800;&quot;&gt;DgroupId=&lt;/span&gt;com.netapsys.springmvc  -&lt;span style=&quot;color: #007800;&quot;&gt;DartifactId=&lt;/span&gt;spring-mvc-webapp&lt;/pre&gt;


&lt;p&gt;Pour pouvoir utiliser les annotations notre projet doit être un projet en java 1.5. Ouvrez le &lt;ins&gt;pom.xml&lt;/ins&gt; situé dans le répertoire que vous venez de créér &lt;ins&gt;spring-mvc-webapp&lt;/ins&gt; et ajoutez ces informations entre les balises build. Vous devriez obtenir ceci&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;xml&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;build&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;finalName&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;spring-mvc-webapp&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/finalName&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;plugins&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;plugin&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;maven-compiler-plugin&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;configuration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;source&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;1.5&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/source&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;target&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;1.5&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/target&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/configuration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/plugin&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;	
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;plugin&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;maven-eclipse-plugin&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;version&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;2.4&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/version&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;configuration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;					
        &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;downloadSources&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;true&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/downloadSources&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;downloadJavadocs&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;true&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/downloadJavadocs&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;					
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/configuration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/plugin&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;		
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/plugins&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/build&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Pour bien faire, j'indique aussi au plugin eclipse que je souhaite disposer des codes sources, ce qui est toujours un très bon choix quand on travail avec du code open-source.&lt;/p&gt;


&lt;p&gt;Ensuite il faut en faire un projet eclipse&amp;nbsp;: allez dans le répertoire que vous venez de créér et lancez la commande maven eclipse:eclipse&lt;/p&gt;

&lt;pre class=&quot;bash&quot;&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;cd&lt;/span&gt; spring-mvc-webapp
mvn eclipse:eclipse&lt;/pre&gt;


&lt;p&gt;Puis importez le projet dans eclipse en vous étant assuré que la variable M2_REPO pointe bien vers votre répertoire ~/.m2/repository
Si ce n'est pas le cas&amp;nbsp;: windows &amp;gt; preference &amp;gt; Java &amp;gt; Build Path &amp;gt; Class path variables puis New pour créer une nouvelle variable.&lt;/p&gt;


&lt;p&gt;Par exemple pour moi M2_REPO = C:/Documents and Settings/michael_courcy/.m2/repository&lt;/p&gt;


&lt;h2&gt;Etape 2&amp;nbsp;: Exécution de l'application avec le serveur web jetty&lt;/h2&gt;


&lt;p&gt;Une fois notre projet créé nous allons modifier le pom.xml pour pouvoir exécuter ce projet sous le serveur web jetty&amp;nbsp;:&lt;/p&gt;


&lt;p&gt;Vous devriez obtenir ceci&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;xml&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;build&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;finalName&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;spring-mvc-webapp&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/finalName&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;plugins&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;plugin&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;maven-compiler-plugin&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;configuration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;source&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;1.5&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/source&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;target&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;1.5&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/target&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/configuration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/plugin&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;plugin&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;groupId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;org.mortbay.jetty&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/groupId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;maven-jetty-plugin&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;version&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;6.0.1&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/version&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;configuration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;scanIntervalSeconds&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;10&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/scanIntervalSeconds&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/configuration&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/plugin&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/plugins&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/build&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Remarquez qu'ici je n'ai fait que configurer jetty. Maven téléchargera lui-même jetty si nécessaire.&lt;/p&gt;


&lt;p&gt;Vérifions que notre configuration fonctionne bien et exécutons l'application.&lt;/p&gt;

&lt;pre class=&quot;bash&quot;&gt;mvn jetty:run&lt;/pre&gt;


&lt;p&gt;Si vous n'avez pas l'habitude d'utiliser maven pour vos applications web, Maven téléchargera nécessairement de nombreuses dépendances transitives. C'est peut être l'occasion de prendre un café ...&lt;/p&gt;


&lt;p&gt;Enfin, si tout s'est bien passé, vous pouvez vous connecter à l'url&amp;nbsp;: &lt;a href=&quot;http://localhost:8080/spring-mvc-webapp/&quot; hreflang=&quot;fr&quot;&gt;http://localhost:8080/spring-mvc-webapp/&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;Pour y découvrir l'éternel Hello World.&lt;/p&gt;


&lt;h2&gt;Etape 3&amp;nbsp;: Branchement de Spring MVC&lt;/h2&gt;


&lt;p&gt;Spring mvc fonctionne par certains aspects de façon similaire à Struts&amp;nbsp;: une servlet frontale va prendre l'ensemble des requêtes et les dispatcher à ses contrôleurs, ces contrôleurs sont des singletons issus d'une fabrique Spring.&lt;/p&gt;


&lt;p&gt;Donc ce que je vais trouver dans web.xml&amp;nbsp;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Une servlet pour dispatcher les requêtes aux contrôleurs.&lt;/li&gt;
&lt;li&gt;Un mapping de servlet&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;On aboutit à ce fichier web.xml&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;xml&quot;&gt;&lt;span style=&quot;color: #00bbdd;&quot;&gt;&amp;lt;!DOCTYPE web-app PUBLIC&lt;/span&gt;
&lt;span style=&quot;color: #00bbdd;&quot;&gt; &amp;quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&amp;quot;&lt;/span&gt;
&lt;span style=&quot;color: #00bbdd;&quot;&gt; &amp;quot;http://java.sun.com/dtd/web-app_2_3.dtd&amp;quot; &amp;gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;web-app&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;display-name&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Archetype Created Web Application&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/display-name&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;lt;!-- déclare la servlet frontale (très similaire à struts)  --&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;servlet&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;servlet-name&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;spring-mvc-webapp&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/servlet-name&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;servlet-class&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;org.springframework.web.servlet.DispatcherServlet&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/servlet-class&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;load-on-startup&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;2&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/load-on-startup&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/servlet&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;lt;!-- Toutes les requêtes se terminant par .html seront servis par la servlet frontal --&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;servlet-mapping&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;servlet-name&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;spring-mvc-webapp&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/servlet-name&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;url-pattern&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;*.html&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/url-pattern&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/servlet-mapping&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/web-app&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Avant de recopier ce fichier, coupez jetty (Ctrl + C dans le terminal).&lt;/p&gt;


&lt;p&gt;Ajoutez la dépendances à Spring et à spring-webmvc dans le pom.xml&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;xml&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;dependency&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;groupId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;org.springframework&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/groupId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;spring&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;version&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;2.5.3&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/version&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/dependency&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;dependency&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;groupId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;org.springframework&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/groupId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;spring-webmvc&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/artifactId&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;version&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;2.5.3&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/version&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/dependency&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;puis régénérez votre projet eclipse&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;bash&quot;&gt;mvn eclipse:clean eclipse:eclipse&lt;/pre&gt;

&lt;p&gt;enfin rafraîchissez votre projet eclipse quand l'opération est finie pour voir apparaitre vos librairies et éventuellement consulter le code source.&lt;/p&gt;


&lt;p&gt;On remarque immédiatement à la lecture de web.xml qu'on ne voit pas comment on informe la servlet frontale des contrôleurs auxquels elle va devoir redispatcher les requêtes.&lt;/p&gt;


&lt;p&gt;Par convention, si la servlet s'appelle spring-mvc-webapp, alors elle ira chercher sa fabrique de contrôleurs dans WEB-INF/spring-mvc-webapp-servlet.xml. C'est une convention qui peut être surdéfinie mais que nous allons suivre ici.&lt;/p&gt;


&lt;p&gt;Voici donc le fichier spring-mvc-webapp-servlet.xml. Les commentaires détaillent chaque élément&amp;nbsp;: je vous invite à les lire.&lt;/p&gt;
&lt;pre class=&quot;xml&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;?xml&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;version&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;encoding&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;UTF-8&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;lt;!--
  - Contexte d'application pour spring-mvc-webapp.
--&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;beans&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;xmlns&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://www.springframework.org/schema/beans&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;xmlns:xsi&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&lt;/span&gt;
		&lt;span style=&quot;color: #000066;&quot;&gt;xmlns:p&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://www.springframework.org/schema/p&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;xmlns:context&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://www.springframework.org/schema/context&amp;quot;&lt;/span&gt;
		&lt;span style=&quot;color: #000066;&quot;&gt;xsi:schemaLocation&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;lt;!--
    - Tous les contrôleurs sont automatiquement détectés grâce à l'annotation @Controller.
    - On définit ici dans quel package le post processor doit chercher ces beans éventuellement annotés.
  --&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;context:component-scan&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;base-package&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;com.netapsys.springmvc.web&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;lt;!--
      - Les controlleurs de cette application fournissent une annotation @RequestMapping 
      - Qui peuvent être déclaré de deux manière différentes : 
      - Au niveau de la classe par exemple : 
      -     @RequestMapping(&amp;quot;/addVisit.html&amp;quot;)
      -     Pour ce type de controlleurs on peut annoter les méthodes pour qu'elles
      -     prennent en charge une requête Post ou une requête Get
      -     @RequestMapping(method = RequestMethod.GET)
      - Ou alors au niveau de chaque méthode, différents exemples  seront fournis.		 
    --&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;bean&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;class&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;&amp;lt;!--
        Ceci est le view resolver, il permet de définir la technologie de vue utilisée et comment
        sélectionner une vue. Ici on prendra la solution la plus simple, elle permet de mapper 
        le nom de la vue retournée avec la sélection d'une jsp.
        Exemple si le nom de la vue retournée est &amp;quot;foo/bar/hello&amp;quot; alors on utilisera le fichier
        WEB-INF/jsp/foo/bar/hello.jsp pour constuire la vue. 
      --&amp;gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;bean&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;class&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;org.springframework.web.servlet.view.InternalResourceViewResolver&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;p:prefix&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;/WEB-INF/jsp/&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;p:suffix&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;.jsp&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/beans&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Tout est en place maintenant pour écrire notre premier contrôleur et notre première jsp.&lt;/p&gt;


&lt;h2&gt;Etape 4&amp;nbsp;: Notre premier contrôleur&lt;/h2&gt;


&lt;p&gt;Créez le source folder /src/main/java/
et créez la classe HelloSpringMVC dans le package com.netapsys.springmvc.web&lt;/p&gt;
&lt;pre class=&quot;java&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;package&lt;/span&gt; com.&lt;span style=&quot;color: #006600;&quot;&gt;netapsys&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;springmvc&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;web&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #a1a100;&quot;&gt;import org.springframework.stereotype.Controller;&lt;/span&gt;
&lt;span style=&quot;color: #a1a100;&quot;&gt;import org.springframework.ui.ModelMap;&lt;/span&gt;
&lt;span style=&quot;color: #a1a100;&quot;&gt;import org.springframework.web.bind.annotation.RequestMapping;&lt;/span&gt;
&lt;span style=&quot;color: #a1a100;&quot;&gt;import org.springframework.web.bind.annotation.RequestMethod;&lt;/span&gt;
&lt;span style=&quot;color: #a1a100;&quot;&gt;import org.springframework.web.bind.annotation.RequestParam;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/**
 * Contrôleur que l'on invoquera pour l'url /helloSpringMVC.html.
 * 
 * @author Michael Courcy
 *
 */&lt;/span&gt;
@Controller
@RequestMapping&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;/helloSpringMVC.html&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; HelloSpringMVC &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/**
    * Handler de la méthode Get pour l'URL /helloSpringMVC.html. 
    * 
    * @param name le nom que l'on doit afficher dans la vue.
    * @param model une map de toutes les données qui seront utilisables dans la vue 
    * @return le nom de la vue qu'il faudra utiliser.
    */&lt;/span&gt;
  @RequestMapping&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;method = RequestMethod.&lt;span style=&quot;color: #006600;&quot;&gt;GET&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt;  &lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;String&lt;/span&gt; sayHelloWithSpringMVC&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;
    @RequestParam&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;value=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;,required=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;false&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;String&lt;/span&gt; name, 
    ModelMap model&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; 
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    model.&lt;span style=&quot;color: #006600;&quot;&gt;addAttribute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;,name&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// on utilisera donc le fichier /WEB-INF/jsp/hello.jsp&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//au regard de la stratégie de résolution des vues &lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//utilisée dans cette application.&lt;/span&gt;
    &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Plusieurs remarques sont à faire à la lecture de ce contrôleur&amp;nbsp;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Le contrôleur n'hérite d'aucune classe particulière, il est identifié comme contrôleur grâce à l'annotation @Controller&lt;/li&gt;
&lt;li&gt;C'est encore une annotation qui permet de savoir quelle méthode invoquer pour une requê&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;te de type Get&amp;nbsp;: @RequestMapping(method = RequestMethod.GET)&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Notre méthode prend un paramètre supplémentaire ModelMap qui est en fait un HashMap amélioré dans lequel on place toutes les informations que l'on souhaite disponibles lors de la génération de la vue avec la jsp.&lt;/li&gt;
&lt;li&gt;Le code n'utilise pas directement l'objet HttpServletRequest mais il indique encore grâce aux annotations comment renseigner les paramètres à partir du contenu de l'objet HttpServletRequest @RequestParam(value=&quot;name&quot;,required=false)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ce dernier point est très important&amp;nbsp;: ne pas utiliser directement l'objet HttpServletRequest nous permet de créer des tests unitaires très simplement sans avoir à créer de request mock.&lt;/p&gt;


&lt;p&gt;Créez le source folder /src/test/java et créez aussi le package com.netapsys.springmvc.web pour y placer le test suivant&lt;/p&gt;
&lt;pre class=&quot;java&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;package&lt;/span&gt; com.&lt;span style=&quot;color: #006600;&quot;&gt;netapsys&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;springmvc&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;web&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #a1a100;&quot;&gt;import org.springframework.ui.ModelMap;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #a1a100;&quot;&gt;import junit.framework.TestCase;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; HelloSpringMCVTest &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;extends&lt;/span&gt; TestCase &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; testSayHelloWithSpringMVC&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
    HelloSpringMVC helloSpringMVC = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; HelloSpringMVC&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    ModelMap model = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; ModelMap&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;String&lt;/span&gt; view = helloSpringMVC.&lt;span style=&quot;color: #006600;&quot;&gt;sayHelloWithSpringMVC&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;mic&amp;quot;&lt;/span&gt;, model&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//vérifions que c'est bien la vue hello qui est générée&lt;/span&gt;
    assertEquals&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;hello&amp;quot;&lt;/span&gt;, view&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//vérifions que le modele contient bien un attribut name&lt;/span&gt;
    assertTrue&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;model.&lt;span style=&quot;color: #006600;&quot;&gt;containsAttribute&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//vérifions que cet attribut name est bien &amp;quot;mic&amp;quot;&lt;/span&gt;
    assertEquals&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;mic&amp;quot;&lt;/span&gt;,model.&lt;span style=&quot;color: #006600;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;name&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Vous pouvez lancer ce test avec maven&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;bash&quot;&gt;mvn -&lt;span style=&quot;color: #007800;&quot;&gt;Dtest=&lt;/span&gt;HelloSpringMCVTest &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;test&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;pour un test particulier ou alors&lt;/p&gt;
&lt;pre class=&quot;bash&quot;&gt;mvn &lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;test&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;pour refaire tous les tests.&lt;/p&gt;


&lt;h2&gt;Etape 5&amp;nbsp;: Notre propre jsp&lt;/h2&gt;


&lt;p&gt;Créez le fichier WEB-INF/jsp/hello.jsp&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;html&quot;&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&amp;lt;%@ page &lt;span style=&quot;color: #000066;&quot;&gt;language&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;java&amp;quot;&lt;/span&gt; contentType&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;text/html; charset=ISO-8859-1&amp;quot;&lt;/span&gt;
    pageEncoding&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;ISO-8859-1&amp;quot;&lt;/span&gt;%&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&amp;lt;%@ page isELIgnored &lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;false&amp;quot;&lt;/span&gt; %&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #00bbdd;&quot;&gt;&amp;lt;!DOCTYPE html PUBLIC &amp;quot;-//W3C//DTD HTML 4.01 Transitional//EN&amp;quot; &amp;quot;http://www.w3.org/TR/html4/loose.dtd&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;html&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;head&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;meta&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;http-equiv&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Content-Type&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;content&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;text/html; charset=ISO-8859-1&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;title&amp;gt;&lt;/span&gt;&lt;/span&gt;Spring mvc hello !!&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;body&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
Hello spring mvc vous donne votre nom qui est : ${name}
&amp;nbsp;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Il ne vous reste plus qu'à tester&lt;/p&gt;
&lt;pre class=&quot;bash&quot;&gt;mvn jetty:run&lt;/pre&gt;


&lt;p&gt;Puis à essayer l'url suivante&amp;nbsp;: &lt;a href=&quot;http://localhost:8080/spring-mvc-webapp/helloSpringMVC.html?name=mic&quot; hreflang=&quot;fr&quot;&gt;http://localhost:8080/spring-mvc-webapp/helloSpringMVC.html?name=mic&lt;/a&gt;&lt;/p&gt;</description>
    
          <enclosure url="http://blog.netapsys.fr/public/code/spring-mvc-webapp.zip"
      length="23450" type="application/zip" />
    
    
          <comments>http://blog.netapsys.fr/index.php/post/2008/04/13/Introduction-A-Spring-MVC#comment-form</comments>
      <wfw:comment>http://blog.netapsys.fr/index.php/post/2008/04/13/Introduction-A-Spring-MVC#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.netapsys.fr/index.php/feed/rss2/comments/14</wfw:commentRss>
      </item>
    
  <item>
    <title>Hibernate - Utilisation efficace de l'API Criteria</title>
    <link>http://blog.netapsys.fr/index.php/post/2008/04/02/Hibernate-utilisation-efficace-de-lAPI-Criteria</link>
    <guid isPermaLink="false">urn:md5:c9e5dde5fec8e2aa4db6e5d7c8e18b6c</guid>
    <pubDate>Wed, 02 Apr 2008 17:06:00 +0200</pubDate>
    <dc:creator>Michael Courcy</dc:creator>
        <category>Java</category>
        <category>criteria</category><category>hibernate</category>    
    <description>&lt;p&gt;Utiliser l'API criteria plutôt que le langage de requête HQL présente de sérieux avantages. Parmi l'un deux, on peut en considérer un particulièrement interessant&amp;nbsp;: la décoration de requête.&lt;/p&gt;    &lt;p&gt;Supposons que vous ayez écrit une classe de DAO assez générique avec une méthode de requête paginée.&lt;/p&gt;

&lt;pre class=&quot;java&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; GenericDaoHibernate&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;lt;&lt;/span&gt;T, PK &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;extends&lt;/span&gt; Serializable&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;implements&lt;/span&gt; GenericDao&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;lt;&lt;/span&gt;T, PK&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/**
      * La classe persistante sur laquelle nous allons faire nos requêtes
      */&lt;/span&gt;
    &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;private&lt;/span&gt; Class&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&lt;/span&gt; persistentClass&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/**
     * Constructeur initialisant la classe persistante à manipuler
     * Ceci nous permettra de développer des classes de DAO fortement typée.
     * 
     * @param persistentClass the class type you'd like to persist
     */&lt;/span&gt;
    &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; GenericDaoHibernate&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;final&lt;/span&gt; Class&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&lt;/span&gt; persistentClass&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
        &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;this&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;persistentClass&lt;/span&gt; = persistentClass&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; List&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;lt;&lt;/span&gt;T&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&lt;/span&gt; getAll&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;final&lt;/span&gt; SearchCriteria searchCriteria&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//récupérons la session et créons un critère&lt;/span&gt;
        &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;final&lt;/span&gt; Session session = getSessionFactory&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;getCurrentSession&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;final&lt;/span&gt; Criteria crit = session.&lt;span style=&quot;color: #006600;&quot;&gt;createCriteria&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;persistentClass&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// gestion de l'ordre des résultats&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;searchCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;isDesc&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
            crit.&lt;span style=&quot;color: #006600;&quot;&gt;addOrder&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Order.&lt;span style=&quot;color: #006600;&quot;&gt;desc&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;searchCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getActiveOrder&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
            crit.&lt;span style=&quot;color: #006600;&quot;&gt;addOrder&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Order.&lt;span style=&quot;color: #006600;&quot;&gt;asc&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;searchCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getActiveOrder&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Les sous classes de GenericDaoHibernate&lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// devront redéfinir cette méthode pour ajouter des &lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// critères supplémentaires&lt;/span&gt;
        addExtraCriteria&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;crit, searchCriteria&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Procédons à un comptage du résultat total de la requête&lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// mais sans charger la collection d'objets.&lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// D'une certaine manière on peut dire que le &lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// critère bascule en mode &amp;quot;comptage&amp;quot;.&lt;/span&gt;
        crit.&lt;span style=&quot;color: #006600;&quot;&gt;setProjection&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Projections.&lt;span style=&quot;color: #006600;&quot;&gt;countDistinct&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;id&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;final&lt;/span&gt; &lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;Long&lt;/span&gt; totalResult = &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;Long&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #aaaadd; font-weight: bold;&quot;&gt;Integer&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; crit.&lt;span style=&quot;color: #006600;&quot;&gt;uniqueResult&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        searchCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;setTotalResult&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;totalResult&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On rebascule en mode &amp;quot;liste d'objets&amp;quot;&lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// si l'on est amené a faire des jointures assurons nous que la liste &lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;//qui nous est retournée est réellement unique       &lt;/span&gt;
        crit.&lt;span style=&quot;color: #006600;&quot;&gt;setProjection&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        crit.&lt;span style=&quot;color: #006600;&quot;&gt;setResultTransformer&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Criteria.&lt;span style=&quot;color: #006600;&quot;&gt;DISTINCT_ROOT_ENTITY&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// puis retournons la liste dans les limites de la pagination&lt;/span&gt;
        &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;return&lt;/span&gt; crit
                .&lt;span style=&quot;color: #006600;&quot;&gt;setMaxResults&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;searchCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getNumberOfResultPerPage&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
                .&lt;span style=&quot;color: #006600;&quot;&gt;setFirstResult&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #993333;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;searchCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getActivePage&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; - &lt;span style=&quot;color: #cc66cc;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;  &lt;span style=&quot;color: #66cc66;&quot;&gt;*&lt;/span&gt; searchCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getNumberOfResultPerPage&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
                .&lt;span style=&quot;color: #006600;&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/**
     * Les sous classes doivent surdéfinir cette méthode pour ajouter plus de contraintes à la recherche.
     * 
     * @param crit  le critere  hibernate que l'on veut modifier.
     * @param searchCriteria Le critère de recherche fourni par la couche service.
     */&lt;/span&gt;
    &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; addExtraCriteria&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;final&lt;/span&gt; Criteria crit,
            &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;final&lt;/span&gt; SearchCriteria searchCriteria&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// rien : c'est la sous classe qui redéfinira le corps de la méthode&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Si vous lisez attentivement ce code, vous constatez qu'il couvre des besoins très génériques&amp;nbsp;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Récupération d'une collection d'objets&lt;/li&gt;
&lt;li&gt;Pagination des résultats&lt;/li&gt;
&lt;li&gt;Gestion systématique d'un ordonnancement&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Et bien entendu, on souhaiterait ne pas récrire ça à chaque requête ... Par exemple ProductHibernateDao devrait pouvoir utiliser ce travail en restreignant le résultat à une catégorie ou à la comparaison d'une chaîne dans le libellé des contenus localisés du produit.&lt;/p&gt;


&lt;p&gt;Heureusement, pour ce faire, ProductHibernateDao n'a qu' à redéfinir la méthode addExtraCriteria(final Criteria crit,   final SearchCriteria searchCriteria).&lt;/p&gt;


&lt;p&gt;Voici un exemple d'implémentation de ProductHibernateDao&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;java&quot;&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt; ProductDaoHibernate
    &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;extends&lt;/span&gt; GenericDaoHibernate&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;lt;&lt;/span&gt;Product, Long&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;implements&lt;/span&gt; ProductDao
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp;
   &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; ProductDaoHibernate&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
        &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;super&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Product.&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;    
&amp;nbsp;
&amp;nbsp;
    &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;/**
     * {@inheritDoc}
     * 
     * @see com.netapsys.shop.dao.hibernate.GenericDaoHibernate#addExtraCriteria(org.hibernate.Criteria,
     * com.netapsys.shop.flow.SearchCriteria)
     */&lt;/span&gt;
    @Override
    &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #993333;&quot;&gt;void&lt;/span&gt; addExtraCriteria&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;final&lt;/span&gt; Criteria crit,
            &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;final&lt;/span&gt; SearchCriteria searchCriteria&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
        ProductCriteria productCriteria = &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;ProductCriteria&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; searchCriteria&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On restreint la recherche a une catégorie si elle est définie.&lt;/span&gt;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;productCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getCategory&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;= &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;null&lt;/span&gt;
                &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; productCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getCategory&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;getId&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;= &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
            crit.&lt;span style=&quot;color: #006600;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Expression.&lt;span style=&quot;color: #006600;&quot;&gt;eq&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;category.id&amp;quot;&lt;/span&gt;, productCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getCategory&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;
                    .&lt;span style=&quot;color: #006600;&quot;&gt;getId&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
        &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;productCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getSearchOn&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;= &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;null&lt;/span&gt;
                &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;!&lt;/span&gt;productCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getSearchOn&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;equals&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;none&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
            &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// On recherche une chaîne de caractères &amp;quot;sword&amp;quot; dans la référence produit&lt;/span&gt;
            &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;productCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getSearchOn&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;equals&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;ref&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
                crit.&lt;span style=&quot;color: #006600;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;Expression.&lt;span style=&quot;color: #006600;&quot;&gt;ilike&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;ref&amp;quot;&lt;/span&gt;, productCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getSword&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;,
                        MatchMode.&lt;span style=&quot;color: #006600;&quot;&gt;ANYWHERE&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
            &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// Remarquez qu'ici j'utilise une fonction puissante de &lt;/span&gt;
            &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// l'API Criteria qui me permet de rechercher &lt;/span&gt;
            &lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;// dans une sous collection de Product.&lt;/span&gt;
            &lt;span style=&quot;color: #b1b100;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;productCriteria.&lt;span style=&quot;color: #006600;&quot;&gt;getSearchOn&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;equals&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;title&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#123;&lt;/span&gt;
                crit.&lt;span style=&quot;color: #006600;&quot;&gt;createCriteria&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;localizedProductContents&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #006600;&quot;&gt;add&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;
                        Expression.&lt;span style=&quot;color: #006600;&quot;&gt;ilike&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;publishInfo.title&amp;quot;&lt;/span&gt;, productCriteria
                                .&lt;span style=&quot;color: #006600;&quot;&gt;getSword&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;, MatchMode.&lt;span style=&quot;color: #006600;&quot;&gt;ANYWHERE&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #66cc66;&quot;&gt;;&lt;/span&gt;
            &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
        &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
    &lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #66cc66;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Ainsi, tout le processus de requêtage et de pagination a pu être mutualisé dans GenericHibernateDao alors que le requêtage particulier a été déplacé dans les classes dérivées, ici par exemple ProductHibernateDao.&lt;/p&gt;


&lt;p&gt;On a d'une certaine manière décoré la requête en modifiant l'objet Criteria par la classe fille. Il ne s'agit pas néanmoins de la véritable mise en oeuvre d'un pattern décorateur mais la chose pourrait tout à fait être envisagée avec l'utilisation des critères détachés.&lt;/p&gt;</description>
    
    
    
          <comments>http://blog.netapsys.fr/index.php/post/2008/04/02/Hibernate-utilisation-efficace-de-lAPI-Criteria#comment-form</comments>
      <wfw:comment>http://blog.netapsys.fr/index.php/post/2008/04/02/Hibernate-utilisation-efficace-de-lAPI-Criteria#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.netapsys.fr/index.php/feed/rss2/comments/9</wfw:commentRss>
      </item>
    
</channel>
</rss>