Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c01/h14/mnt/37629/domains/blog.evolutionarydawn.com/html/wp-includes/cache.php on line 36

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c01/h14/mnt/37629/domains/blog.evolutionarydawn.com/html/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c01/h14/mnt/37629/domains/blog.evolutionarydawn.com/html/wp-includes/theme.php on line 540
Evolutionary Dawn » Custom JSF Component 1.2

Evolutionary Dawn

June 11, 2009

Custom JSF Component 1.2

Filed under: Uncategorized — admin @ 12:25 pm

Since I had a hard time finding a complete solution anywhere online I decided that I would post a simple how-to for a JSF Custom component.

The problem that this component seeks to address is that the <f:verbatim> tag does not work properly in a <ui:repeat> tag.

This presented a issue to me because I have RAW HTML stored in my data model to be used in my simple blog application.

1. The most confusing thing about JSF Custom components.  YOU NEED 3 SEPERATE CLASSES TO IMPLEMENT A CUSTOM COMPONENT.

Alot of other tutorials will teach you there is a method in which you can use only two classes to accomplish the task, although this is true the most straight forward approach is to use 3 classes to implement your component.

The are as follows:

  1. ┬áThe component class (For this tutorial: HTMLVerbatimComponent) This class is technically where the “DATA” resisdes
  2. The tag class (For this tutorial: HTMLVerbatimTag) This class is the ADAPTER piece between the “JSF Page” and the DATA Model “Your component”
  3. The renderer class (For this tutorial: HTMLVerbatimRenderer) This class is response for the actual rendering of the HTML or OUTPUT.

The component class:

package com.mdb.web.jsf.component;
 
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.el.ValueExpression;
 
public class HTMLVerbatimComponent extends UIComponentBase {
 
    public HTMLVerbatimComponent() { }
 
    //Properties you want exposed as tag values must be exposed using getters/setters
    private String value;
    public String getValue() {
        if(value != null)
            return value;
        //If the value has not already been set interpret the Expression language and get resulting object
        Object value = this.getValueExpressionValue("value");
        return value != null ? (String)value : null;
    }
 
    public void setValue(String value) { this.value = value;}
 
    //Honestly I am not sure what this method does besides link the component to the config files
    public String getFamily() {
        return "com.mdb.web.jsf.component.HTMLVerbatim";
    }
 
    //Method allows component state to be stored
    @Override
    public Object saveState(FacesContext context) {
        return new Object[] { super.saveState(context), value};
    }
 
    //Method allows component state to be restored
    @Override
    public void restoreState(FacesContext context, Object object) {
        Object state[] = (Object[]) object;
        super.restoreState(context, state[0]);
        value = (String)state[1];
    }
 
    //Helper method to simplify getters/setters
    //This helps the JSF Component use Expression Language
    private Object getValueExpressionValue(String name) {
        ValueExpression ve = super.getValueExpression(name);
        return ve.getValue(FacesContext.getCurrentInstance().getELContext());
    }
}

The tag class:

package com.mdb.web.jsf.component;
 
import javax.faces.webapp.UIComponentELTag;
import javax.faces.component.UIComponent;
import javax.el.ValueExpression;
public class HTMLVerbatimTag extends UIComponentELTag {
    //Temporary holder property hence if I put
    //&lt;tag:htmlVerbatim value="#{SomeBackingBean.htmlText}"/&gt;
    //information is stored in this object until the data is pushed into the component in setProperties method
    private ValueExpression value = null;
    public HTMLVerbatimTag() {}
    public ValueExpression getValue() { return this.value;}
    public void setValue(ValueExpression value) { this.value = value;}
 
    //This is where the action happens
    //Data is taken from the tag on the JSF Page and placed into the Component
    @Override
    protected void setProperties(UIComponent component) {
        super.setProperties(component);
        HTMLVerbatimComponent verbatimComponent = (HTMLVerbatimComponent)component;
        if(value != null) {
            verbatimComponent.setValueExpression("value", value);
        }
    }
 
    //Method links this tag object to the component in config files
    public String getComponentType() { return "com.mdb.web.jsf.component.HTMLVerbatim"; }
 
    //Method links this tag object to the renderer in config files
    public String getRendererType() { return "com.mdb.web.jsf.component.HTMLVerbatim";}
 
}

The renderer class:

package com.mdb.web.jsf.component;import javax.faces.render.Renderer;
 
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import java.io.IOException;
 
public class HTMLVerbatimRenderer extends Renderer {public HTMLVerbatimRenderer() {}
 
    //Render your HTML HERE
    @Override
    public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
        HTMLVerbatimComponent verbatimComponent = (HTMLVerbatimComponent)component;
        ResponseWriter writer = context.getResponseWriter();
        writer.write(verbatimComponent.getValue());
        writer.flush();
    }
 
}

Now you must make some changes to your configuration files and I make the big assumption you are using Facelets.

Create a file in your WEB-INF directory called: custom.taglib.xml

Add the following content (Edit accordingly for your own component and domain):

 
  "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
  "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"&gt;
 
