Nécessite un projet de type Dynamic Web Project.
<?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:body-content>JSP</javaee:body-content> <javaee:attribute> <javaee:name>attribute1</javaee:name> <javaee:required>true</javaee:required> <javaee:rtexprvalue>true</javaee:rtexprvalue> </javaee:attribute> <javaee:attribute> <javaee:name>attribute2</javaee:name> <javaee:rtexprvalue>true</javaee:rtexprvalue> </javaee:attribute> </javaee:tag> </javaee:taglib>
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 + ".<br />"); 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"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <p> <tag:nomtag attribute1="attr1">Corps</tag:nomtag> </p> </body> </html>
Rendu :
Bonjour, les attributs valent attr1 et null. Le corps du message : Corps.
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 <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); } }
<?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>
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.