You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Christian Wiesing <cw...@ordix.de> on 2006/11/27 15:38:13 UTC

Using custom JSF-Components with Facelets

Hello,

how can I use custom JSF components with Facelets? I have some JSF 
components which I wanna use in my Facelets-Web-App.

Is there any example or something like that?

regards,

Christian



Re: Using custom JSF-Components with Facelets

Posted by Andrew Robinson <an...@gmail.com>.
You can also download my ANT plug-in that generates taglib.xml files
from a TLD file [1 & 2]. It is what I used to get started using
Tomahawk with facelets. I hand modify it now that it is done, so let
me know if you use it and find any issues. Check out the "all" release
for the source code and an example.

-Andrew

[1] http://sourceforge.net/projects/jsf-comp
[2] http://sourceforge.net/project/showfiles.php?group_id=137466&package_id=168411&release_id=390067

On 11/27/06, Matthias Wessendorf <ma...@apache.org> wrote:
> Hi,
>
> I think the issue is that your component is rendering itself, which
> you shouldn't do.
> (or can you try null for the renderer-type node?)
>
> It is better to provide a renderer for your component (better reuse of
> the component).
> After all use these nodes in the taglib.xml file
>
>             <component-type />
>             <renderer-type />
>
> one cool thing with facelets is, that you don't need to write a tag,
> (since that guy is only for the jsp world of jsf)
>
>
>
> On 11/27/06, Christian Wiesing <cw...@ordix.de> wrote:
> > Thanks,
> >
> > i created the Facelets-Taglib-File, but it still don't work. It would be
> > great if somebody could tell me what I do wrong. See my source code below.
> >
> > Thanks.
> >
> > Christian
> >
> > ---------------------------------------
> > view.xhtml ----------------------------
> > ---------------------------------------
> >
> > <!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:ui="http://java.sun.com/jsf/facelets"
> >       xmlns:h="http://java.sun.com/jsf/html"
> >       xmlns:f="http://java.sun.com/jsf/core"
> >       xmlns:cf="http://test.com/customtags">
> >
> >     <cf:jsfhello hellomsg="Hello world."   />
> >
> > </html>
> >
> > ---------------------------------------
> > custom-taglib.xml ---------------------
> > ---------------------------------------
> >
> > <?xml version="1.0"?>
> > <!DOCTYPE facelet-taglib PUBLIC
> >   "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
> >   "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
> >
> > <facelet-taglib>
> >     <namespace>http://test.com/customtags</namespace>
> >
> >     <tag>
> >         <tag-name>jsfhello</tag-name>
> >     <component>
> >             <component-type>demo.JsfHello</component-type>
> >         </component>
> >     </tag>
> >
> > </facelet-taglib>
> >
> > ---------------------------------------
> > faces-config --------------------------
> > ---------------------------------------
> > <faces-config>
> >
> >   <component>
> >    <component-type>demo.JsfHello</component-type>
> >    <component-class>demo.HelloUIComp</component-class>
> >   </component>
> >
> >
> > ---------------------------------------
> > HelloUIComp.java -------------------
> > ---------------------------------------
> > package demo;
> >
> > import java.util.Date;
> > import javax.faces.component.UIComponentBase;
> > import javax.faces.context.FacesContext;
> > import java.io.IOException;
> > import javax.faces.context.ResponseWriter;
> >
> >
> > public class HelloUIComp extends UIComponentBase
> > {
> >
> >   public void encodeBegin(FacesContext context) throws IOException
> >   {
> >    ResponseWriter writer = context.getResponseWriter();
> >    String hellomsg = (String)getAttributes().get("hellomsg");
> >
> >    writer.startElement("h3", this);
> >    if(hellomsg != null)
> >      writer.writeText(hellomsg, "hellomsg");
> >    else
> >      writer.writeText("Hello from a custom JSF UI Component!",
> > null);
> >    writer.endElement("h3");
> >    writer.startElement("p", this);
> >    writer.writeText(" Today is: " + new Date(), null);
> >    writer.endElement("p");
> >   }
> >
> >  public String getFamily()
> >  {
> >   return "HelloFamily";
> >  }
> > }
> >
> > ---------------------------------------
> > FacesHelloTag -----------------------
> > ---------------------------------------
> >
> > package demo;
> >
> > import javax.faces.application.Application;
> > import javax.faces.webapp.UIComponentTag;
> > import javax.faces.component.UIComponent;
> > import javax.faces.el.ValueBinding;
> > import javax.faces.context.FacesContext;
> >
> > public class FacesHelloTag extends UIComponentTag
> > {
> >   // Declare a bean property for the hellomsg attribute.
> >   public String hellomsg = null;
> >
> >
> >   // Associate the renderer and component type.
> >   public String getComponentType() { return "demo.JsfHello"; }
> >   public String getRendererType() { return null; }
> >
> >   protected void setProperties(UIComponent component)
> >   {
> >     super.setProperties(component);
> >
> >     // set hellomsg
> >     if (hellomsg != null)
> >     {
> >       if (isValueReference(hellomsg))
> >       {
> >         FacesContext context = FacesContext.getCurrentInstance();
> >         Application app = context.getApplication();
> >         ValueBinding vb = app.createValueBinding(hellomsg);
> >         component.setValueBinding("hellomsg", vb);
> >       }
> >       else
> >         component.getAttributes().put("hellomsg", hellomsg);
> >     }
> >   }
> >
> >   public void release()
> >   {
> >     super.release();
> >     hellomsg = null;
> >   }
> >
> >
> >   public void setHellomsg(String hellomsg)
> >   {
> >     this.hellomsg = hellomsg;
> >   }
> > }
> >
> > Matthias Wessendorf schrieb:
> > > Hi Christian,
> > >
> > > take a look at [1]. That describes the steps for Tomahawk custom
> > > components; which are also true for your custom components.
> > >
> > > HTH,
> > > Matthias
> > >
> > > [1] http://wiki.apache.org/myfaces/Use_Facelets_with_Tomahawk
> > >
> > > On 11/27/06, Christian Wiesing <cw...@ordix.de> wrote:
> > >> Hello,
> > >>
> > >> how can I use custom JSF components with Facelets? I have some JSF
> > >> components which I wanna use in my Facelets-Web-App.
> > >>
> > >> Is there any example or something like that?
> > >>
> > >> regards,
> > >>
> > >> Christian
> > >>
> > >>
> > >>
> > >>
> > >>
> > >
> > >
> >
> >
>
>
> --
> Matthias Wessendorf
> http://tinyurl.com/fmywh
>
> further stuff:
> blog: http://jroller.com/page/mwessendorf
> mail: mwessendorf-at-gmail-dot-com
>