<facelet-taglib>
    <namespace>http://mechdrives.com/web/jsf/component/tags</namespace>
    <tag>
        <tag-name>htmlVerbatim</tag-name>
        <component>
            <component-type>com.mdb.web.jsf.component.HTMLVerbatim</component-type>
            <renderer-type>com.mdb.web.jsf.component.HTMLVerbatim</renderer-type>
        </component>
    </tag>
</facelet-taglib>

Edit your faces-config.xml and add the following content(Edit accordingly for your own component and domain):

 
<faces-config version="1.2">
    xmlns="http://java.sun.com/xml/ns/javaee"
    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-facesconfig_1_2.xsd"&gt;
    <component>
        <component-type>com.mdb.web.jsf.component.HTMLVerbatim</component-type>
        <component-class>com.mdb.web.jsf.component.HTMLVerbatimComponent</component-class>
    </component>
    <render-kit>
        <renderer>
            <component-family>com.mdb.web.jsf.component.HTMLVerbatim</component-family>
            <renderer-type>com.mdb.web.jsf.component.HTMLVerbatim</renderer-type>
            <renderer-class>com.mdb.web.jsf.component.HTMLVerbatimRenderer</renderer-class>
        </renderer>
    </render-kit>
    <application>
        <locale-config>
            <default-locale>en</default-locale>
        </locale-config>
        <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
</faces-config>

Edit your web.xml and add the following content:

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

Now you should be able to use your new component/tag simply by adding a namespace to your JSF Page as such:

 

then using the tag as such:

<custom:htmlverbatim value="#{SomeBackingBean.htmlText}">
</custom:htmlverbatim>

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress

  • lavinia honey boy girlshare
  • english subtitles for korean drama playful kiss
  • ncis la download free
  • bubble bobble free game full version
  • red hot chili peppers greatest hits
  • songs of film barfi for
  • ye tumhari meri baaten ringtone free download
  • glee project mad world song download
  • microsoft download free 2007
  • starcraft 2 mac download client
  • foster the people helena beat acoustic mp3 download
  • nyan koi episode 1 english sub
  • rhoma irama mirasantika mp3
  • sharp driver printer
  • hum tv drama humsafar all episodes download
  • too many items minecraft beta 1.6.6 download
  • musicas anos 70 e 80
  • download she elvis costello notting hill
  • download mod loader for minecraft mac
  • ashampoo burning studio 9 free software
  • asus motherboard drivers free download for windows xp
  • new mashup songs 2012 download
  • linkin park new album free
  • revolutionary vol 2 album download
  • photo book maker software download free
  • school days hq game
  • download zynga poker for pocket pc
  • oracle 11g application server download
  • mad cobra mp3
  • the english dictionary free
  • gta san andreas download 1 link pc
  • the virgin cinta free
  • compiz configuration settings manager download
  • download free ebook international business competing in the global marketplace
  • mae eu te amo volume 3
  • jogos de ps2 gratis
  • sidney samson feat lil jon mutate
  • raiden fighters 2 rom download
  • nokia n82 rocknscroll download
  • how to a font to microsoft word 2007
  • amor clandestino de mana download
  • busca implacavel 2 dublado avi
  • blunt blowin free mp3
  • rock on hindi movie download free
  • free direct file sharing
  • megadeth this day we fight
  • drizzy is back free
  • apostila de espanhol baixar gratis
  • gta chinatown wars psp rom download
  • advent vega usb driver download
  • pakistani ghajini full movie free
  • photoshop em portugues download free
  • non stop hits download
  • trigun subtitle indonesia
  • choo choo soul album
  • ang bayan kong pilipinas mp3 free
  • x note full game free mac
  • jailbreak software for ps3 download
  • chota bacha song
  • kelly cutrone normal gets you nowhere lomg download
  • download filme eclipse legendado rmvb
  • adam dan hawa download mkv
  • wwe raw 2005 game free full version for pc
  • playman winter games 3 download
  • stai in ploaie ca ai valoare
  • mac miller free album download best day ever
  • bakemonogatari sub ita download ep 13
  • root chainfire3d pro free
  • free la noire keygen pc
  • firmware 3.41 eu ps3 download
  • htc desire z download music
  • lethal weapon 2
  • swf decompiler free for windows
  • bb manager 4
  • imnul crizei download fisierul meu
  • okidata c3400 status monitor download
  • shaka zulu pickney download
  • mary mary walking instrumental free
  • cd do eminem 2012 download
  • cartao de natal baixar gratis
  • bomb ringtones free s
  • safetysuit these times download mp3
  • jailbreak 5.0 1 free
  • jogo do mario gratis para pc
  • lou bega sweet like cola mp3
  • odd future mixtape zip
  • youtube all video downloader software free download
  • bollywood songs clips download
  • samsung n148 plus driver download for windows 7
  • angry birds space for nokia c6 00
  • baby boy free movie
  • games download full version 3d
  • charlie and chocolate factory download movie
  • filme 2012 avi dublado
  • free antispyware 2012
  • grand theft auto 4 episodes from liberty city tpb
  • justiAža jovem dublado 1 temporada completa
  • cell phone software free
  • lil wayne soundowl
  • video conversation bahasa inggris
  • your phone ringtone download
  • free music download to windows media player legal