Developpement Web - JSTL
Developpement Web - JSTL
Jean-Michel [email protected]
http://www.info.univ-angers.fr/pub/richer
M2 Informatique 2010-2011
1 / 44
Developpement Web - JSTL
Plan
Plan
1 Introduction
2 Utilisation de la JSTL
3 Bibliographie
2 / 44
Developpement Web - JSTL
Introduction
Utilisation de la JSTL
Objectifs
• comprendre le principe de JSTL
• decouvrir quelques aspects de JSTL
3 / 44
Developpement Web - JSTL
Introduction
Que signifie JSTL ?
Definition (JSTL)
JSP Standard Tag Library met a disposition du developpeurdes balises pour accomplir la plupart des taches qui doiventetre realisees avec les JSP.
En simplifie :
On remplace les balises et le code Java par du XML specifique.
Documentation• https://jstl.dev.java.net/
• http://tomcat.apache.org/taglibs/index.html
4 / 44
Developpement Web - JSTL
Introduction
Les versions de la JSTL
Actuellement 2 versions• JSTL 1.0 necessite (au minimum) un conteneur JSP 1.2
• JSTL 1.1 necessite (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
Developpement Web - JSTL
Introduction
Expression Language pour JSP 2.1
URL de base : http://java.sun.com
Librairie URI pr efixecore /jsp/jstl/core c
Format /jsp/jstl/fmt fmtXML /jsp/jstl/xml xSQL /jsp/jstl/sql sql
Functions /jsp/jstl/functions fn
http://java.sun.com/products/products/jsp/jstl/1.1/docs/tlddocs/index.html
6 / 44
Developpement 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
Developpement Web - JSTL
Introduction
JSTL - XML
XML : x• Core
• Flow control
• Transformation
8 / 44
Developpement Web - JSTL
Introduction
JSTL - i18n
Internationalization (i18n) : fmt
• Locale
• Message formatting
• Number and date formatting
9 / 44
Developpement Web - JSTL
Introduction
JSTL - SQL et Functions
SQL : sql
• Database
Functions : fn• Collection length
• String manipulation
10 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Utilisation de la JSTL
Utilisation de la JSTL
11 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Obtenir et installer la JSTL
JSTL pour Tomcat - etape 1
• Recuperer la vesion 1.1 :http://tomcat.apache.org/taglibs/standard/
• decompresser dans /opt• inclure dans le projet (Add external jars ) les fichiers :
jstl.jar et standard.jar du repertoire lib
12 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Obtenir et installer la JSTL
JSTL pour Tomcat - etape 2
• copier les fichier .jar dans le repertoire lib de Tomcatsudo 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 repertoire WEB-INF de l’application
• modifier le fichier web.xml pour ajouter des taglib
13 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Obtenir et installer la JSTL
JSTL pour Tomcat - etape 31 <taglib>2 <taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>3 <taglib-location>/WEB-INF/fmt.tld</taglib-location>4 </taglib>5 ....67 <taglib>8 <taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>9 <taglib-location>/WEB-INF/x-rt.tld</taglib-location>
10 </taglib>
14 / 44
Developpement Web - JSTL
Utilisation de la JSTL
JSTL Core
JSTL Core
15 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Utiliser JSTL Core
Utiliser JSTL Core
1 <%@ taglib2 prefix="c"3 uri="http://java.sun.com/jsp/jstl/core"4 %>
16 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Core - set / remove
Encodage URL - Exemple 1
1 <!-- set from parameter -->2 <c:set var="userId" scope="session" value="${param.fieldId}" />34 <c:set var="artistName" scope="page" value="Michael Jackson" />56 <!-- remove from session -->7 <c:remove var="userId" scope="session" />
17 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Core - URL
Encodage URL - Exemple 1
1 <!-- Script JSP -->2 <a href="<% response.encodeURL("page.jsp") %>">link</a>34 <!-- Equivalent JSTL Core -->5 <a href="<c:url value=’page.jsp’ />">link</a>6
18 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Core - URL
Encodage URL - Exemple 2
visualisation d’un produit enregistre 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>89 <!-- Equivalent JSTL Core -->
10 <a href="<c:url value=’view.jsp?pr id=${jspProduct.id}’ />">view</a>11
19 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Core - forEach
Core - forEach
permet de realiser une iteration sur les containers
20 / 44
Developpement 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
Developpement Web - JSTL
Utilisation de la JSTL
Core - forEach - Attributs
Core - forEach - Attributs
permettent une gestion plus fine de la boucle :
• begin indice de debut
• end indice de fin
• step increment
• varStatus informations sur la boucle (first, last, index,count)
22 / 44
Developpement 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);78 <!-- 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
Developpement Web - JSTL
Utilisation de la JSTL
Core - forTokens
Core - forTokens
permet de decomposer une chaine de caracteres en elementssepares par des delimiteurs comme java.util.StringTokenizer
• var variable qui recoit chacune des valeurs
• items chaine qui sera decomposee
• delims liste des delimiteurs sous forme de chaine
24 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Core - forTokens
parcours forTokens
decomposition et affichage de la liste des codes des produits :1 <!-- Code Servlet -->2 session.setAttribute("productCodes", "12,89,783");34 <-- Code JSP + forTokens-->5 <c:forTokens var="code" items="${productCodes}" delims=",">6 <li> ${code} </li>7 </c:forTokens >
25 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Core - if
Core - if
permet de realiser un test conditionnel
• test permet de decrire la condition
26 / 44
Developpement 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
Developpement Web - JSTL
Utilisation de la JSTL
Core - choose
Core - choose
sorte de switch / if
• when permet de decrire chaque condition
• otherwise correspond au default de Java
28 / 44
Developpement 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
Developpement Web - JSTL
Utilisation de la JSTL
Core - import
Core - import
• permet d’importer un autre fichier qui sera inclus dans lapage courante, agit commme le #include du langage C
• permet aussi de charger un fichier et de le stocker dansune variable
30 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Core - import
import
1 <c:import url="/includes/header.html" />23 <c:import url="/includes/footer.jsp" />45 <c:import url="/text message.txt" var="myText" />
31 / 44
Developpement Web - JSTL
Utilisation de la JSTL
JSTL XML
JSTL XML
32 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Utiliser JSTL XML
Utiliser JSTL XML
1 <%@ taglib2 prefix="xml"3 uri="http://java.sun.com/jsp/jstl/xml"4 %>
33 / 44
Developpement 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
Developpement Web - JSTL
Utilisation de la JSTL
XML - parse
parse
1 <c:import url="/persons.xml" var="personsXML" />23 <x:parse xml="${personsXML}" var="myDocument" />4 <ul>5 <x:forEach var="item" varStatus="status"6 select="$myDocument/persons/person">78 <li><x:out select="$item/first-name" /></li>9
10 </x:forEach >
11 </ul>12
35 / 44
Developpement Web - JSTL
Utilisation de la JSTL
XML - forEach
forEach
1 <c:import url="/persons.xml" var="personsXML" />2 <x:parse xml="${personsXML}" var="myDocument" />34 <ul>5 <x:forEach var="item" varStatus="status"6 select="$myDocument/persons/person/@id">78 <li><x:out select="$item" /></li>9
10 </x:forEach >
11 </ul>
36 / 44
Developpement Web - JSTL
Utilisation de la JSTL
XML - set
set
1 <c:import url="/persons.xml" var="personsXML" />2 <x:parse xml="${personsXML}" var="myDocument" />34 <x:set var="mickey"5 select="$myDocument/persons/person[@id=3]" />67 <x:out select="$mickey/first-name" />
37 / 44
Developpement Web - JSTL
Utilisation de la JSTL
JSTL SQL
JSTL SQL
38 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Utiliser JSTL SQL
Utiliser JSTL SQL
1 <%@ taglib2 prefix="sql"3 uri="http://java.sun.com/jsp/jstl/sql"4 %>
39 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Configuration de la DataSource
Configuration de la DataSource
mettre le fichier context.xml dans le repertoire META-INF1 <?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"/>1314 <!-- Specify the security realm and location of the users file15 <Realm className=”org.apache.catalina.realm.MemoryRealm”16 pathname=”/tomcat/webapps/ROOT/WEB-INF/users.xml” />17 -->18 </Context >
40 / 44
Developpement Web - JSTL
Utilisation de la JSTL
Configuration de Tomcat
Configuration de Tomcat
mettre le Connector/J
• dans le repertoire lib de Tomcat
• dans le repertoire WEB-INF/lib du projet
41 / 44
Developpement Web - JSTL
Utilisation de la JSTL
SQL - query
query
1 <sql:setDataSource dataSource="jdbcCommands"/>2 <sql:query var="customers" sql="select * from customer where 1" />34 <table>
5 <c:forEach var="columnName" items="${customers.columnNames}">6 <th><c:out value="${columnName}"/></th>7 </c:forEach >
89 <c:forEach var="row" items="${customers.rows}">
10 <tr>11 <c:forEach var="column" items="${row}">12 <td><c:out value="${column.value}"/></td>13 </c:forEach >
14 </tr>15 </c:forEach >
16 </table>
42 / 44
Developpement Web - JSTL
Bibliographie
Bibliographie
Bibliographie
43 / 44
Developpement Web - JSTL
Bibliographie
Bibliographie, sitographie
• Developpement 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
Top Related