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

  • can i take tramadol with
  • same
  • cost
  • 5 3 sobres
  • social impact
  • para que es
  • 500 mg ivp
  • clinic menomonee falls
  • possible lose weight after
  • can you take together
  • irritability autism
  • 24 hour directions
  • generic fast shipping
  • exercise
  • cheap united states
  • vs
  • can cause night sweats
  • a nuvaring
  • withdrawal hell side effects
  • dxm
  • mixing with
  • dosage equivalent
  • is a mao inhibitor
  • untuk kurus
  • generic picture
  • headaches advil
  • suspension 30ml
  • logo
  • difference between cr
  • does really work
  • hct
  • sodium versus
  • nursing considerations
  • 100 mg migraines
  • is it safe for glaucoma
  • target pharmacy generic
  • vs
  • 8 dpo pregnancy symptoms
  • cream horse sarcoid
  • rheumatoid arthritis
  • how long till is out of your system
  • h pylori
  • second trimester of pregnancy
  • bph
  • e ematuria
  • history
  • 10mg side effects women
  • take treat chlamydia
  • vs
  • is addictive
  • 50 mg capsules
  • difference between
  • does ds have penicillin
  • 200 mg side effects
  • going generic
  • 10 mg tablete
  • medicament dangereux
  • risks baby
  • for protein s deficiency
  • medication action
  • rash
  • safe kids
  • will increasing my help me lose weight
  • 60 mg prospect
  • dizziness vertigo
  • and
  • urination
  • can cause high blood pressure
  • how much should i take for a uti
  • taking both and
  • hct knee pain
  • and together
  • stimulant
  • most common side effects ds
  • gel side effects
  • taking for cold
  • hct
  • e fase luteale
  • long term effects of and depression
  • dosage for ptsd
  • vs for pneumonia
  • swelling feet
  • 2 mg\/kg
  • interactions benadryl
  • can give my 2 year old benadryl
  • manufacturer
  • can you use ds for sinus infection
  • bad back
  • a monkasta
  • generic manufacturers
  • aromasin or
  • interfere with
  • kraz 255b
  • controlled substance schedule
  • antibiotics for mastitis
  • interazione pantoprazolo e
  • 50mg para que serve
  • can treat syphilis
  • can women take for kidney stones
  • taking without having period first
  • dose chronic pain
  • antibiotic for uti
  • side effects white tongue
  • interactions
  • upp i vikt
  • amphetamine
  • v tAhotenstvA
  • took for 1 week
  • emulgel bei zerrung
  • dispers n3
  • er vs
  • sleep problems
  • bacterial vagi
  • can help ovulation
  • allergy baby
  • commercial
  • hct 20 12.5 mg
  • can take melatonin
  • e preAo
  • work on cold sores
  • edl
  • does side effects go away
  • over the counter alternative to
  • patient reviews
  • antidepressant drugs
  • how long do side effects last
  • horror stories
  • or ambien for sleep
  • cream heat rash
  • ringworm
  • taking during outbreak
  • drug interaction between
  • sleep disorders
  • breastfeeding toddler
  • when does it start working
  • 200 mg fiyatA
  • side effects sweating
  • solutab vs generic
  • side effects blood pressure
  • drug abuse
  • what foods should i not eat while on
  • vs flexeril
  • buy xr online
  • abuse
  • dosage for breastfeeding
  • increased appetite
  • used nerve damage
  • t nasAl kullanAlAr
  • is safe during pregnancy
  • klonopin combination
  • drug-induced lupus
  • drug facts
  • e serenace
  • single dose packet alcohol
  • or side effects
  • como diluir o
  • a ciAA?a
  • can you take two at once
  • 9543714737
  • effects of drinking alcohol while on
  • can use treat tooth infection
  • is good for anxiety
  • peak effect time
  • tonsillitis dosage
  • news
  • side effects younger men
  • late period after stopping
  • cream for male yeast infection
  • taken with
  • liquid oral
  • benadryl children
  • side effects sleepiness
  • generic for 150 mg
  • gel boots
  • und regaine
  • withdrawal itchy skin
  • side effects of
  • drug interactions between lyrica
  • 3654 drive
  • snorting
  • selling craigslist
  • how long should i take
  • 60 mg
  • tablets india
  • gnrh
  • availability 2009
  • much does generic cost
  • antibiotic for uti
  • taken at bedtime
  • wellbutrin
  • 8 comprimidos
  • tropfen packungsbeilage
  • good sore throat
  • pump side effects
  • y lactancia
  • does work if your drunk
  • 4mg odt cost
  • ditropan
  • mixing and vicodin
  • sydney cbd
  • otc
  • how to stop spam emails
  • dbol after
  • dosis para 4 semanas
  • vs
  • tylenol while nursing
  • zonegran vs
  • solutabs
  • stroke study
  • jord
  • 100mg for acne
  • skin crawling
  • can take acetaminophen
  • and keppra
  • and macrobid interactions
  • and pregnancy safety
  • can you take with xanax
  • allergy sulfa
  • green starter pack dosing
  • capsules open
  • buy high street
  • side effects pregnancy
  • parche online
  • dry lips solution
  • to treat borderline personality disorder
  • sulfa allergy
  • drinking beer
  • is 10 mg the same as
  • 8841081293
  • interaction
  • rotting teeth
  • generic form er
  • bracelets women
  • 14 day treatment
  • directions
  • d
  • women available india
  • nhs prescription
  • medication dosage
  • can u take
  • max dose
  • how much is at costco
  • tier 2
  • side effects osteoporosis
  • cost uk
  • 200 vademecum
  • with high blood pressure
  • tendonitis
  • generic smz tmp
  • 3 weeks pregnant
  • can you get high on
  • tablets dosage
  • how to safely stop taking
  • side effects anxiety
  • can make cough worse
  • flector vs gel
  • dosage sinus infection
  • golf commercial
  • savoia re di e gerusalemme
  • how to stop emails
  • band boston 2009
  • 400 mg dosis
  • safe breastfeeding
  • take headache
  • side effects increased anxiety
  • taking and breastfeeding
  • diferenAa entre e cr
  • side effects
  • can you use for tooth infection
  • with adrenaline side effects
  • koortslip zwangerschap
  • drug interactions
  • while
  • bid 200 28 yan etkileri
  • and breast cancer
  • antibiotic and birth control
  • cellulitis flucloxacillin
  • ulotka dla pacjenta
  • taken every other day
  • foods allowed on diet
  • new studies
  • e bun
  • acne breakout after
  • dosage vulvodynia
  • for sinusitis
  • settlement
  • or levothroid
  • waschbecken keramag nr. 1
  • og p piller
  • blood sugar
  • pfizer
  • to conversion
  • taking and
  • iran eu
  • 500mg used
  • online canada
  • en 3 semanas
  • benadryl
  • tudo sobre o
  • posologie traitement zona
  • taking before bed
  • and exercise tolerance
  • 4 mg oral granules
  • can you take tylenol pm
  • side effects joint pain
  • will prevent swine flu
  • where can i buy or serophene
  • hepatitis c
  • et alcool
  • taking
  • birth control pills pregnancy
  • and combo
  • joint pain side effects
  • or
  • taking pregnant
  • how fast does start working
  • raise blood pressure
  • blood pressure medication
  • comprar en mexico
  • how quick does work
  • taking endep together
  • images
  • como importar o
  • better than
  • herbal supplements
  • provera and calendar
  • deutsch
  • when to take second dose of
  • withdrawal tips
  • philippines
  • brain damage
  • maximum dosage
  • roche price
  • affect menstrual cycle
  • drug interactions
  • will help wrinkles
  • e. coli diarrhea
  • dangers
  • buy gold coast
  • can you take klonopin
  • stopping due rash
  • level lab values
  • para que sirve en colombia
  • weekly dose
  • para que es
  • adverse side effects
  • low potassium
  • symptoms overdose
  • y
  • patient comments
  • bivirkninger
  • withdrawal symptoms
  • recreational dosage
  • sleepy
  • why does lower blood pressure
  • for a tooth infection
  • important take full glass water
  • iron
  • high creatinine
  • bula do 80 mg
  • drip rate
  • should refrigerated
  • safe labor
  • is k sparing
  • sleepiness go away
  • is a nsaid
  • interaction between
  • intramuscular injection
  • is a broad spectrum
  • long stays your system
  • drug classification
  • iv shortage
  • blood work needed
  • overdose how much
  • can women use
  • solution dosage
  • typical dose of for uti
  • cytomel and thyroid cancer
  • tolerance
  • how long until works
  • generic 20mg reviews
  • fda indication
  • there recall
  • most effective time to take
  • buy 100mg online
  • side effects long term
  • taking orally abortion
  • 6 year old
  • medication assistance
  • and interaction
  • same time
  • best place
  • vs benadryl allergic reaction
  • class action lawsuit breast cancer
  • vs side effects
  • dosage for 2 year old
  • how does work on ulcers
  • day 14 no ovulation
  • diarrhea treatment
  • inert ingredients