Re: Using custom JSF-Components with Facelets

Posted by Matthias Wessendorf <ma...@apache.org>.
Hi,

I think the issue is that your component is rendering itself, which
you shouldn't do.
(or can you try null for the renderer-type node?)

It is better to provide a renderer for your component (better reuse of
the component).
After all use these nodes in the taglib.xml file

            <component-type />
            <renderer-type />

one cool thing with facelets is, that you don't need to write a tag,
(since that guy is only for the jsp world of jsf)



On 11/27/06, Christian Wiesing <cw...@ordix.de> wrote:
> Thanks,
>
> i created the Facelets-Taglib-File, but it still don't work. It would be
> great if somebody could tell me what I do wrong. See my source code below.
>
> Thanks.
>
> Christian
>
> ---------------------------------------
> view.xhtml ----------------------------
> ---------------------------------------
>
> <!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:ui="http://java.sun.com/jsf/facelets"
>       xmlns:h="http://java.sun.com/jsf/html"
>       xmlns:f="http://java.sun.com/jsf/core"
>       xmlns:cf="http://test.com/customtags">
>
>     <cf:jsfhello hellomsg="Hello world."   />
>
> </html>
>
> ---------------------------------------
> custom-taglib.xml ---------------------
> ---------------------------------------
>
> <?xml version="1.0"?>
> <!DOCTYPE facelet-taglib PUBLIC
>   "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
>   "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
>
> <facelet-taglib>
>     <namespace>http://test.com/customtags</namespace>
>
>     <tag>
>         <tag-name>jsfhello</tag-name>
>     <component>
>             <component-type>demo.JsfHello</component-type>
>         </component>
>     </tag>
>
> </facelet-taglib>
>
> ---------------------------------------
> faces-config --------------------------
> ---------------------------------------
> <faces-config>
>
>   <component>
>    <component-type>demo.JsfHello</component-type>
>    <component-class>demo.HelloUIComp</component-class>
>   </component>
>
>
> ---------------------------------------
> HelloUIComp.java -------------------
> ---------------------------------------
> package demo;
>
> import java.util.Date;
> import javax.faces.component.UIComponentBase;
> import javax.faces.context.FacesContext;
> import java.io.IOException;
> import javax.faces.context.ResponseWriter;
>
>
> public class HelloUIComp extends UIComponentBase
> {
>
>   public void encodeBegin(FacesContext context) throws IOException
>   {
>    ResponseWriter writer = context.getResponseWriter();
>    String hellomsg = (String)getAttributes().get("hellomsg");
>
>    writer.startElement("h3", this);
>    if(hellomsg != null)
>      writer.writeText(hellomsg, "hellomsg");
>    else
>      writer.writeText("Hello from a custom JSF UI Component!",
> null);
>    writer.endElement("h3");
>    writer.startElement("p", this);
>    writer.writeText(" Today is: " + new Date(), null);
>    writer.endElement("p");
>   }
>
>  public String getFamily()
>  {
>   return "HelloFamily";
>  }
> }
>
> ---------------------------------------
> FacesHelloTag -----------------------
> ---------------------------------------
>
> package demo;
>
> import javax.faces.application.Application;
> import javax.faces.webapp.UIComponentTag;
> import javax.faces.component.UIComponent;
> import javax.faces.el.ValueBinding;
> import javax.faces.context.FacesContext;
>
> public class FacesHelloTag extends UIComponentTag
> {
>   // Declare a bean property for the hellomsg attribute.
>   public String hellomsg = null;
>
>
>   // Associate the renderer and component type.
>   public String getComponentType() { return "demo.JsfHello"; }
>   public String getRendererType() { return null; }
>
>   protected void setProperties(UIComponent component)
>   {
>     super.setProperties(component);
>
>     // set hellomsg
>     if (hellomsg != null)
>     {
>       if (isValueReference(hellomsg))
>       {
>         FacesContext context = FacesContext.getCurrentInstance();
>         Application app = context.getApplication();
>         ValueBinding vb = app.createValueBinding(hellomsg);
>         component.setValueBinding("hellomsg", vb);
>       }
>       else
>         component.getAttributes().put("hellomsg", hellomsg);
>     }
>   }
>
>   public void release()
>   {
>     super.release();
>     hellomsg = null;
>   }
>
>
>   public void setHellomsg(String hellomsg)
>   {
>     this.hellomsg = hellomsg;
>   }
> }
>
> Matthias Wessendorf schrieb:
> > Hi Christian,
> >
> > take a look at [1]. That describes the steps for Tomahawk custom
> > components; which are also true for your custom components.
> >
> > HTH,
> > Matthias
> >
> > [1] http://wiki.apache.org/myfaces/Use_Facelets_with_Tomahawk
> >
> > On 11/27/06, Christian Wiesing <cw...@ordix.de> wrote:
> >> Hello,
> >>
> >> how can I use custom JSF components with Facelets? I have some JSF
> >> components which I wanna use in my Facelets-Web-App.
> >>
> >> Is there any example or something like that?
> >>
> >> regards,
> >>
> >> Christian
> >>
> >>
> >>
> >>
> >>
> >
> >
>
>


