jee approfondi chamilah ingar - guillaume monteil - cyril tovena

37
JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Upload: gauvain-bachelet

Post on 03-Apr-2015

118 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

JEE Approfondi

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 2: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

I- Makumbaa) Introductionb) Installationc) Exempled) Test

II- XMLBeansa) Introductionb) Installationc) Exempled) Test

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 3: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 4: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Conception en 2001 par une association d’étudiants

Basé sur le design pattern MVC

Librairie de tags JSP => facilite l’interaction entre l’application et la BDD associée

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 5: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Outils nécessaires :

Makumba ! Un conteneur de servlets JEE Un serveur SQL Un driver JDBC ANTLR : Another Tool for Language

Recognition

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 6: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Arborescence du projet

File > New > Dynamic Web Projet

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

monProjet/ WEB-INF/ lib/ classes/ web.xml

Page 7: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Ajout des librairies (sous Eclipse)

Clic droit sur le projet > Build Path

> Configure Build Path

> Onglet Librairies > Add External JARs

makumba.jarantlr-?.?.?.jarmysql-connector-java-?.?.?-bin.jar

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 8: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Configuration du web.xml

Le fichier est déjà fait si on utilise Tomcat : Aller sur :

http://www.makumba.org/makumba-install.html

Récupérer le web.xml dans la partie « Deployment Descriptor »

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 9: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Fichiers de configuration BDD (1/2)monProjet/WEB-INF/classes/localhost_mysql_maBDD.properties

Clic droit sur le dossier classes> New > File

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

# Nom d’utilisateur pour se connecter à la BDDsql.user=root# Mot de passesql.password= # Autoriser Makumba à modifier la BDDalter#=true

maBDD est le nom de la base de données

Page 10: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Fichiers de configuration BDD (2/2)monProjet/WEB-INF/classes/MakumbaDatabase.properties

Clic droit sur le dossier classes> New > File

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

#Base de données par défautdefault=localhost_mysql_monProjet

