Télécharger le diaporama de la présentation.

publicité
STRUTS





Introduction
Technologies Java
Architecture
Développement
Enseigner quoi ?
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
1
Historique




2001: première version 1.0
2002: version 1.1
OpenSource Apache Jakarta
Craig McClanahan




Apache Jserv
Tomcat
JSF, JSTL
Michel par
Coletta - Tomcat,
Journées
2003:supporté
weblogic,...
PATTERNS - 3-4 Avril 2003 Grenoble
2
Application Web




Protocole HTTP: requete/reponse, sans état
Programmation coté serveur
CGI
java
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
3
Technologies Java





Servlets
JSP
TagLib
Bean & EJB
Conteneur web
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
4
Exemple
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
5
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>");
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
6
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>");
}
}
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
7
Servlet(3)
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
8
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";
%>
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
9
JSP(2)
<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>
Avantage: facile à développer
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
10
JSP(3)
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
11
Tag



Alternative aux scriptlets
délicat à développer
JSTL
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
12
Bean


Evite de mettre du code métier dans les
servlets ou JSP.
Utilisation prévue dans JSP avec les
balises <jsp:usebean>
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
13
Application Web

modèle en couches
persistance
métier
présentation
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
14
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.
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
15
Core J2EE pattern catalog
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
16
Front Controller

http://java.sun.com/blueprints/corej2eepatterns/Patterns/FrontController.html
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
17
MVC & STRUTS



Vue = page JSP + Tag + bean
Controleur = servlet
Modèle = bean,…
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
18
Architecture de Struts
D ’après document de C. McClanahan
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
19
Structure des répertoires

Webappli
 pages .jsp
 WEB-INF
 web.xml
 struts-config.xml
 lib
 fichiers .jar
 classes
 fichiers .class
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
20
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 />
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
21
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>
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
22
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>
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
23
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;
}
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
24
LookupForm.java(2)
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;
}
}


déploiement dans WEB-INF/classes
struts-config.xml
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
25
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"/>
Michel Coletta - Journées
</struts-config>
PATTERNS - 3-4 Avril 2003 Grenoble
26
Action
public 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);
Michel Coletta - Journées
}
PATTERNS - 3-4 Avril 2003 Grenoble
27
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));
}
}
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
28
Enseigner quoi ?





Technologies Java
patterns
exemple simple
projet
alternative PHP:

http://phrame.itsd.ttu.edu/
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
29
Biblio

Site officiel:


http://jakarta.apache.org/struts/
livres:
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
30
Annexe
public 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"));
// ...
Michel Coletta - Journées
}
PATTERNS - 3-4 Avril 2003 Grenoble
31
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);
}
Michel Coletta - Journées
PATTERNS - 3-4 Avril 2003 Grenoble
32
Téléchargement