-- 
Matthias Wessendorf
http://tinyurl.com/fmywh

further stuff:
blog: http://jroller.com/page/mwessendorf
mail: mwessendorf-at-gmail-dot-com

Re: Using custom JSF-Components with Facelets

Posted by kindsol <ki...@hotpop.com>.
You also need to tell facelets to look at your custom-taglib:

Add to web.xml something like:

<context-param>
   	<param-name>facelets.LIBRARIES</param-name>
   	<param-value>/WEB-INF/custom-taglib.xml</param-value>
   </context-param

You can add more files to the param-value, just separate each file by  
a semicolon.

Good luck!

-Sol


On Nov 27, 2006, at 8:05 AM, Christian Wiesing wrote:

> Thanks,
>
> i created the Facelets-Taglib-File, but it still don't work. It  
> would be great if somebody could tell me what I do wrong. See my  
> source code below.
>
> Thanks.
>
> Christian
>
> ---------------------------------------
> view.xhtml ----------------------------
> ---------------------------------------
>
> <!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:ui="http://java.sun.com/jsf/facelets"
>      xmlns:h="http://java.sun.com/jsf/html"
>      xmlns:f="http://java.sun.com/jsf/core"
>      xmlns:cf="http://test.com/customtags">
>        <cf:jsfhello hellomsg="Hello world."   />
>                                           </html>
>
> ---------------------------------------
> custom-taglib.xml ---------------------
> ---------------------------------------
>
> <?xml version="1.0"?>
> <!DOCTYPE facelet-taglib PUBLIC
>  "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
>  "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
>
> <facelet-taglib>
>    <namespace>http://test.com/customtags</namespace>
>    <tag>
>        <tag-name>jsfhello</tag-name>
>    <component>
>            <component-type>demo.JsfHello</component-type>
>        </component>      </tag>
>
> </facelet-taglib>
>
> ---------------------------------------
> faces-config --------------------------
> ---------------------------------------
> <faces-config>
>
>  <component>
>   <component-type>demo.JsfHello</component-type>
>   <component-class>demo.HelloUIComp</component-class>
>  </component>
>
>
> ---------------------------------------
> HelloUIComp.java -------------------
> ---------------------------------------
> package demo;
>
> import java.util.Date;
> import javax.faces.component.UIComponentBase;
> import javax.faces.context.FacesContext;
> import java.io.IOException;
> import javax.faces.context.ResponseWriter;
>
>
> public class HelloUIComp extends UIComponentBase
> {
>
>  public void encodeBegin(FacesContext context) throws IOException
>  {
>   ResponseWriter writer = context.getResponseWriter();
>   String hellomsg = (String)getAttributes().get("hellomsg");
>    writer.startElement("h3", this);
>   if(hellomsg != null)
>     writer.writeText(hellomsg, "hellomsg");
>   else
>     writer.writeText("Hello from a custom JSF UI Component!",  
> null);            writer.endElement("h3");    writer.startElement 
> ("p", this);
>   writer.writeText(" Today is: " + new Date(), null);
>   writer.endElement("p");   }
>
> public String getFamily()
> {
>  return "HelloFamily";
> }
> }
> ---------------------------------------
> FacesHelloTag -----------------------
> ---------------------------------------
>
> package demo;
>
> import javax.faces.application.Application;
> import javax.faces.webapp.UIComponentTag;
> import javax.faces.component.UIComponent;
> import javax.faces.el.ValueBinding;
> import javax.faces.context.FacesContext;
>
> public class FacesHelloTag extends UIComponentTag
> {
>  // Declare a bean property for the hellomsg attribute.
>  public String hellomsg = null;
>
>
>  // Associate the renderer and component type.
>  public String getComponentType() { return "demo.JsfHello"; }
>  public String getRendererType() { return null; }
>
>  protected void setProperties(UIComponent component)
>  {
>    super.setProperties(component);
>      // set hellomsg
>    if (hellomsg != null)
>    {
>      if (isValueReference(hellomsg))
>      {
>        FacesContext context = FacesContext.getCurrentInstance();
>        Application app = context.getApplication();
>        ValueBinding vb = app.createValueBinding(hellomsg);
>        component.setValueBinding("hellomsg",  
> vb);                      }
>      else
>        component.getAttributes().put("hellomsg", hellomsg);
>    }                         }
>
>  public void release()
>  {
>    super.release();
>    hellomsg = null;
>  }
>
>
>  public void setHellomsg(String hellomsg)
>  {
>    this.hellomsg = hellomsg;
>  }
> }
>
> Matthias Wessendorf schrieb:
>> Hi Christian,
>>
>> take a look at [1]. That describes the steps for Tomahawk custom
>> components; which are also true for your custom components.
>>
>> HTH,
>> Matthias
>>
>> [1] http://wiki.apache.org/myfaces/Use_Facelets_with_Tomahawk
>>
>> On 11/27/06, Christian Wiesing <cw...@ordix.de> wrote:
>>> Hello,
>>>
>>> how can I use custom JSF components with Facelets? I have some JSF
>>> components which I wanna use in my Facelets-Web-App.
>>>
>>> Is there any example or something like that?
>>>
>>> regards,
>>>
>>> Christian
>>>
>>>
>>>
>>>
>>>
>>
>>
>