Page 11: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Définition des donnéesmonProjet/WEB-INF/classes/dataDefinitions/*.mdd

Créer un dossier nommé « dataDefinitions » dans le dossier « classes »

Créer un fichier nomTable.mdd par table existante

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

# Personne.mdd# Fichier de description pour la table Personne nom=not null char[32] ; age=not null int ;

Page 12: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

ApplicationmonProjet/WEB-INF/index.jsp

Clic droit sur monProjet

> New > JSP > index.jsp

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

<%@taglib uri="http://www.makumba.org/presentation" prefix="mak" %>

<mak:list from="Person p">   <mak:value expr="p.name"/>, <mak:value expr="p.age"/>

</mak:list>

Les balises « mak » permettent d’agir sur

la BDD et d’y récupérer des

données.

Page 13: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Après l’installation on lance la jsp et on obtient une erreur de lancement du serveur Tomcat :

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

java.lang.reflect.InvocationTargetExceptionat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)Caused by: java.lang.SecurityException: La servlet de classe org.apache.catalina.servlets.InvokerServlet est privil�gi�e (privileged) et ne peut �tre charg� par cette application webat org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1145)at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:992)at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4058)at org.apache.catalina.core.StandardContext.start(StandardContext.java:4371)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)at org.apache.catalina.core.StandardService.start(StandardService.java:516)at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)at org.apache.catalina.startup.Catalina.start(Catalina.java:578)... 6 more

Page 14: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

La configuration de Tomcat ne convient pas à Makumba !

Dans le répertoire d’installation de tomcat, trouver le fichier context.xml dans le répertoire conf

Ajouter l’attribut « privileged=¨true¨ » à la balise <context>

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 15: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

On supprime la configuration de tomcat sous Eclipse et on relance la page jsp.

On obtient cette fois :org.makumba.commons.RuntimeWrappedException:

java.lang.NoClassDefFoundError: ch/ubique/inieditor/IniEditor

Page 16: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

En fait il manque toutes les autres librairies extérieures

On peut les trouver dans la version sources de makumba et il faut les ajouter toutes sauf « jsp-api.jar » car Tomcat inclut déjà cette librairie dans la version 6

Page 17: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Ca fonctionne enfin !

Une table person_ est créée.

Si on insère des champs il seront listés par la JSP.

Page 18: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Structure de la table auto créée

Page 19: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Un autre exemple <mak:newForm>

Page 20: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

<mak:editForm>

Page 21: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 22: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Conception en 2003 par Apache

Permet le databinding Java/XML

Travail à partir des schémas XSD

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 23: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Outils nécessaires

JDK (minimum 1.4) ANT Saxon XSLT et XQuery XMLBeans

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 24: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Variables d’environnement – Windows (1/3)(après avoir téléchargé et dézippé XMLBeans)

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 25: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Variables d’environnement – Windows (2/3)(après avoir téléchargé et dézippé XMLBeans)

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 26: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Variables d’environnement – Windows (3/3)(après avoir téléchargé et dézippé XMLBeans)

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Pour la V2 de XMLBeans, il faut également ajouterjsr173_1.0_api.jar

Page 27: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Variables d’environnement – Linux(après avoir téléchargé et dézippé XMLBeans)

export XMLBEANS_HOME = « chemin » export

PATH=$PATH:$XMLBEANS_HOME/bin export

CLASSPATH=$XMLBEANS_HOME/lib/xbean.jar:$CLASSPATH

For XMLBeans V2, export CLASSPATH=$CLASSPATH:$XMLBEANS_HOME/lib/jsr173[_1.0]_api.jar

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 28: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Ajout des librairies au projet (Eclipse)

Toutes les librairies se trouvant dans le répertoire lib du dossier xmlbeans sauf xmlpublic.jar

Pour une compatibilité XPath/XQuery, ajouter les librairies de saxon.

Les librairies obtenues après génération des classes (cf slide suivant).

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Page 29: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Génération des classes : 1ère méthode

Elle se fait à partir d’un schéma XSD Création d’une tâche ANT (build.xml) :

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

<?xml version="1.0" encoding="UTF-8" ?>  <project name= " monProjet" default="xmlbean" basedir=".">      <property name="xmlbeans.home" value="." />      <property name="schema" value="schema/monSchema.xsd" />      <property name="dest.jar" value="dest/mesSchemas.jar" />        <path id="xmlbeans.path">        <fileset dir="${xmlbeans.home}/lib"   includes="*.jar"/>      </path>        <taskdef name="xmlbean"  classname="org.apache.xmlbeans.impl.tool.XMLBean"   classpathref="xmlbeans.path"    />            <target name="xmlbean">         <xmlbean schema="${schema}"                   srcgendir="src"                   destfile="${dest.jar}"                   classpathref="xmlbeans.path" />      </target>  </project>  

L’archive mesSchemas.jar est à placer dans le build path du

projet.

Page 30: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Génération des classes : 2ème méthode

Elle se fait à partir d’un schéma XSD Utilisation de la commande SCOMP

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

test.jar : version compilée à ajouter

aux librairies du projet

src contient les classes Java non compilées

Page 31: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Si vous utilisez XMLBeans avec Eclipse et Apache Tomcat, et que vous obtenez l’erreur : ExceptionClassNotFound « Impossible de compiler la page », il faut faire :

Clic droit sur le projet > Propriétés > Java EE Module Dependencies

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Problème avec Eclipse

Il faut cocher toutes les

cases.

Page 32: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Génération d’un fichier XML

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

// import des classes Java générées précédemment

public class GenerationXML {         public static void main(String[] args) throws IOException{                               ParcautomobileDocument parc =  ParcautomobileDocument.Factory.newInstance();                           Parcautomobile parcautomobile =  parc.addNewParcautomobile();                     Vehicule vehicule1 = parcautomobile.addNewVehicule();                                     vehicule1.setModele(Modele.COUPE);                            Marque marque1 = vehicule1.addNewMarque();              marque1.setNom(Nom.RENAULT);                            boolean estValide = parc.validate();  XmlOptions opts = new XmlOptions();                          opts.setSavePrettyPrint();                             System.out.println(parc.xmlText(opts));               parc.save(new File ("d:/parcAuto.xml"), opts);   }

<?xml version="1.0" encoding="UTF-8"?>  <parcautomobile>    <vehicule modele="coupe">          <marque>              <nom>Renault</nom>  

        </marque>  </vehicule></parcautomobile>

Page 33: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Parsing d’un fichier XML

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

// import des classes Java générées précédemment

public class ParseXML {            public static void main(String[] args) throws Exception        {          //Parsing du fichier XML          ParcautomobileDocument Parcautomobile = ParcautomobileDocument.Factory.parse(new File ("d:/parcAuto.xml")); Vehicule[] modelesVehicule = parc.getVehiculeArray(); 

   for (Vehicule v : modelesVehicule){                         System.out.println(v.getModele());                         Marque[] lesMarques =  v.getMarqueArray();                 for (Marque m : lesMarques){     

                         }   }}  

Page 34: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

L’interface XMLCursor

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

// import des classes Java générées précédemment

public class InsertionXML {            public static void main(String[] args) throws Exception        {

          ParcautomobileDocument Parcautomobile = ParcautomobileDocument.Factory.parse(new File ("d:/parcAuto.xml")); XmlCursor filtreCursor = Parcautomobile.newCursor();       filtreCursor.toNextToken();       filtreCursor.toNextToken();             filtreCursor.beginElement("vehicule");          filtreCursor.insertAttributeWithValue("modele", "sport");  

filtreCursor.dispose();}  

Page 35: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

XQuery

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

// import des classes Java générées précédemment

public class XQueryXML {            public static void main(String[] args) throws Exception        {

          ParcautomobileDocument Parcautomobile = ParcautomobileDocument.Factory.parse(new File ("d:/parcAuto.xml")); String requete = "for $e in //parcautomobile/vehicule/marque " +                   "where $e/nom = 'Peugeot' " +                   "return $e//nombre";                             XmlCursor monCursor1 = parcautomobile.newCursor().execQuery(requete);  System.out.println(monCursor1.getObject().toString());           System.out.println("Expression 1 : le nombre de vehicules de marque Peugeot est : " + monCursor1.getTextValue());  }  

<parcautomobile> <vehicule> <marque> <nom> Peugeot </nom> <nombre> 7 </nombre> </marque> </vehicule></parcautomobile>

Page 36: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

XPath

Chamilah Ingar - Guillaume Monteil - Cyril Tovena

// import des classes Java générées précédemment

public class XPathXML {            public static void main(String[] args) throws Exception        {

          ParcautomobileDocument Parcautomobile = ParcautomobileDocument.Factory.parse(new File ("d:/parcAuto.xml")); XmlCursor monCursor = Parcautomobile.newCursor();           monCursor.toFirstChild();            monCursor.selectPath("$this//marque//nom");                                     while (monCursor.toNextSelection()) {               

System.out.println(monCursor.getTextValue());               }  }  

<parcautomobile> <vehicule> <marque> <nom> Peugeot </nom> <nombre> 7 </nombre> </marque> </vehicule></parcautomobile>

Page 37: JEE Approfondi Chamilah Ingar - Guillaume Monteil - Cyril Tovena

Chamilah Ingar - Guillaume Monteil - Cyril Tovena