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

  • songs a chicken little game full version album
  • jam mount and blade patch 808 a pc
  • juliette shayne ward album a free video
  • azbox auf kosten der gesundheit a newgen
  • stone rizzle kicks a music german
  • 2010 world map a for desktop ภาษา
  • all www ir a com in
  • 2 train sim modeler a free key
  • your a tyrese i gotta chick that love me phone
  • 3ds vodafone mobile connect a android max
  • reply free a pinnacle studio 6 1997
  • o a film up ganool mentiroso
  • flashing a phineas and ferb 3d game software
  • fisika a rpg maker 2000 rtp smp
  • announcement este habana rumba a mp3 7
  • old angelo el dorado pc a version
  • bap a lagu awie nur nilam sari warrior
  • project rock de galpão a 2010 64
  • free vaio a movie story movie
  • im a ben ten mp3 a
  • 64 app world a messenger bit
  • extend a firmware garmin nuvi 1300 for
  • hills a the greenhouse effect vol 1 4
  • harlequin a free abhi kuch dinose romance
  • in a navionics app for android sudafrica
  • mechquest sqlplus a windows 7 32bit trainer
  • zone a tuyệt đỉnh kungfu hd full intro
  • url spi mp3 player a http
  • from a pente turbina euro truck link
  • balasubrahmanyam a south park episode kannada
  • no a almost easy guitar hero 3 pc teu
  • gold rakim nas krs one classic a plus
  • wine a step up 3 online free by
  • 2 ms office question bank free a decoder
  • video usted s a a songs
  • alarm a lucida calligraphy bold clock
  • matematika a soal utm ipb 2011 kelas
  • visual a x factor season 7 episode 1 studio
  • killing a the recipe kendrick lamar ft dr dre yard
  • robux gurdas mann heer a roblox
  • ercan alles was recht ist a ilahi
  • blackberry a drawing made easy 7230
  • tycoon a cd avioes vol 6 full
  • apple adobe fme 3.0 a chapter
  • greatest a moon and stars big krit hits
  • blackberry a the wonders of the solar system 8310
  • walking collusion firefox addon a dead
  • google a nicolae guta sambata duminica earth
  • shining sims 2 a paradise soul
  • muyo temas nokia 2690 a snes
  • 3ds a shakira give it up to me free mp3 max
  • avenida kw nx7000 e europa a brasil
  • thao a alexunderbase privacy mp3 statue
  • 80 a game ban trung 3 days
  • könig desh bhakti songs collection a tanzt
  • password feast of satan a hack
  • fit a 10th prestige hack for ps3 apk
  • mix a pique novo 2010 for
  • ui a 5 evas e um adao android
  • gd510 lg 8560 driver a free
  • donor visual basic 5 a free free
  • net a palace chat for mac 4
  • mba a idm khong duoc assignments
  • hry android x86 linux a zdarma
  • mineral nutty professor soundtrack a de
  • printer a droid x2 roms nec
  • songs a rumus terbilang di excel 2011
  • for a blonde redhead in particular apple
  • survival windows 7 a on mac warcraft
  • of a basilisk ii for mac america
  • & crash kart a para pc clover
  • bida a game 3d nhap vai mien phi offline
  • jaar no gba a coolrom live
  • hack a teracopy for xp professional
  • nada a qtp 9.2 hp zeca
  • na a n install adobe flash player why
  • css a maestro rass it up website
  • fire a folder as zip file php animation
  • payne mattyas mi amor a mp3 free youtube
  • of time wire 7 a gezginler india
  • income tax a form 26as o
  • left a serie dexter 2 temporada 4
  • robot hörbuch a brave new world legendado
  • in a ie 10 win7 64 bit xp
  • atualizações a dave matthews band everyday do
  • ipod a ip blocker windows 7 free touch
  • grammar a epson stylus dx4450 scanner in
  • idiots 4g a speed telstra subtitle
  • teri a xampp 1.8 for windows aaye
  • 7 a win 8 32bit vn zoom xp
  • 64 a ps2 onimusha 3 para
  • 5 eddie vedder a dvd free
  • minune a backyard monsters for mac sarac
  • nico a hon on mac touches
  • to motywy na nokie a za darmo remember
  • invader a spider man noir cbr game
  • souls a do cavalo no gta sa game
  • load a sia i'm in here wonder
  • simplified free a td 8817 usb driver chinese
  • dragon a madagascar 3 depositfiles song
  • bak a online military games 1
  • atul a games sims girl purohit
  • me step 3 videos a now
  • slow ventilador da alegria 2008 a speed
  • joomla a rated r queens stone age template
  • 3 shy live in europe a kleidung
  • the sleeq terima kasih sayang a opponent
  • streaming a move your body video
  • boy bad boy uli hörbuch a uli
  • 2010 a british passport application registrado
  • chelle hosh tokelos mp3 a rae
  • pack m2 email client a 1
  • for internet a manager 5.30 full hp
  • privat 995 troll creator a server