Re: Using custom JSF-Components with Facelets

Posted by Christian Wiesing <cw...@ordix.de>.
Thanks,

i created the Facelets-Taglib-File, but it still don't work. It would be 
great if somebody could tell me what I do wrong. See my source code below.

Thanks.

Christian

---------------------------------------
view.xhtml ----------------------------
---------------------------------------

<!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:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:cf="http://test.com/customtags">
     
    <cf:jsfhello hellomsg="Hello world."   />
                                           
</html>

---------------------------------------
custom-taglib.xml ---------------------
---------------------------------------

<?xml version="1.0"?>
<!DOCTYPE facelet-taglib PUBLIC
  "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
  "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">

<facelet-taglib>
    <namespace>http://test.com/customtags</namespace>
 
    <tag>
        <tag-name>jsfhello</tag-name>
    <component>
            <component-type>demo.JsfHello</component-type>
        </component>   
    </tag>

</facelet-taglib>

---------------------------------------
faces-config --------------------------
---------------------------------------
<faces-config>

  <component>
   <component-type>demo.JsfHello</component-type>
   <component-class>demo.HelloUIComp</component-class>
  </component>


---------------------------------------
HelloUIComp.java -------------------
---------------------------------------
package demo;

import java.util.Date;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import java.io.IOException;
import javax.faces.context.ResponseWriter;


