Outils pour utilisateurs

Outils du site


helloworld:web:java:taglib:jsf

Nécessite un projet de type Dynamic Web Project avec les JSF activés.

component + renderer

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;
 
@FacesRenderer (componentFamily="com.llgc.heureCourante", rendererType="com.llgc.heureAvecRenderer")
public class TagHeureRenderer extends Renderer
{
  @Override
  public void encodeBegin (FacesContext context, UIComponent component) throws IOException
  {
    Calendar calendrier = Calendar.getInstance ();
    String intitule = (String) component.getAttributes ().get ("label");
    String heureCourante = calendrier.get (Calendar.HOUR) + ":" + calendrier.get (Calendar.MINUTE) + ":"
        + 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);
  }
}
TagHeureHtml.java
package com.llgc;
 
import java.io.IOException;
 
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
 
@FacesComponent ("com.llgc.monHtmlHeureCourante")
public class TagHeureHtml extends HtmlOutputText
{
 
  @Override
  public String getFamily ()
  {
 
    return "com.llgc.heureCourante";
  }
 
  @Override
  public void encodeEnd (FacesContext context) throws IOException
  {
    ResponseWriter out = context.getResponseWriter ();
    out.write ("<br />");
    super.encodeEnd (context);
  }
}
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/tagheure.taglib.xml</param-value>
  </context-param>
</web-app>
tagheure.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</javaee:namespace>
  <javaee:tag>
    <javaee:tag-name>heure</javaee:tag-name>
    <javaee:component>
      <javaee:component-type>com.llgc.monHtmlHeureCourante</javaee:component-type>
      <javaee:renderer-type>com.llgc.heureAvecRenderer</javaee:renderer-type>
    </javaee:component>
    <javaee:attribute>
      <javaee:name>label</javaee:name>
      <javaee:required>true</javaee:required>
    </javaee:attribute>
  </javaee:tag>
</javaee:facelet-taglib>
taglib.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">
<f:view>
  <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>
</f:view>
</html>

Rendu : Rendu exemple 2

Source XHTML

Volé (et actualisé) depuis Custom tags in JSF 2.0 Archive du 29/08/2012 le 27/04/2020

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>
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>
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>

Il est possible de spécifier plusieurs taglib.xml dans le fichier web.xml en les séparant par des ;.

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>

Rendu : 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.txt · Dernière modification : 2020/05/10 23:56 de root