Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Richfaces components

Tags:

jsf

richfaces

I'm looking to override the functionality of some Richfaces components (mostly the extendedDataTable).

Could somebody provide a thorough example of any component they extended, with code examples?

I know the below thread describes how to do it, but a code example would be hugely beneficial.

Extend Richfaces components - for example customize Datatable component for specific implementation


Ok.....I've been trawling the internet and doesn't seem to be a whole lot of clear step by step simple examples of this. Seem to be the case here too :(

I have managed to put together the following so far:

I've created a custom ExtendedDataTableRenderer renderer as follows:

package com.test;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.richfaces.renderkit.html.ExtendedDataTableRenderer;

public class HtmlExtendedDataTableRendered extends ExtendedDataTableRenderer
{
     @Override
     public void encodeBegin(FacesContext context, UIComponent component) throws IOException
     {
               System.out.println("------- in HtmlExtendedDataTableRendered init() -----");

                    // TODO Auto-generated method stub
                    super.encodeBegin(context, component);
     }
}

And added the following to my faces-config.xml as follows:

<render-kit>
     <renderer>
          <component-family>org.richfaces.ExtendedDataTable</component-family>
          <renderer-type>org.richfaces.ExtendedDataTableRenderer</renderer-type>
          <renderer-class>com.test.HtmlExtendedDataTableRendered</renderer-class>
     </renderer>
</render-kit>

When I hit a page that has an entendedDataTable component this seems to be working (It is prining the test code to the console).

Now I also extended the HtmlExtendedDataTable class as follows:

package com.test;

import java.io.IOException;
import javax.faces.context.FacesContext;
import org.richfaces.component.html.HtmlExtendedDataTable;

public class customExtendedDataTable extends HtmlExtendedDataTable {
    /*
     * The renderer type for this component.
    */
    public static final String RENDERER_TYPE = "com.test.HtmlExtendedDataTableRendered";

    public customExtendedDataTable() {

         System.out.println("------- in customExtendedDataTable init() -----");

         setRendererType(RENDERER_TYPE);
    }

   @Override
    public String getFamily() {
        return customExtendedDataTable.RENDERER_TYPE;
    }


   @Override
   public boolean groupIsExpanded(int index)
   {
        System.out.println("------- in groupIsExpanded -----");

        // TODO Auto-generated method stub
        return super.groupIsExpanded(1);
   }

   @Override
   public void encodeBegin(FacesContext context) throws IOException
   {
        System.out.println("------- in encodeBegin -----");

        // TODO Auto-generated method stub
        super.encodeBegin(context);
   }
}

Nothing is been printed to the console from within this class when I enter a page that has the extendedDataTable component.

Could anybody provide me with some help as to why this is not working please?

Thanks

like image 893
Thomas Buckley Avatar asked Feb 16 '11 12:02

Thomas Buckley


1 Answers

You've only definied the renderer in faces-config.xml, but you have definied the component nowhere. It has to go in the faces-config.xml as well:

<component>
    <component-type>customExtendedDataTable</component-type>
    <component-class>com.test.customExtendedDataTable</component-class>
</component>

And don't forget to add a getter getComponentType() to your custom component which returns the same value as is been definied in <component-type>.

Further you need to create a tag file as well so that you can use it in your view. It is not so that you can substitute the code of an existing rich:extendedDataTable. You've got to use my:extendedDataTable for example. Creating a taglib.xml is pretty clumsy, I can't write it down from top of head (yet? ;) ), so here are some links which should help you in the right direction:

  • JSF component development
  • JSF component writing checklist
  • JSF component with all event handlers

Unrelated to the concrete problem, classnames should really start with uppercase.

like image 146
BalusC Avatar answered Oct 22 '22 01:10

BalusC