Développement Web
Transcription
Développement Web
Développement Web - JSTL Développement Web - JSTL Jean-Michel Richer [email protected] http://www.info.univ-angers.fr/pub/richer M2 Informatique 2010-2011 1 / 44 Développement Web - JSTL Plan Plan 1 Introduction 2 Utilisation de la JSTL 3 Bibliographie 2 / 44 Développement Web - JSTL Introduction Utilisation de la JSTL Objectifs • comprendre le principe de JSTL • découvrir quelques aspects de JSTL 3 / 44 Développement Web - JSTL Introduction Que signifie JSTL ? Definition (JSTL) JSP Standard Tag Library met à disposition du développeur des balises pour accomplir la plupart des tâches qui doivent être réalisées avec les JSP. En simplifié : On remplace les balises et le code Java par du XML spécifique. Documentation • https://jstl.dev.java.net/ • http://tomcat.apache.org/taglibs/index.html 4 / 44 Développement Web - JSTL Introduction Les versions de la JSTL Actuellement 2 versions • JSTL 1.0 nécessite (au minimum) un conteneur JSP 1.2 • JSTL 1.1 nécessite (au minimum) un conteneur JSP 2.0 pour Tomcat • 3.x Servlet 2.2 et JSP 1.1 • 4.x Servlet 2.3 and JSP 1.2 • 5.5 Servlet 2.4 and JSP 2.0 • 6.x Servlet 2.5 and JSP 2.1 5 / 44 Développement Web - JSTL Introduction Expression Language pour JSP 2.1 URL de base : http://java.sun.com Librairie core Format XML SQL Functions URI /jsp/jstl/core /jsp/jstl/fmt /jsp/jstl/xml /jsp/jstl/sql /jsp/jstl/functions préfixe c fmt x sql fn http://java.sun.com/products/products/jsp/jstl/1.1/docs/tlddocs/index.html 6 / 44 Développement Web - JSTL Introduction JSTL - Core Core : c • Variable support : remove, set • Flow control : choose, forEach, forTokens, if • URL management : import, redirect, url • Miscellaneous : catch, out 7 / 44 Développement Web - JSTL Introduction JSTL - XML XML : x • Core • Flow control • Transformation 8 / 44 Développement Web - JSTL Introduction JSTL - i18n Internationalization (i18n) : fmt • Locale • Message formatting • Number and date formatting 9 / 44 Développement Web - JSTL Introduction JSTL - SQL et Functions SQL : sql • Database Functions : fn • Collection length • String manipulation 10 / 44 Développement Web - JSTL Utilisation de la JSTL Utilisation de la JSTL Utilisation de la JSTL 11 / 44 Développement Web - JSTL Utilisation de la JSTL Obtenir et installer la JSTL JSTL pour Tomcat - étape 1 • Récupérer la vesion 1.1 : http://tomcat.apache.org/taglibs/standard/ • décompresser dans /opt • inclure dans le projet (Add external jars) les fichiers : jstl.jar et standard.jar du répertoire lib 12 / 44 Développement Web - JSTL Utilisation de la JSTL Obtenir et installer la JSTL JSTL pour Tomcat - étape 2 • copier les fichier .jar dans le répertoire lib de Tomcat sudo cp /opt/jakarta-taglibs-standard-1.1.2/lib/*.jar /opt/apache-tomcat-6.0.26/lib/ • importer les fichier .tld de /opt/jakarta-taglibs-standard-1.1.2/tld/* dans le répertoire WEB-INF de l’application • modifier le fichier web.xml pour ajouter des taglib 13 / 44 Développement Web - JSTL Utilisation de la JSTL Obtenir et installer la JSTL JSTL pour Tomcat - étape 3 1 2 3 4 5 6 7 8 9 <taglib> <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri> <taglib-location>/WEB-INF/fmt.tld</taglib-location> </taglib> .... <taglib> <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri> <taglib-location>/WEB-INF/x-rt.tld</taglib-location> 10 </taglib> 14 / 44 Développement Web - JSTL Utilisation de la JSTL JSTL Core JSTL Core 15 / 44 Développement Web - JSTL Utilisation de la JSTL Utiliser JSTL Core Utiliser JSTL Core 1 <%@ taglib 2 prefix="c" 3 uri="http://java.sun.com/jsp/jstl/core" 4 %> 16 / 44 Développement Web - JSTL Utilisation de la JSTL Core - set / remove Encodage URL - Exemple 1 1 2 3 4 5 6 7 <!-- set from parameter --> <c:set var="userId" scope="session" value="${param.fieldId}" /> <c:set var="artistName" scope="page" value="Michael Jackson" /> <!-- remove from session --> <c:remove var="userId" scope="session" /> 17 / 44 Développement Web - JSTL Utilisation de la JSTL Core - URL Encodage URL - Exemple 1 1 2 3 4 5 6 <!-- Script JSP --> <a href="<% response.encodeURL("page.jsp") %>">link</a> <!-- Equivalent JSTL Core --> <a href="<c:url value=’page.jsp’ />">link</a> 18 / 44 Développement Web - JSTL Utilisation de la JSTL Core - URL Encodage URL - Exemple 2 visualisation d’un produit enregistré dans la session : 1 <!-- Script JSP --> 2 <%@ page import="com.commands.model.Product" %> 3 <% 4 Product p = (Product) session.getAttribute("jspProduct"); 5 String url = "view.jsp?pr id=" + p.getId(); 6 %> 7 <a href="<% response.encodeURL(url) %>">view</a> 8 9 <!-- Equivalent JSTL Core --> 10 <a href="<c:url value=’view.jsp?pr id=${jspProduct.id}’ />">view</a> 11 19 / 44 Développement Web - JSTL Utilisation de la JSTL Core - forEach Core - forEach permet de réaliser une itération sur les containers 20 / 44 Développement Web - JSTL Utilisation de la JSTL Core - forEach parcours forEach parcours de la liste des produits : 1 <c:forEach var="item" items="${jspCommand.products}"> 2 <tr> 3 <td> ${item.label} </td> 4 <td> ${item.price} </td> 5 <td> ${item.quantity} </td> 6 </tr> 7 </c:forEach> 21 / 44 Développement Web - JSTL Utilisation de la JSTL Core - forEach - Attributs Core - forEach - Attributs permettent une gestion plus fine de la boucle : • begin indice de début • end indice de fin • step incrément • varStatus informations sur la boucle (first, last, index, count) 22 / 44 Développement Web - JSTL Utilisation de la JSTL Core - forEach - Attributs parcours forEach - Attributs parcours d’une liste d’entiers 1 <!-- Code Servlet --> 2 int tab[]=new int[10]; 3 for (int i=0;i<tab.length;++i) { 4 tab[i]=i*i; 5 } 6 session.setAttribute("squares", tab); 7 8 <!-- Code JSP --> 9 <c:forEach var="n" items="${squares}" 10 begin="0" end="9" step="1" varStatus="status"> 11 <li> ${status.index} * ${status.index} = ${n} </li> 12 </c:forEach> 23 / 44 Développement Web - JSTL Utilisation de la JSTL Core - forTokens Core - forTokens permet de décomposer une chaine de caractères en éléments séparés par des délimiteurs comme java.util.StringTokenizer • var variable qui reçoit chacune des valeurs • items chaine qui sera décomposée • delims liste des délimiteurs sous forme de chaine 24 / 44 Développement Web - JSTL Utilisation de la JSTL Core - forTokens parcours forTokens décomposition et affichage de la liste des codes des produits : 1 <!-- Code Servlet --> 2 session.setAttribute("productCodes", "12,89,783"); 3 4 <-- Code JSP + forTokens--> 5 <c:forTokens var="code" items="${productCodes}" delims=","> 6 <li> ${code} </li> 7 </c:forTokens> 25 / 44 Développement Web - JSTL Utilisation de la JSTL Core - if Core - if permet de réaliser un test conditionnel • test permet de décrire la condition 26 / 44 Développement Web - JSTL Utilisation de la JSTL Core - if test conditionnel 1 <c:if test="${!empty person}"> 2 <c:if test="${person.lastName == ’Richer’}"> 3 <p>lastName is set correctly to : 4 <c:out value="${person.lastName}" /></p> 5 </c:if> 6 <c:if test="${person.salary > 3000.59}" > 7 <p>salary is set correctly to : 8 <c:out value="${person.salary}" /></p> 9 </c:if> 10 </c:if> 27 / 44 Développement Web - JSTL Utilisation de la JSTL Core - choose Core - choose sorte de switch / if • when permet de décrire chaque condition • otherwise correspond au default de Java 28 / 44 Développement Web - JSTL Utilisation de la JSTL Core - choose test choose 1 <c:choose> 2 <c:when test="${user.rights == 255 }" > 3 ... 4 </c:when> 5 <c:otherwise> 6 ... 7 </c:otherwise> 8 </c:choose> 29 / 44 Développement Web - JSTL Utilisation de la JSTL Core - import Core - import • permet d’importer un autre fichier qui sera inclus dans la page courante, agit commme le #include du langage C • permet aussi de charger un fichier et de le stocker dans une variable 30 / 44 Développement Web - JSTL Utilisation de la JSTL Core - import import 1 <c:import url="/includes/header.html" /> 2 3 <c:import url="/includes/footer.jsp" /> 4 5 <c:import url="/text message.txt" var="myText" /> 31 / 44 Développement Web - JSTL Utilisation de la JSTL JSTL XML JSTL XML 32 / 44 Développement Web - JSTL Utilisation de la JSTL Utiliser JSTL XML Utiliser JSTL XML 1 <%@ taglib 2 prefix="xml" 3 uri="http://java.sun.com/jsp/jstl/xml" 4 %> 33 / 44 Développement Web - JSTL Utilisation de la JSTL Fichier exemple Fichier exemple 1 <?xml version="1.0" encoding="UTF-8"?> 2 <persons> 3 <person id="1"> 4 <first-name>Donald</first-name> 5 <last-name>Duck</last-name> 6 <date-of-birth>01-01-1970</date-of-birth> 7 <salary>1000.0</salary> 8 </person> 9 <person id="2"> 10 <first-name>Picsou</first-name> 11 <last-name>Duck</last-name> 12 <date-of-birth>02-01-1960</date-of-birth> 13 <salary>120000.0</salary> 14 </person> 15 <person id="3"> 16 <first-name>Mickey</first-name> 17 <last-name>Mouse</last-name> 18 <date-of-birth>01-01-1980</date-of-birth> 19 <salary>3000.20</salary> 20 </person> 21 </persons> 34 / 44 Développement Web - JSTL Utilisation de la JSTL XML - parse parse 1 2 3 4 5 6 7 8 9 10 11 12 <c:import url="/persons.xml" var="personsXML" /> <x:parse xml="${personsXML}" var="myDocument" <ul> <x:forEach var="item" varStatus="status" select="$myDocument/persons/person"> /> <li><x:out select="$item/first-name" /></li> </x:forEach> </ul> 35 / 44 Développement Web - JSTL Utilisation de la JSTL XML - forEach forEach 1 2 3 4 5 6 7 8 9 10 11 <c:import url="/persons.xml" var="personsXML" /> <x:parse xml="${personsXML}" var="myDocument" /> <ul> <x:forEach var="item" varStatus="status" select="$myDocument/persons/person/@id"> <li><x:out select="$item" /></li> </x:forEach> </ul> 36 / 44 Développement Web - JSTL Utilisation de la JSTL XML - set set 1 2 3 4 5 6 7 <c:import url="/persons.xml" var="personsXML" /> <x:parse xml="${personsXML}" var="myDocument" /> <x:set var="mickey" select="$myDocument/persons/person[@id=3]" /> <x:out select="$mickey/first-name" /> 37 / 44 Développement Web - JSTL Utilisation de la JSTL JSTL SQL JSTL SQL 38 / 44 Développement Web - JSTL Utilisation de la JSTL Utiliser JSTL SQL Utiliser JSTL SQL 1 <%@ taglib 2 prefix="sql" 3 uri="http://java.sun.com/jsp/jstl/sql" 4 %> 39 / 44 Développement Web - JSTL Utilisation de la JSTL Configuration de la DataSource Configuration de la DataSource mettre le fichier context.xml dans le répertoire META-INF 1 <?xml version="1.0" encoding="UTF-8"?> 2 <Context> 3 <!-- Specify a JDBC datasource --> 4 <Resource name="jdbcCommands" 5 auth="Container" 6 type="javax.sql.DataSource" 7 username="commuser" 8 password="commpass" 9 driverClassName="com.mysql.jdbc.Driver" 10 url="jdbc:mysql://localhost:3306/commands?autoReconnect=true" 11 maxActive="10" 12 maxIdle="4"/> 13 14 <!-- Specify the security realm and location of the users file 15 <Realm className=”org.apache.catalina.realm.MemoryRealm” 16 pathname=”/tomcat/webapps/ROOT/WEB-INF/users.xml” /> 17 --> 18 </Context> 40 / 44 Développement Web - JSTL Utilisation de la JSTL Configuration de Tomcat Configuration de Tomcat mettre le Connector/J • dans le répertoire lib de Tomcat • dans le répertoire WEB-INF/lib du projet 41 / 44 Développement Web - JSTL Utilisation de la JSTL SQL - query query 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <sql:setDataSource dataSource="jdbcCommands"/> <sql:query var="customers" sql="select * from customer where 1" /> <table> <c:forEach var="columnName" items="${customers.columnNames}"> <th><c:out value="${columnName}"/></th> </c:forEach> <c:forEach var="row" items="${customers.rows}"> <tr> <c:forEach var="column" items="${row}"> <td><c:out value="${column.value}"/></td> </c:forEach> </tr> </c:forEach> </table> 42 / 44 Développement Web - JSTL Bibliographie Bibliographie Bibliographie 43 / 44 Développement Web - JSTL Bibliographie Bibliographie, sitographie • Développement Web avec J2EE, O’ Reilly, Eric Sarrion, Paris, 2005, ISBN 2-35402-140-2 • Java Servlets and JSP, Joel Murach, Andrea Steelman, Murach, 2nd Edition, 2008, ISBN 9781890774448 44 / 44