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

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
helloworld:web:java:taglib:jsp [2015/08/13 19:12] – Ajout de l'exemple 2 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]].
- +
-=====JSP pur=====+
  
 +=====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"?>
Ligne 26: Ligne 25:
 </javaee: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 122: Ligne 125:
 </code> </code>
  
-=====JSP/JSF===== +=====Ajout d'un validateur des attributs===== 
-Sur le principe, il ne faut pas mélanger les JSP et JSF mais j'ai mis trop de temps à faire marcher cet exemple pour le perdre.+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''.
  
-Il faut un projet dynamique avec le support des JSF mais la page est générée depuis une JSP. +<file java TagPersoExtra.java>
- +
-<file xml faces-config.xml> +
-<?xml version="1.0" encoding="UTF-8"?> +
-<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee" +
-  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +
-  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd" +
-  version="2.2"> +
-  <component> +
-    <description>Une partie du rendu de ce composant est délégué à un Renderer</description> +
-    <display-name>Composant Heure Courante avec Renderer spécifique</display-name> +
-    <component-type>monHtmlHeureCourante</component-type> +
-    <component-class>com.llgc.TagHeureHtml</component-class> +
-  </component> +
-  <render-kit> +
-    <renderer> +
-      <display-name>Heure courante avec Renderer</display-name> +
-      <component-family>heureCourante</component-family> +
-      <renderer-type>heureAvecRenderer</renderer-type> +
-      <renderer-class>com.llgc.TagHeureRenderer</renderer-class> +
-    </renderer> +
-  </render-kit> +
-</faces-config> +
-</file> +
- +
-<file java TagHeure.java>+
 package com.llgc; package com.llgc;
  
-import javax.faces.component.UIComponent; +import java.util.Enumeration;
-import javax.faces.webapp.UIComponentELTag;+
  
-// Le nom de cette classe correspond avec celui défini dans le fichier tld, rubrique tag-class+import javax.servlet.jsp.tagext.TagData; 
-public class TagHeure extends UIComponentELTag +import javax.servlet.jsp.tagext.TagExtraInfo;
-+
-  private String label;+
  
-  public String getLabel () +public class TagPersoExtra extends TagExtraInfo
-  { +
-    return label; +
-  } +
- +
-  public void setLabel (String label) +
-  { +
-    this.label = label; +
-  } +
- +
-  @Override +
-  public String getComponentType () +
-  { +
-    // Le nom du type du composant correspond avec celui défini dans le fichier faces-config.xml, +
-    // rubrique component|component-type +
-    return "monHtmlHeureCourante"; +
-  } +
- +
-  @Override +
-  public String getRendererType () +
-  { +
-    // Le nom du rendu du composant correspond avec celui défini dans le fichier faces-config.xml, +
-    // rubrique render-kit|renderer|renderer-type +
-    return "heureAvecRenderer"; +
-  } +
- +
-  @Override +
-  protected void setProperties (UIComponent composant) +
-  { +
-    composant.getAttributes().put("label", label); +
-    super.setProperties (composant); +
-  } +
- +
-+
-</file> +
- +
-<file java TagHeureRenderer.java> +
-package com.llgc; +
- +
-import java.io.IOException; +
-import java.util.Calendar; +
- +
-import javax.faces.component.UIComponent; +
-import javax.faces.context.FacesContext; +
-import javax.faces.context.ResponseWriter; +
-import javax.faces.render.Renderer; +
- +
-public class TagHeureRenderer extends Renderer+
 { {
   @Override   @Override
-  public void encodeBegin (FacesContext context, UIComponent componentthrows IOException+  public boolean isValid (TagData tagData)
   {   {
-    Calendar calendrier = Calendar.getInstance (); +    Enumeration <String> attr tagData.getAttributes (); 
-    String intitule (String) component.getAttributes ().get ("label"); +    System.out.println ("B"); 
-    String heureCourante = calendrier.get (Calendar.HOUR) + ":" + calendrier.get (Calendar.MINUTE) + ":" +    while (attr.hasMoreElements ())
-        + calendrier.get (Calendar.SECOND); +
-    ResponseWriter out = context.getResponseWriter (); +
-    out.write ("<table border=\"1\"><tr bgcolor=\"yellow\"><td><font color=\"red\">" + intitule + ":" + heureCourante +
-        + "</font></td></tr></table>"); +
-    super.encodeBegin (context, component); +
-  } +
-+
-</file> +
- +
-<file java TagPerso.java> +
-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 (); +      String it attr.nextElement (); 
-      out.println (".");+      if (it.equals ("attribute1") && tagData.getAttributeString (it) == null) 
 +      { 
 +        return false; 
 +      } 
 +      System.out.println (it + + tagData.getAttributeString (it));
     }     }
-    catch (IOException e) +    return super.isValid (tagData);
-    { +
-      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;+
   }   }
 } }
 </file> </file>
  
-<file xml taglib2.jsp+<file xml tagperso.tld
-<%@ page language="javacontentType="text/html; charset=UTF-8" +<?xml version="1.0encoding="UTF-8"?
-  pageEncoding="UTF-8"%+<javaee:taglib version="2.1xmlns:javaee="http://java.sun.com/xml/ns/javaee" 
-<%@ taglib uri="WEB-INF/tagperso2.tldprefix="tag"%> +  xmlns:xml="http://www.w3.org/XML/1998/namespacexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
-<%@ taglib uri="http://java.sun.com/jsf/coreprefix="f"%> +  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
-<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> +  <javaee:short-name>tagperso</javaee:short-name
-<html> +  <javaee:tag
-<head> +    <javaee:name>nomtag</javaee:name
-<meta http-equiv="Content-Type" content="text/html; charset=UTF-8/+    <javaee:tag-class>com.llgc.TagPerso</javaee:tag-class
-<title>Insert title here</title+    <javaee:tei-class>com.llgc.TagPersoExtra</javaee:tei-class
-</head> +    … 
-<body+  </javaee:tag
-  <f:view+</javaee:taglib>
-    <p> +
-      <tag:heure label="test" /> +
-    </p+
-  </f:view+
-</body+
-</html>+
 </file> </file>
  
-Rendu : +<WRAP center round important 60%> 
-{{:helloworld:web:java:taglib:jsp:rendu_ex2.png?85x30|Rendu exemple 2}}+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.1439485973.txt.gz · Dernière modification : 2015/08/13 19:12 de root