Outils pour utilisateurs

Outils du site


helloworld:web:java:taglib:jsf

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:jsf [2015/08/13 22:56] – Création roothelloworld:web:java:taglib:jsf [2020/05/10 23:56] (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]] avec les ''JSF'' activés. +Nécessite un projet de type [[ide:eclipse:projet|Dynamic Web Project]] avec les ''JSF'' activés.
- +
-<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; +
- +
-import javax.faces.component.UIComponent; +
-import javax.faces.webapp.UIComponentELTag; +
- +
-// Le nom de cette classe correspond avec celui défini dans le fichier tld, rubrique tag-class. +
-public class TagHeure extends UIComponentELTag +
-+
-  private String label; +
- +
-  public String getLabel () +
-  { +
-    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>+
  
 +=====component + renderer=====
 <file java TagHeureRenderer.java> <file java TagHeureRenderer.java>
 package com.llgc; package com.llgc;
Ligne 82: Ligne 13:
 import javax.faces.render.Renderer; import javax.faces.render.Renderer;
  
 +@FacesRenderer (componentFamily="com.llgc.heureCourante", rendererType="com.llgc.heureAvecRenderer")
 public class TagHeureRenderer extends Renderer public class TagHeureRenderer extends Renderer
 { {
Ligne 108: Ligne 40:
 import javax.faces.context.ResponseWriter; import javax.faces.context.ResponseWriter;
  
 +@FacesComponent ("com.llgc.monHtmlHeureCourante")
 public class TagHeureHtml extends HtmlOutputText public class TagHeureHtml extends HtmlOutputText
 { {
Ligne 114: Ligne 47:
   public String getFamily ()   public String getFamily ()
   {   {
-    // Le nom du rendu du composant correspond avec celui défini dans le fichier faces-config.xml, + 
-    // rubrique render-kit|renderer|component-family +    return "com.llgc.heureCourante";
-    return "heureCourante";+
   }   }
  
Ligne 123: Ligne 55:
   {   {
     ResponseWriter out = context.getResponseWriter ();     ResponseWriter out = context.getResponseWriter ();
-    out.write ("</b>");+    out.write ("<br />");
     super.encodeEnd (context);     super.encodeEnd (context);
   }   }
Ligne 137: Ligne 69:
   <context-param>   <context-param>
     <param-name>javax.faces.FACELETS_LIBRARIES</param-name>     <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
-    <param-value>/WEB-INF/tagheure.taglib.xml</param-value>+    <param-value>/META-INF/tagheure.taglib.xml</param-value>
   </context-param>   </context-param>
 </web-app> </web-app>
Ligne 152: Ligne 84:
     <javaee:tag-name>heure</javaee:tag-name>     <javaee:tag-name>heure</javaee:tag-name>
     <javaee:component>     <javaee:component>
-      <javaee:component-type>monHtmlHeureCourante</javaee:component-type> +      <javaee:component-type>com.llgc.monHtmlHeureCourante</javaee:component-type> 
-      <javaee:renderer-type>heureAvecRenderer</javaee:renderer-type>+      <javaee:renderer-type>com.llgc.heureAvecRenderer</javaee:renderer-type>
     </javaee:component>     </javaee:component>
 +    <javaee:attribute>
 +      <javaee:name>label</javaee:name>
 +      <javaee:required>true</javaee:required>
 +    </javaee:attribute>
   </javaee:tag>   </javaee:tag>
 </javaee:facelet-taglib> </javaee:facelet-taglib>
Ligne 172: Ligne 108:
   </h:head>   </h:head>
   <h:body>   <h:body>
-    <p> +    <tag:heure label="test" />
-      <tag:heure label="test" /+
-    </p>+
   </h:body>   </h:body>
 </f:view> </f:view>
Ligne 181: Ligne 115:
  
 Rendu : Rendu :
-{{:helloworld:web:java:taglib:jsp:rendu_ex2.png?85x30|Rendu exemple 2}}+{{:helloworld:web:java:taglib:jsp:rendu_ex2.png|Rendu exemple 2}} 
 + 
 +=====Source XHTML===== 
 +Volé (et actualisé) depuis [[http://www.mkyong.com/jsf2/custom-tags-in-jsf-2-0/|Custom tags in JSF 2.0]] {{ :helloworld:web:java:taglib:jsf:custom_tags_in_jsf_2.0_mkyong.com_2020-04-26_12_20_37_am_.html |Archive du 29/08/2012 le 27/04/2020}} 
 + 
 +<file xml taglibsource.xhtml> 
 +<?xml version="1.0" encoding="UTF-8" ?> 
 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 +<html xmlns="http://www.w3.org/1999/xhtml" 
 +  xmlns:h="http://java.sun.com/jsf/html" 
 +  xmlns:f="http://java.sun.com/jsf/core" 
 +  xmlns:ui="http://java.sun.com/jsf/facelets"> 
 +<f:view> 
 +<h:body> 
 +  <table border="1"> 
 +    <tr bgcolor="yellow"> 
 +      <td> 
 +        <font color="red">#{label}: 
 +          <h:outputText value="#{session.lastAccessedTime}"> 
 +            <f:convertDateTime pattern="HH:mm:ss" type="date" /> 
 +          </h:outputText> 
 +        </font> 
 +      </td> 
 +    </tr> 
 +  </table> 
 +</h:body> 
 +</f:view> 
 +</html> 
 +</file> 
 + 
 +<file xml tagheure2.taglib.xml> 
 +<?xml version="1.0" encoding="UTF-8"?> 
 +<javaee:facelet-taglib version="2.0" 
 +  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-facelettaglibrary_2_0.xsd "> 
 +  <javaee:namespace>http://llgc.org/2</javaee:namespace> 
 +  <javaee:tag> 
 +    <javaee:tag-name>heure</javaee:tag-name> 
 +    <javaee:source>taglibsource.xhtml</javaee:source> 
 +    <javaee:attribute> 
 +      <javaee:name>label</javaee:name> 
 +      <javaee:required>true</javaee:required> 
 +    </javaee:attribute> 
 +  </javaee:tag> 
 +</javaee:facelet-taglib> 
 +</file> 
 + 
 +<file xml web.xml> 
 +<?xml version="1.0" encoding="UTF-8"?> 
 +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 +  xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
 +  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
 +  version="3.1"> 
 +  <context-param> 
 +    <param-name>javax.faces.FACELETS_LIBRARIES</param-name> 
 +    <param-value>/META-INF/tagheure2.taglib.xml</param-value> 
 +  </context-param> 
 +</web-app> 
 +</file> 
 + 
 +<WRAP center round info 60%> 
 +Il est possible de spécifier plusieurs ''taglib.xml'' dans le fichier ''web.xml'' en les séparant par des '';''
 +</WRAP> 
 + 
 +<file xml taglib2.xhtml> 
 +<?xml version="1.0" encoding="UTF-8" ?> 
 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
 +<html xmlns="http://www.w3.org/1999/xhtml" 
 +  xmlns:h="http://java.sun.com/jsf/html" 
 +  xmlns:f="http://java.sun.com/jsf/core" 
 +  xmlns:ui="http://java.sun.com/jsf/facelets" 
 +  xmlns:tag="http://llgc.org/2"> 
 +<h:head> 
 +  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
 +  <title>Insert title here</title> 
 +</h:head> 
 +<h:body> 
 +  <tag:heure label="test" /> 
 +</h:body> 
 +</html> 
 +</file> 
 +Rendu : 
 +{{:helloworld:web:java:taglib:jsp:rendu_ex2.png|Rendu exemple 2}} 
 + 
 +=====Ajout d'un validateur des attributs===== 
 +En ''JSF'', il n'est possible de valider les attributs que pour des composants héritants de ''EditableValueHolder'', par exemple ''HtmlInputText'' ou encore ''HtmlSelectManyCheckbox''
 + 
 +La seule solution que j'ai trouvé pour avoir un comportement similaire à ''isValid'' de ''TagExtraInfo'' est de générer une exception ''IOException'' dans la méthode ''encodeBegin'' du ''Renderer''.
helloworld/web/java/taglib/jsf.1439499387.txt.gz · Dernière modification : 2015/08/13 22:56 de root