Outils pour utilisateurs

Outils du site


helloworld:web:java:taglib:jsp

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
helloworld:web:java:taglib:jsp [2015/08/13 11:21] – Création roothelloworld:web:java:taglib:jsp [2020/05/10 23:57] (Version actuelle) – Suppression de la taille par défaut pour les images root
Ligne 1: Ligne 1:
-Nécessite un projet de type [[eclipse:howto:dwp|Dynamic Web Project]].+Nécessite un projet de type [[ide:eclipse:projet|Dynamic Web Project]].
  
 +=====Cas général=====
 <file xml tagperso.tld> <file xml tagperso.tld>
 <?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
-<j2ee:taglib version="2.0" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee"+<javaee:taglib version="2.1" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
   xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "> +  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd "> 
-  <j2ee:tlib-version>2.0</j2ee:tlib-version> +  <javaee:tlib-version>2.1</javaee:tlib-version> 
-  <j2ee:short-name>tagperso</j2ee:short-name> +  <javaee:short-name>tagperso</javaee:short-name> 
-  <j2ee:tag> +  <javaee:tag> 
-    <j2ee:name>nomtag</j2ee:name> +    <javaee:name>nomtag</javaee:name> 
-    <j2ee:tag-class>com.llgc.TagPerso</j2ee:tag-class> +    <javaee:tag-class>com.llgc.TagPerso</javaee:tag-class> 
-    <j2ee:body-content>JSP</j2ee:body-content> +    <javaee:body-content>JSP</javaee:body-content> 
-    <j2ee:attribute> +    <javaee:attribute> 
-      <j2ee:name>attribute1</j2ee:name> +      <javaee:name>attribute1</javaee:name> 
-      <j2ee:required>true</j2ee:required> +      <javaee:required>true</javaee:required> 
-      <j2ee:rtexprvalue>true</j2ee:rtexprvalue> +      <javaee:rtexprvalue>true</javaee:rtexprvalue> 
-    </j2ee:attribute> +    </javaee:attribute> 
-    <j2ee:attribute> +    <javaee:attribute> 
-      <j2ee:name>attribute2</j2ee:name> +      <javaee:name>attribute2</javaee:name> 
-      <j2ee:rtexprvalue>true</j2ee:rtexprvalue> +      <javaee:rtexprvalue>true</javaee:rtexprvalue> 
-    </j2ee:attribute> +    </javaee:attribute> 
-  </j2ee:tag> +  </javaee:tag> 
-</j2ee:taglib>+</javaee:taglib>
 </file> </file>
-<note>''rtexprvalue'' permet l'utilisation des EL dans les attributs.</note>+ 
 +<WRAP center round info 60%> 
 +''rtexprvalue'' permet l'utilisation des EL dans les attributs. 
 +</WRAP> 
 <file java TagPerso.java> <file java TagPerso.java>
 package com.llgc; package com.llgc;
Ligne 97: Ligne 102:
 <file xml taglib.jsp> <file xml taglib.jsp>
 <%@ page language="java" contentType="text/html; charset=UTF-8" <%@ page language="java" contentType="text/html; charset=UTF-8"
-  pageEncoding="UTF-8+  pageEncoding="UTF-8"%>
-  import="java.util.List, com.llgc.BeanPersonne, com.llgc.Liste6"%>+
 <%@ taglib uri="WEB-INF/tagperso.tld" prefix="tag"%> <%@ taglib uri="WEB-INF/tagperso.tld" prefix="tag"%>
  
Ligne 120: Ligne 124:
 Le corps du message : Corps. Le corps du message : Corps.
 </code> </code>
 +
 +=====Ajout d'un validateur des attributs=====
 +La méthode ''isValid'' est exécutée avant la classe générant le code ''HTML'' et génère une exception (et donc fait échouer le chargement de la page) si elle renvoie ''false''.
 +
 +<file java TagPersoExtra.java>
 +package com.llgc;
 +
 +import java.util.Enumeration;
 +
 +import javax.servlet.jsp.tagext.TagData;
 +import javax.servlet.jsp.tagext.TagExtraInfo;
 +
 +public class TagPersoExtra extends TagExtraInfo
 +{
 +  @Override
 +  public boolean isValid (TagData tagData)
 +  {
 +    Enumeration <String> attr = tagData.getAttributes ();
 +    System.out.println ("B");
 +    while (attr.hasMoreElements ())
 +    {
 +      String it = attr.nextElement ();
 +      if (it.equals ("attribute1") && tagData.getAttributeString (it) == null)
 +      {
 +        return false;
 +      }
 +      System.out.println (it + " : " + tagData.getAttributeString (it));
 +    }
 +    return super.isValid (tagData);
 +  }
 +}
 +</file>
 +
 +<file xml tagperso.tld>
 +<?xml version="1.0" encoding="UTF-8"?>
 +<javaee:taglib version="2.1" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
 +  xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 +  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd ">
 +  <javaee:tlib-version>2.1</javaee:tlib-version>
 +  <javaee:short-name>tagperso</javaee:short-name>
 +  <javaee:tag>
 +    <javaee:name>nomtag</javaee:name>
 +    <javaee:tag-class>com.llgc.TagPerso</javaee:tag-class>
 +    <javaee:tei-class>com.llgc.TagPersoExtra</javaee:tei-class>
 +    …
 +  </javaee:tag>
 +</javaee:taglib>
 +</file>
 +
 +<WRAP center round important 60%>
 +Dans mon cas, pour que les modifications dans TagPersoExtra soient prises en compte, à chaque fois, il fallait que je réenregistre la page ''.JSP'', comme si le cache ne détectait pas la dépendance et n'actualisait pas la compilation de TagPersoExtra.
 +</WRAP>
 +
 +Rendu (si ''isValid'' renvoie faux) :
 +{{:helloworld:web:java:taglib:jsp:rendu_ex2_fail.png|Échec de la fonction ''isValid''}}
helloworld/web/java/taglib/jsp.1439457705.txt.gz · Dernière modification : 2015/08/13 11:21 de root