Nécessite un projet de type [[ide:eclipse:projet|Dynamic Web Project]]. =====Cas général===== 2.1 tagperso nomtag com.llgc.TagPerso JSP attribute1 true true attribute2 true ''rtexprvalue'' permet l'utilisation des EL dans les attributs. package com.llgc; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; @SuppressWarnings ("serial") public class TagPerso extends TagSupport { private String attribute1; private String attribute2; public String getAttribute1 () { return attribute1; } public void setAttribute1 (String attribute1) { this.attribute1 = attribute1; } public String getAttribute2 () { return attribute2; } public void setAttribute2 (String attribute2) { this.attribute2 = attribute2; } @Override public int doEndTag () throws JspException { try { JspWriter out = pageContext.getOut (); out.println ("."); } catch (IOException e) { e.printStackTrace (); throw new JspException (e); } return EVAL_PAGE; } @Override public int doStartTag () throws JspException { JspWriter out = pageContext.getOut (); try { out.println ("Bonjour, les attributs valent " + attribute1 + " et " + attribute2 + ".
"); out.println ("Le corps du message : "); } catch (IOException e) { e.printStackTrace (); throw new JspException (e); } return EVAL_BODY_INCLUDE; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="WEB-INF/tagperso.tld" prefix="tag"%> Insert title here

Corps

Rendu : Bonjour, les attributs valent attr1 et null. Le corps du message : Corps. =====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''. 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 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); } } 2.1 tagperso nomtag com.llgc.TagPerso com.llgc.TagPersoExtra 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. Rendu (si ''isValid'' renvoie faux) : {{:helloworld:web:java:taglib:jsp:rendu_ex2_fail.png|Échec de la fonction ''isValid''}}