Home

Spring MVC - gardeux

image

Contents

1. return modelazndView Enfin nous cr ons la vue jsp carList jsp html gt body gt lt h1 gt Car List lt h1 gt c forEach items S carList var car S car brand name car model car price lt br gt c forEach lt body gt z html Vous pouvez maintenant compiler votre application et l ex cuter sur un serveur Tomcat Si tout fonctionne correctement vous verrez s afficher une superbe vue d un garage ma fois assez r duit A File Edit View History del icio us Bookmarks Tools Help ie M And e E TAG Car List Mercedes SL 500 40000 Peugeot 607 35000 j nttp localhost 8180 springmvc list cars html Et voila Bonne utilisation de Spring MVC
2. er le fichier de configuration WEB INF springmvc servlet xml nom bas sur la servlet K2xml version 1 0 encoding uTF 8 BP Xbeans xmlns http www springframework org schema beans xmlns xsi http www w3 0rg 2001 XMLSchema instance xsi schemaLocation http www springframework org schema beans http www springframework org schema beans spring beans 2 5 xsd lt bean name hello world html class springmve web HelloWorldController gt lt bean id viewResolver class org springframework web servlet view InternalResourceViewResolver lt property name viewClass value org springframework web servlet view JstlView lt property name prefix value jsp lt property name suffix value jsp lt bean gt lt beans gt Dans ce fichier nous avons ainsi mapp l url hello world html au contr leur HelloWorldController D clar un viewResolver quand la vue view name est appel par le contr leur le fichier jsp view name jsp sera utilis Nous allons maintenant cr er le contr leur correspondant notre vue dans le dossier WEB INF src springmvc web HelloWorldController java package springmvc web import java io IOException import javax servlet ServletException import javax servlet http HttpServletRequest import javax servlet http HttpServletResponse import org springframework web servlet ModelAndView import org springframework web servlet mvc Controller public cl
3. EISTI Spring MVC Manuel d installation et d utilisation VILLETTE Charles BEQUET Pierre DE SAINT MARTIN C dric CALAY ROCHE Vincent NAUTRE Francois 20 05 2010 Logiciels Pr requis Eclipse http www eclipse org Tomcat http tomcat apache org Fichiers utilis s par dans le tutorial http www springsource org download Vous pourrez via cette page et en vous inscrivant au pr alable t l charger les jar n cessaires l utilisation de Spring Etape 1 Dans un premier temps vous aurez besoin de java 1 5 ou sup rieur ainsi que de tomcat et Eclipse Tout d abord cr ez un nouveau dossier springmvc A l int rieur de celui ci cr ez les dossiers classes jsp et WEB INF Dans le dossier jsp ajoutez la nouvelle jsp index jsp html gt body gt lt prHi lt p gt lt body gt lt html gt Dans le dossier WEB INF ajoutez le fichier web xml et cr er le dossier src E xml version 1 0 encoding uTF 8 P3 lt web app version 2 4 xmlns http java sun com xml ns j2ee xmlns xsi http www w3 0rg 2001 XMLSchema instance xsi schemaLocation http java sun com xml ns j2ee http java sun com xml ns j2ee web app 2 4 xsd gt lt welcome file list gt lt welcome file gt jsp index jsp lt welcome file gt lt welcome file list gt lt web app gt Ex cutez ensuite votre application l aide de Tomcat Vous devriez voir s afficher un mag
4. ass HelloWorldController implements Controller public ModelAndView handleRequest HttpServletRequest request HttpServletResponse response throws ServletException IOException String aMessage Hello World MVC ModelaAndView modelAndView new ModelAndView hells world modelAndView addObj ect message aMessage return modelAndView Le contr leur appelle la vue hello world affichant message Nous allons maintenant nous occuper de la vue jsp hello world jsp lt 40 taglib prefix c uri http java sun com jsp jstl core html body lt p gt This is my message message lt p gt lt body gt lt html gt Cette jsp affichera l attribut pr c demment d clar dans le contr leur Vous pouvez enfin afficher le message dans votre navigateur pr f r IE n est pas votre navigateur pr f r Si votre r sultat est le suivant a marche File Edit View History del icio us Bookmarks Tools Help T T F gt oM This is my message Hello World MVC Etape 4 Nous allons maintenant utiliser le mod le MVC et le mettre en place Nous allons cr er une page affichant une liste de voitures et leurs caract ristiques id Long model String name String price BigDecimal country String Nous avons ainsi besoin D un mod le de classes Car et Brand Un manager de classe afin d obtenir la liste de voitures Uncontr leur de class va utiliser les
5. ew Brand brandl setId 1ong i brandi setName Mercedes brand1 setCountry Germany Brand brand new Brand brand2 setrId long 2 brandz setNHame Peugeot brand setCountry France Car carl new Car carl setId long 1 carl serBrand brandl carl setrModel S5SL 500 carl setPrice new BigDecimal 50000 Car car new Car carz setlId long 2 car2 setBrand brand2 carz setModel 6 amp 07 car2 setPrice new BigDecimal 35000 carList new LinkedList Car carList add car1l carList add car2 public List Car getCarList return carList gt Controller Dans le fichier springmvc servlet xml il nous faut d clarer l URL lt bean name list cars html class springmvc web CarListController Puis nous cr ons le contr leur WEB INF src springmvc web CarListController java package springmvc web import javax servlet http HttpServletRequest import javax servlet http HttpS3ervletResponse import org springframework web servlet Modela2ndView import org springframework web servlet mvc Controller import springmvc service CarManager public class CarListController implements Controller public ModelAndView handleRequest HttpServletRequest arg HttpServletResponse argl throws Exception CarManager carManager new CarManager ModelandView modela2ndView new Model ndView carLnist modelAndView addObject carList carManager getCarList
6. m thodes du manager Une vue jsp affiche la liste des voitures gt Mod les Brand java et Car java package springmvc model package springmvc model import java math BigDecimal public class Brand private Long id public class Car private String name PPT Bong my rivate Brand brand private String country A private String model private BigDecimal price public String getCountry return country public Long getId return id public void setCountry String country T public void setId Long id 1 this country country iig dod public Long getId public Brand getBrand return id return brand public void setBrand Brand brand public void setId Long id Chia Exudd e ui this id id public String getModel public String getName return model return name 2 EM public void setModel String model 1 this model model public void setName String name this name name public BigDecimal getPrice return price i public void setPrice BigDecimal price this price price gt Manager Class A cr er dans WEB INF src springmvc service CarManager java package springmvc service import java math BigDecimal import java util LinkedList import java util List import springmvc model Brand import springmvc model cCar public class CarManager private static List Car carLisrt static Brand brandi n
7. mework dist spring jar spring framework dist modules spring webmvc jar spring framework lib jakarta taglibs standard jar spring framework lib jakarta commons commons logging jar spring framework lib j2ee servlet api jar spring framework lib j2ee jstl jar Etape 3 gt Comment marche Spring MVC Basiquement de la m me fa on que Struts HTTP request Dispatcher Servlet Controller Java class HTTP response View JSP En se basant sur l url de la requ te HTTP le DispatcherServlet appelle le contr leur correspondant Une vue est ensuite rendu et envoy en tant que r ponse HTTP Dans le fichier web xml nous allons d clarer le dispatcher de Servlet et mapper les html lt xml version 1 0 encoding uTF 8 P lt web app version 2 4 xmlns http java sun com xml ns j2ee xmlns xsi http www w3 org 2001 xMLSchema instance xsi schemaLocation http java sun com xml ns j2ee http java sun com xml ns j2ee web app 2 4 xsd gt lt servlet gt zservlet name springmvcoc servlet name zservlet class org springframework web servlet DispatcherServletc servlet class Zload on startup 1z load on startup c servlet lt servlet mapping gt lt servlet name gt sprinogmve lt servlet namez url pattern html z url pattern servlet mapping lt welcome ile listz zwelcome file jsp index jsp lt fwelcome ilez z welcome file 1ist lt web app Nous allons maintenant cr
8. nifique Hi signifiant que l affichage de la jsp s est d roul correctement Etape 2 Dans le dossier WEB INF ajoutez maintenant le fichier build properties appserver home usr share tomcat5 5 appserver lib S appserver home common lib Puis toujours dans le m me dossier ajoutez le fichier build xml K xml version 1 0 encoding uTF 8 P3 lt project basedir default build gt lt property file build properties gt lt property name src dir value src gt lt property name build dir value classes gt lt path id build classpath lt fileset dir lib gt lt include name jar gt lt fileset gt Xfileset dir appserver lib lt servlet API classes gt lt include name servlet jar lt ileset gt Xpathelement path build dir gt lt path gt target name build gt mkdir dir build dir gt lt javac destdir build dir source 1 5 target 1 5 debug true deprecation false optimize false failonerror true gt lt src path S S src dir gt lt classpath refid build classpath gt lt javac gt lt target gt lt target name clean description Clean output directories gt delete f ileset dir S S build dir gt include name class lt ileset gt lt delete gt lt target gt lt project Dans le dossier WEB INF cr ez le dossier lib et ajoutez y les jar suivants spring fra

Download Pdf Manuals

image

Related Search

Related Contents

MODE D`EMPLOI POUR LA CONSULTATION PUBLIQUE  20W Ni-MH Battery Charger  Toshiba D-RW2SU/D-RW2SC DVD Recorder User Manual  SDRN/SCI-Diabetes Research Register Data Entry – User Guide  Jasco 52060 Instruction Manual  Radio Control Basics  

Copyright © All rights reserved.
Failed to retrieve file