Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies...

32
Michel Coletta - Journées PATTERNS - 3-4 Avril 200 3 - Grenoble 1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Transcript of Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies...

Page 1: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

1

STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Page 2: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

2

Historique 2001: première version 1.0 2002: version 1.1 OpenSource Apache Jakarta Craig McClanahan

Apache Jserv Tomcat JSF, JSTL

2003:supporté par Tomcat, weblogic,...

Page 3: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

3

Application Web Protocole HTTP: requete/reponse, sans

état

Programmation coté serveur CGI java

Page 4: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

4

Technologies Java Servlets JSP TagLib Bean & EJB Conteneur web

Page 5: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

5

Exemple

Page 6: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

6

Servlet Pur Java, dérivation de ServletHttp

public class LookupServlet extends HttpServlet{ public void doGet( HttpServletRequest req,

HttpServletResponse res) throws ServletException, IOException{ res.setContentType("text/html"); PrintWriter out= res.getWriter();

String theme = req.getParameter("theme");String lieu = (theme.equalsIgnoreCase("PATTERN"))?

"Grenoble":"erreur";

out.println("<html>"); out.println("<body>");

Page 7: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

7

Servlet(2)

out.println("<img src=\"/webhello/images/di.gif\" width=\"100\" height=\"74\">");

out.println("<p>(servlet)<p>");out.println("<table bgcolor=\"#dedede\">

<tr><td><h2>lieu: "); out.println(lieu);

out.println("</h2></td></tr></table>"); out.println("</body></html>"); }}

Page 8: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

8

Servlet(3)

Page 9: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

9

JSP Html « enrichi »

<%@ page language="java" %>

<html> <head> <title>Struts Application</title> </head> <body> <% String theme = request.getParameter("theme"); String lieu = (theme.equals("PATTERN"))?"Grenoble":"erreur"; %>

Page 10: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

10

JSP(2)

Avantage: facile à développer

<img src="images/di.gif" width="100" height="74"> <p>

(jsp)<p><table bgcolor="#dedede"><tr><td>

<h2>lieu : <%=lieu%> </h2> </td></tr></table> </body></html>

Page 11: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

11

JSP(3)

Page 12: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

12

Tag Alternative aux scriptlets délicat à développer JSTL

Page 13: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

13

Bean Evite de mettre du code métier

dans les servlets ou JSP. Utilisation prévue dans JSP avec les

balises <jsp:usebean>

Page 14: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

14

Application Web modèle en couches

pré

sen

tatio

n

métie

r

persista

nce

Page 15: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

15

Architecture MVC modèle = contient la logique métier de

l ’application ainsi qu ’une représentation des données persistantes.

Vue = assure l ’interface avec l ’utilisateur(saisie et présentation des résultats).

Contrôleur = assure l ’enchainement des vues et les accés au modèle.

Core J2EE: pattern Front Controller.

Page 16: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

16

Core J2EE pattern catalog

Page 17: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

17

Front Controller http://java.sun.com/blueprints/corej2eepatterns/Patterns/

FrontController.html

Page 18: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

18

MVC & STRUTS Vue = page JSP + Tag + bean Controleur = servlet Modèle = bean,…

Page 19: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

19

Architecture de Struts

D ’après document de C. McClanahan

Page 20: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

20

Structure des répertoires Webappli

pages .jsp WEB-INF

web.xml struts-config.xml lib

fichiers .jar classes

fichiers .class

Page 21: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

21

Vue: Index.jsp<%@ page language="java" %><%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<html> <head> <title>Struts Application</title> </head> <body> <img src="images/di.gif" width="150" height="68">

<html:errors />

Page 22: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

22

Index.jsp (2) <html:form action="Lookup" name="lookupForm" type="wiley.LookupForm" > <table width="45%" border="0"> <tr> <td>journées thématiques:</td> <td><html:text property="theme" /></td> </tr> <tr> <td colspan="2" align="center"><html:submit /></td> </tr> </table> </html:form>

</body></html>

Page 23: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

23

Index.jsp(3) struts-html.tld web.xml

<taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location></taglib>

<welcome-file-list> <welcome-file>index.jsp</welcome-file></welcome-file-list>

Page 24: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

24

ActionForm: LookupForm.java public class LookupForm extends ActionForm {

private String theme = null;

public String getTheme() { return (theme); }

public void setTheme(String theme) { this.theme = theme; }

public void reset(ActionMapping mapping, HttpServletRequest request) { this.theme = null; }

Page 25: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

25

LookupForm.java(2)

déploiement dans WEB-INF/classes struts-config.xml

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

ActionErrors erreurs = new ActionErrors();if((theme == null) || (theme.length() == 0)){

erreurs.add("theme",new ActionError("erreurs.theme.required"));

}return erreurs;

}}

Page 26: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

26

struts-config.xml<struts-config> <form-beans> <form-bean name="lookupForm" type="wiley.LookupForm"/> </form-beans> <action-mappings> <action path="/Lookup" type="wiley.LookupAction" validate="true" input="/index.jsp" name="lookupForm" > <forward name="succes" path="/lieu.jsp"/> <forward name="echec" path="/index.jsp"/> </action> </action-mappings> <message-resources parameter="wiley.ApplicationResources"/></struts-config>

Page 27: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

27

Actionpublic class LookupAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

String lieu = null; String theme = null; String cible = new String("succes");

if ( form != null ) { LookupForm lookupForm = (LookupForm)form; theme = lookupForm.getTheme(); lieu = getLieu(theme); }

Page 28: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

28

Action(2)

if ( lieu == null ) { cible = new String("echec"); ActionErrors erreurs = new ActionErrors(); erreurs.add(ActionErrors.GLOBAL_ERROR, new ActionError("erreurs.theme.inconnu",theme)); if(!erreurs.empty()) saveErrors(request,erreurs); } else { request.setAttribute("LIEU", lieu); } return (mapping.findForward(cible)); }}

Page 29: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

29

Enseigner quoi ? Technologies Java patterns exemple simple projet alternative PHP:

http://phrame.itsd.ttu.edu/

Page 30: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

30

Biblio Site officiel:

http://jakarta.apache.org/struts/

livres:

Page 31: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

31

Annexepublic interface Action{

public String execute(HttpServletRequest req); }

public class ActionServlet extends HttpServlet { private HashMap actions; public void init(ServletConfig config) {

super.init(config); initActions();

} private void initActions() {

actions = new HashMap(); actions.put("main-menu", new Action1("menu.jsp")); actions.put("add", new action2("add.jsp")); // ... }

Page 32: Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble1 STRUTS Introduction Technologies Java Architecture Développement Enseigner quoi ?

Michel Coletta - Journées PATTERNS - 3-4 Avril 2003 - Grenoble

32

Annexe (2) public void service(HttpServletRequest req,

HttpServletResponse res) throws ServletException, IOException { String next;

Action cmd = lookupAction(req.getParameter("cmd")); next = cmd.execute(req); ActionToken.set(req);

RequestDispatcher rd = getServletContext().getRequestDispatcher(next);

rd.forward(req, res); } private Command lookupAction(String cmd){

return (Action)actions.get(cmd); }