public class HelloUIComp extends UIComponentBase
{

  public void encodeBegin(FacesContext context) throws IOException
  {
   ResponseWriter writer = context.getResponseWriter();
   String hellomsg = (String)getAttributes().get("hellomsg");
  
   writer.startElement("h3", this);
   if(hellomsg != null)
     writer.writeText(hellomsg, "hellomsg");
   else
     writer.writeText("Hello from a custom JSF UI Component!", 
null);          
   writer.endElement("h3");  
   writer.startElement("p", this);
   writer.writeText(" Today is: " + new Date(), null);
   writer.endElement("p");  
  }

 public String getFamily()
 {
  return "HelloFamily";
 }
} 

---------------------------------------
FacesHelloTag -----------------------
---------------------------------------

package demo;

import javax.faces.application.Application;
import javax.faces.webapp.UIComponentTag;
import javax.faces.component.UIComponent;
import javax.faces.el.ValueBinding;
import javax.faces.context.FacesContext;

public class FacesHelloTag extends UIComponentTag
{
  // Declare a bean property for the hellomsg attribute.
  public String hellomsg = null;


  // Associate the renderer and component type.
  public String getComponentType() { return "demo.JsfHello"; }
  public String getRendererType() { return null; }

  protected void setProperties(UIComponent component)
  {
    super.setProperties(component);
   
    // set hellomsg
    if (hellomsg != null)
    {
      if (isValueReference(hellomsg))
      {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        ValueBinding vb = app.createValueBinding(hellomsg);
        component.setValueBinding("hellomsg", vb);                 
      }
      else
        component.getAttributes().put("hellomsg", hellomsg);
    }                        
  }

  public void release()
  {
    super.release();
    hellomsg = null;
  }


  public void setHellomsg(String hellomsg)
  {
    this.hellomsg = hellomsg;
  }
}

Matthias Wessendorf schrieb:
> Hi Christian,
>
> take a look at [1]. That describes the steps for Tomahawk custom
> components; which are also true for your custom components.
>
> HTH,
> Matthias
>
> [1] http://wiki.apache.org/myfaces/Use_Facelets_with_Tomahawk
>
> On 11/27/06, Christian Wiesing <cw...@ordix.de> wrote:
>> Hello,
>>
>> how can I use custom JSF components with Facelets? I have some JSF
>> components which I wanna use in my Facelets-Web-App.
>>
>> Is there any example or something like that?
>>
>> regards,
>>
>> Christian
>>
>>
>>
>>
>>
>
>


Re: Using custom JSF-Components with Facelets

Posted by Matthias Wessendorf <ma...@apache.org>.
Hi Christian,

take a look at [1]. That describes the steps for Tomahawk custom
components; which are also true for your custom components.

HTH,
Matthias

[1] http://wiki.apache.org/myfaces/Use_Facelets_with_Tomahawk

On 11/27/06, Christian Wiesing <cw...@ordix.de> wrote:
> Hello,
>
> how can I use custom JSF components with Facelets? I have some JSF
> components which I wanna use in my Facelets-Web-App.
>
> Is there any example or something like that?
>
> regards,
>
> Christian
>
>
>
>
>


-- 
Matthias Wessendorf
http://tinyurl.com/fmywh

further stuff:
blog: http://jroller.com/page/mwessendorf
mail: mwessendorf-at-gmail-dot-com