Skip to content

Instantly share code, notes, and snippets.

@sen0rxol0
Last active January 25, 2022 09:58
Show Gist options
  • Save sen0rxol0/c021f3861aaabf0be0e1b44965d8d517 to your computer and use it in GitHub Desktop.
Save sen0rxol0/c021f3861aaabf0be0e1b44965d8d517 to your computer and use it in GitHub Desktop.
La JSTL

JSTL (JavaServer Pages Standard Tag Library)

Liens utiles

JSTL JSR JSTL Core

SOMMAIRE

  • catch Catches any Throwable that occurs in its body and optionally exposes it.
  • choose Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>
  • if Simple conditional tag, which evalutes its body if the supplied condition is true and optionally exposes a Boolean scripting variable representing the evaluation of this condition import Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'.
  • forEach The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality
  • forTokens Iterates over tokens, separated by the supplied delimeters
  • out Like <%= ... >, but for expressions.
  • otherwise Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'
  • param Adds a parameter to a containing 'import' tag's URL.
  • redirect Redirects to a new URL.
  • remove Removes a scoped variable (from a particular scope, if specified).
  • set Sets the result of an expression evaluation in a 'scope'
  • url Creates a URL with optional query parameters.
  • when Subtag of <choose> that includes its body if its condition evalutes to 'true'

Utiliser la JSTL

Afin d'utiliser JSTL il suffit de copier le fichier .jar dans le répertoire /WEB-INF/lib/ de l'application

  <%-- Inclusion de la librairie dans la page .jsp --%>
  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  
  <%-- Affichage d'un attribut de la page --%>
  <c:out value="${valeur}" />
  
   <%-- Boucle un tableau --%>
  <c:forEach var="item" items="${itemsAttribut}"
    <c:out value="${item}" />
  </c:forEach>
  
  <%-- Assignation d'une variable dans le scope: application, request, session, page --%>
  <%-- La variable appartient au scope qu'est accessible via [scope]Scope: si scope = application alors applicationScope  --%>
  <c:set var="nomVariable" value="Valeur de la variable nomVariable" scope="page" />
  <c:out value="${pageScope.nomVariable}" />
  

Incluire taglib dans toutes les pages .jsp Modifier web.xml en ajoutant:

<!-- web.xml -->
<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <inlude-prelude>/WEB-INF/taglibs.jsp</inlude-prelude>
  </jsp-property-group>
</jsp-config>

Créer le fichier taglibs.jsp dans le répertoire /WEB-INF/

<%--   taglibs.jsp --%>
  <%-- Inclusion de la librairie dans toutes les pages .jsp --%>
  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment