1 / 57

Formation Struts

Formation Struts. (Struts 1.3.8) ‏. www.objis.com - Formation STRUTS. 1. A propos d’Objis…. Centre de formation depuis 2005 Spécialiste technologies Java/J2ee Formations intra/inter entreprises Théorie (30%) et pratique (70%) Paris – Lyon – Dakar www.objis.com.

noleta
Télécharger la présentation

Formation Struts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Formation Struts (Struts 1.3.8)‏ www.objis.com - Formation STRUTS 1

  2. www.objis.com - Formation SPRING A propos d’Objis… • Centre de formation depuis 2005 • Spécialiste technologies Java/J2ee • Formations intra/inter entreprises • Théorie (30%) et pratique (70%) • Paris – Lyon – Dakar • www.objis.com www.objis.com - Formation STRUTS 2

  3. www.objis.com - Formation SPRING Formateur : Douglas Mbiandou • Ingénieur INSA Lyon (2000) • 8 ans d'expériences projets SI • Architecte / Formateur Java • Directeur technique Objis (www.objis.com) • Président Club Solidarité Numérique • www.club-solidarite-numerique.org www.objis.com - Formation STRUTS 3

  4. www.objis.com - Formation SPRING SOMMAIRE (3j)‏ • Rappels J2EE • Historique, présentation, positionnement • Configuration struts : struts-config.xml • Actions et formulaires • Bibliothèque de balise Struts • Validation de formulaires : Validator • Gestion d'erreurs • Industrialisation d'écrans avec Tiles • Application multi modules • Intégration avec Hibernate • Intégration avec EJB • Conclusion – ressources www.objis.com - Formation STRUTS 4

  5. www.objis.com - Formation SPRING Rappels J2EE • Architecture • Livraisons • Services • Modularité www.objis.com - Formation STRUTS 5

  6. www.objis.com - Formation SPRING Architecture J2EE www.objis.com - Formation STRUTS 6

  7. www.objis.com - Formation SPRING Livraisons J2EE www.objis.com - Formation STRUTS 7

  8. www.objis.com - Formation SPRING Services J2EE • JNDI : Java Naming Directory Interface • JCA : Java Connector Architecture • JTA : Java Transaction API • JPA : Java Persistence API • JMS : Java Message Service • JAAS : Java Authentication & Authorisation Service • JAX-WS : Java API for XML Web Services • JAXB : Java API for XML Binding www.objis.com - Formation STRUTS 8

  9. www.objis.com - Formation SPRING Architecture multicouche www.objis.com - Formation STRUTS 9

  10. www.objis.com - Formation SPRING Introduction STRUTS • Historique • Pattern MVC2 • Cycle de vie requête HTTP • Avantages / Inconvénients • Concurrents www.objis.com - Formation STRUTS 10

  11. www.objis.com - Formation SPRING Historique Struts • Initiative du projet Jakarta (jakarta.apache.org) • MVC system for JSP technology • Juillet 2001: Struts 1.0 • Craig Mac Clanahan • Struts n'est pas une spécification www.objis.com - Formation STRUTS 11

  12. www.objis.com - Formation SPRING Modèle – Vue – Controleur (MVC)‏ www.objis.com - Formation STRUTS 12

  13. www.objis.com - Formation SPRING Modèle – Vue – Controleur (MVC)‏ www.objis.com - Formation STRUTS 13

  14. www.objis.com - Formation SPRING MVC avec Struts • Vue = JSP, HTML, XML... • Controleur = servlet ActionServlet • Modèle (métier) = JavaBean, EJB, Web service... www.objis.com - Formation STRUTS 14

  15. www.objis.com - Formation SPRING Modèle – Vue – Controleur (MVC)‏ www.objis.com - Formation STRUTS 15

  16. www.objis.com - Formation SPRING Cycle de vie d'une requête www.objis.com - Formation STRUTS 16

  17. www.objis.com - Formation SPRING Avantages Struts • Gère les formulaires et actions de l'utilisateur • Réduit la répétition de code, • Augmente flexibilité et fiabilité, • Réduit le couplage entre composants. • Framework de présentation le plus réputé. • Nombreux développeurs Struts. www.objis.com - Formation STRUTS 17

  18. www.objis.com - Formation SPRING Inconvénients Struts • Utilisable uniquement env web : servlets / jsp • param HttpServletRequest, HttpServletResponse • Pas de mécanisme standard d'interception des actions. • gestion Sécurité, Logging, accès DB... • Séparation entre action et formulaire peu justifiée. • Augmente nb de classes à écrire • Absence de mécanisme d'accès à une couche métier www.objis.com - Formation STRUTS 18

  19. www.objis.com - Formation SPRING Concurents Struts • Spring MVC ( Conteneur léger, Injection dép.) • AJAX (Javascript + XML asynchrone) • WebWork / Struts 2 • JSF (Java Server Faces) • ‏Tapestry www.objis.com - Formation STRUTS 19

  20. www.objis.com - Formation SPRING Composantes et outils Struts • Controleur • Les 5 sections de struts-config.xml • Bean Formulaire & DynaForm • Action Struts • Bibliothèque de balises • Accès aux données JDBC • Validation de formulaire : Validator • Création d'écrans avec Tiles 20

  21. www.objis.com - Formation SPRING Le Controleur • Servlet org.apache.struts.action.ActionServlet (présent dans struts.jar)‏ • Configuré dans web.xml et struts-config.xml • Gère toutes les requêtes qui respectent un certains format à spécifier (ex : *.do)‏ • Assure redirection vers autre action ou bien vue • Appelé à travers URL / lien HTML www.objis.com - Formation STRUTS 21

  22. www.objis.com - Formation SPRING Controleur : configuration web.xml configuration via struts-config.xml gestion par controlleur URLs type http://serveur/page.do 22

  23. Controleur : configuration struts-config.xml www.objis.com - Formation SPRING Liste beans formulaire (associés à une jsp)‏ mapping URL / Action Struts Textes, multilinguisme

  24. www.objis.com - Formation SPRING Les 5 sections de struts-config.xml • <form-beans> : Liste des formulaires Struts • <action-mappings> : Partie la plus importante. permet de lier une URL à une action. • <message-ressources> : fichier de propriété • <global-exeptions> : gestion des exceptions • <global-forwards> : donne des noms génériques aux pages jsp. ces noms peuvent être utilisés depuis n'importe quelle action struts. www.objis.com - Formation STRUTS 24

  25. www.objis.com - Formation SPRING Bean formulaire (<form-beans>)‏ Page web et bean form associé 1 champ -->1 variable 25

  26. www.objis.com - Formation SPRING DynaActionForm (<form-beans>)‏ • Extension de FormBean • Plus besoin de coder le bean 'à la main' ! • FormBean généré dynamiquement par Struts • Configuration dans struts-config.xml • Utilisation directe dans l'action www.objis.com - Formation STRUTS 26

  27. www.objis.com - Formation SPRING DynaActionForm (<form-beans>)‏ struts-config.xml HelloAction.java www.objis.com - Formation STRUTS 27

  28. www.objis.com - Formation SPRING Action Struts • Objectif : effectuer traitement (généralement à travers instanciation d'une couche métier bean/EJB) puis renvoyer l'utilisateur vers nouvelle page, représentée par ActionForward. • Hérite de org.apache.struts.action.Action • méthode execute() à 4 paramètres • mapping : permet de retrouver les vues décrites dans <forward> de struts-config.xml . Ex : mapping.findFormard (“error”). • form: formulaire struts décrit (form-bean) dans struts-config.xml • request/response : objets HttpServletRequest, HttpservletResponse 28

  29. www.objis.com - Formation SPRING Action Struts : Exemple Codage de l'action Configuration de l'action dans struts-config.xml www.objis.com - Formation STRUTS 29

  30. Récupération d'objet dans une Action Struts www.objis.com - Formation SPRING • Pour travailler avec un objet à partir d'une action Struts, nous le crééons (en dur) avec les techniques suivantes • New() • JNDI Lookup • INCONVENIENT : cela créée un couplage (statique) fort entre l'objet demandeur et l'objet apellé. • Application : Accès couches services et DAO (Data Access Object) • SOLUTION : injection de dépendances (ex : Spring)‏ www.objis.com - Formation STRUTS 30

  31. www.objis.com - Formation SPRING Bibliothèque de balises • Objectif : aider le développeur à créer des Vues JSP • 5 librairies • Bean : manipuler javabean dans JSP • HTML : d'excellente qualité, intuitive. Créer formulaires. • Logic : boucles et branchement conditionnels • Nested : Imbrication. Utile pour les arbres d'objets. • Tiles : Inclusion et paramétrage de fragment tiles. • Détronés progressivement par tags JSTL/EL www.objis.com - Formation STRUTS 31

  32. Bibliothèque de balises www.objis.com - Formation SPRING Struts 1.3.8 32

  33. www.objis.com - Formation SPRING Validation coté serveur : exemple 33

  34. Validation coté serveur : méthodologie www.objis.com - Formation SPRING • Il faut redéfinir la méthode validate(ActionMapping mapping,HttpServletRequest request) de l'ActionForm et y implémenter les validations. • Si aucune erreur n'est constatée, la méthode retourne null ou une instance de ActionErrors de taille 0. • Si une erreur est constatée, une instance de ActionErrors contenant une ou plusieurs instances de ActionMessage (ActionError pour d'anciennes versions de Struts) est retournée. Les messages d'erreurs sont alors affichés grâce à la balise <html:errors/> www.objis.com - Formation STRUTS 34

  35. Validation coté serveur : mise en oeuvre www.objis.com - Formation SPRING AdressForm.java MessageRessource.properties 35

  36. www.objis.com - Formation SPRING Validator : présentation • Technologie de validation de formulaire Struts coté client ou Serveur. Souvent utilisé coté client. • Beaucoup de code javascript déja prêt et implémenté dans des classes Jaa (voir validation-rules.xml ). Messages d'erreurs pour • Champs email, entier, chaine, carte de crédit,... • Validation coté client (contraitement à validation coté serveur via validate() du bean formulaire) • Plug-in Struts intégré (balise <plug-in> dans struts-config.xml)‏ www.objis.com - Formation STRUTS 36

  37. www.objis.com - Formation SPRING Validation coté client 37

  38. Validation coté client : mise en oeuvre www.objis.com - Formation SPRING struts-config.xml validation.xml MessageRessource.properties 38

  39. www.objis.com - Formation SPRING Validation client : méthodologie • 1 : Installer Plug-in validator dans struts-config.xml. • 2 : Configurer et ajouter les messages d'erreurs à afficher. • 3 : Créer et mettre à jour \WEB-INF\validation.xml avec règles. Attention si pb langue : <formset language=”fr”> • 4 : Inclure <html:javascript formName=”NomFormbean”/> dans JSP ,juste avant la fermeture du formulaire (</html:form>) • 5 : Ajouter onsubmit=”return validateNomFormbean(this);”lors de la déclaration du formulaire Struts www.objis.com - Formation STRUTS 39

  40. www.objis.com - Formation SPRING Tiles : présentation • Technologie de création de vues (MVC) • Création de vues par héritage (extends) • Nommer les pages au lieu de donner leur chemin complet • Réutiliser des composants de présentation (Ex : Internationalisation)‏ www.objis.com - Formation STRUTS 40

  41. www.objis.com - Formation SPRING mise en oeuvre Tiles : méthode • 1 : Ajout du <plug-in> Tiles dans struts-config.xml + vérif présence de struts-tiles.jar dans \WEB-INF\lib + config web.xml • 2 : Créer le fichier modèle : template.jsp (“header”, “body”, “left”,...) • 3 : Créer fichier \WEB-INF\tiles-def.xml avec une première 'definition'. Ex : definition “accueil” basée sur le fichier modèle (<tiles:insert page='template.jsp'> avec des <tiles:put> de header.jsp, body.jsp...) • 4 : Créez d'autres vues héritant de la vue “accueil” par héritage. • Ex la vue index (<definition name=”contact” extends=”accueil”>) • 5 : Modifier les <forward> dans struts-config.xml avec noms de vues Tiles . Ex : <action path=”/accueil” forward=”accueil” > • 6 : Packager et Tester l'application www.objis.com - Formation STRUTS 41

  42. www.objis.com - Formation SPRING Tiles : mise en oeuvre struts-config.xml web.xml :init-param du controleur JSP www.objis.com - Formation STRUTS 42

  43. Exemple tiles-def.xml www.objis.com - Formation SPRING www.objis.com - Formation STRUTS 43

  44. www.objis.com - Formation SPRING Exemple template.jsp www.objis.com - Formation STRUTS 44

  45. www.objis.com - Formation SPRING Exemple vue tiles www.objis.com - Formation STRUTS 45

  46. www.objis.com - Formation SPRING Accès aux données : DataSource www.objis.com - Formation STRUTS 46

  47. DataSource : configuration avec struts 1.2.9 www.objis.com - Formation SPRING Conseil : Utiliser/compléter l'exemple commenté présent dans struts-blank.war www.objis.com - Formation STRUTS 47

  48. DataSource : récupération avec struts 1.2.9 www.objis.com - Formation SPRING www.objis.com - Formation STRUTS 48

  49. DataSource : configuration avec struts 1.3.8 www.objis.com - Formation SPRING context.xml web.xml 49

  50. DataSource : récupération avec struts 1.3.8 www.objis.com - Formation SPRING Accès à la base de données via JNDI Lookup www.objis.com - Formation STRUTS 50

More Related