Dynamic switch between radiobuttons and dropdownlist – create a new custom JSF component

0

In keeping with a short tradition, today is another day during my Christmas break on which I had some time to dabble in Java Server Faces and particularly the creation of custom JSF components. This article shows a new component that may actually make sense in real applications. Let me give you a little background: most applications present the end user with text fields where the user can type in new values. Most applications also have selection components through which the user can select one of a set of allowable values. These selection components can have various shapes, such as list box, dropdownlist and radio buttons. Depending on several considerations, such as the look and feel requirements of the interaction designers and end users and the number of options to be presented in the selection component, a specific component is used in a page.

Common wisdom has it that for a small number of allowable values, radio buttons – laid out horizontally or vertically – are a good choice. With an increasing number of options, a drop downlist or list-box may be better, as they occupy less real estate than the full set of radio buttons would. The values to choose from are still very much accessible to the end user. For a really large number of options, another component usually comes into play: the List of Values, either in a floating DIV, a popup window or an AJAX powered suggestion element.

There is a challenge however with this common wisdom, apart from the question where exactly lies the boundary between a small and larger number of allowable values. And that is: we may not know beforehand, at design time, what the number of allowable values is going to be. If these values are retrieved from a dynamic collection, perhaps fueled by a web service or database query, the size of the set of value can vary wildly. So any choice made during development for either radio buttons or drop downlist may be invalid at run time. So: do we throw the common wisdom out of the window?

Well, this is exactly where the custom component presented in this article comes in. The SelectSwitcher is a component that wraps multiple select-components in its facets. Depending on the number of select-options – determined dynamically at run time – one of the facets is rendered. So depending on first of all the number of options available to the user and second of all the boundary between a small and large number of options, the page will display either the one or the other selection component.

Illustrated in a simple screen shot: we have a selection element five colors to choose from. Our SelectSwitcher contains three facets: one for a small set of options (verticallay laid out radio buttons) , one for a large set (dropdown list) and one for the read only situation (where no options need to be presented at all). With the first radio group (labeled Set the number of selections where small becomes large) we can specify when we switch from a small set of selection options to a large set.


With the same set of five colors to choose from, when we change this value from 6 to 4, we essentially tell the application that our set of colors is now a large set, and therefore should be displayed as dropdown list:

 

Of course we would have the same effect if the set of colors had dynamically changed from a small one to a large one.

The article goes into a few relatively interesting aspects of custom JSF development, including the use of facets, accessing child components during rendering, manipulating children and transfering both child-elements and attributes etc. When I started out, it seemed a real challenge. Now the component is developed as working prototype, the size of the required code base is breathtakingly small. That must be the power and joy of JSF I suppose.

The JSF snippet....
for using the SelectSwitcher element to achieve the effect demonstrated in the screenshots is the following:

&lt;AMISJSF:SelectSwitcher id=&quot;colorSelectSwitcher&quot; <br />   readonly=&quot;#{DemoSelectSwitcherBean.readOnly}&quot;<br />   switchIndex=&quot;#{DemoSelectSwitcherBean.switchValue}&quot;<br />   value=&quot;#{DemoSelectSwitcherBean.selectValue}&quot;&gt;<br />  &lt;f:facet name=&quot;small&quot;&gt;<br />    &lt;h:panelGrid columns=&quot;2&quot;&gt;<br />      &lt;af:outputText value=&quot;Choose a color from this small selection&quot;/&gt;<br />      &lt;af:selectOneRadio layout=&quot;vertical&quot;/&gt;<br />    &lt;/h:panelGrid&gt;<br />  &lt;/f:facet&gt;<br />  &lt;f:facet name=&quot;large&quot;&gt;<br />    &lt;h:panelGrid columns=&quot;2&quot;&gt;<br />      &lt;af:outputText value=&quot;Choose a color from this long list&quot;/&gt;<br />      &lt;af:selectOneChoice /&gt;<br />    &lt;/h:panelGrid&gt;<br />  &lt;/f:facet&gt;<br />  &lt;f:facet name=&quot;readonly&quot;&gt;<br />    &lt;h:panelGrid columns=&quot;2&quot;&gt;<br />      &lt;af:outputText value=&quot;Here is the read only value for the selected color&quot;/&gt;<br />      &lt;af:outputText value=&quot;READONLY&quot; id=&quot;here&quot;/&gt;<br />    &lt;/h:panelGrid&gt;<br />  &lt;/f:facet&gt;<br />  &lt;f:selectItem itemLabel=&quot;Blue&quot; itemValue=&quot;XXX&quot;/&gt;<br />  &lt;f:selectItem itemLabel=&quot;Yellow&quot; itemValue=&quot;YYY&quot;/&gt;<br />  &lt;f:selectItem itemLabel=&quot;Green&quot; itemValue=&quot;GGG&quot;/&gt;<br />  &lt;f:selectItem itemLabel=&quot;Red&quot; itemValue=&quot;RRR&quot;/&gt;<br />  &lt;f:selectItem itemLabel=&quot;White&quot; itemValue=&quot;WWW&quot;/&gt;<br />&lt;/AMISJSF:SelectSwitcher&gt;<br />&nbsp;

Let’s take a look at the implementation. We have the same combination of artefacts as before: an HtmlSelectSwitcher UIComponent Class, a corresponding Tag Class, a TLD entry (tag library description) and a registration in the faces-config.xml file.

First we will have the trivial stuff out of the way.

1. The UIComponentTag class

package nl.amis.jsf.tags;<br /><br />import javax.faces.application.Application;<br />import javax.faces.component.UIComponent;<br />import javax.faces.context.FacesContext;<br />import javax.faces.el.ValueBinding;<br />import javax.faces.webapp.UIComponentTag;<br /><br />import nl.amis.jsf.HtmlSelectSwitcher;<br /><br /><br />public class SelectSwitcherTag extends UIComponentTag {<br />    String switchIndex;<br />    String value;<br />    String disabled;<br />    String readonly;<br /><br />    public SelectSwitcherTag() {<br />    }<br /><br />    protected void setProperties(UIComponent component) {<br />        super.setProperties(component);<br />        if (switchIndex != null) {<br />            if (isValueReference(switchIndex)) {<br />                FacesContext context = FacesContext.getCurrentInstance();<br />                Application app = context.getApplication();<br />                ValueBinding vb = app.createValueBinding(switchIndex);<br />                component.setValueBinding(HtmlSelectSwitcher.ATTRIBUTE_SWITCHINDEX, <br />                                          vb);<br />            } else {<br />                component.getAttributes().put(HtmlSelectSwitcher.ATTRIBUTE_SWITCHINDEX, <br />                                              switchIndex);<br />            }<br />        }<br />        if (value != null) {<br />            if (isValueReference(value)) {<br />                FacesContext context = FacesContext.getCurrentInstance();<br />                Application app = context.getApplication();<br />                ValueBinding vb = app.createValueBinding(value);<br />                component.setValueBinding(&quot;value&quot;, vb);<br />
            } else {<br />
        component.getAttributes().put(&quot;value&quot;, switchIndex);<br />            }<br />        }<br />        if (readonly != null) {<br />            if (isValueReference(readonly)) {<br />                FacesContext context = FacesContext.getCurrentInstance();<br />                Application app = context.getApplication();<br />                ValueBinding vb = app.createValueBinding(readonly);<br />                component.setValueBinding(HtmlSelectSwitcher.ATTRIBUTE_READONLY, <br />                                          vb);<br />            } else {<br />                component.getAttributes().put(HtmlSelectSwitcher.ATTRIBUTE_READONLY, <br />                                              readonly);<br />            }<br />        }<br />        if (disabled != null) {<br />            if (isValueReference(disabled)) {<br />                FacesContext context = FacesContext.getCurrentInstance();<br />                Application app = context.getApplication();<br />                ValueBinding vb = app.createValueBinding(disabled);<br />                component.setValueBinding(&quot;disabled&quot;, vb);<br />            } else {<br />                component.getAttributes().put(&quot;disabled&quot;, disabled);<br />            }<br />        }<br />    }<br /><br />    public void release() {<br />        super.release();<br />    }<br /><br /><br />    public String getComponentType() {<br />        return &quot;SelectSwitcher&quot;;<br />    }<br /><br />    public String getRendererType() {<br />        return null;<br />    }<br /><br />    public void setSwitchIndex(String switchIndex) {<br />        this.switchIndex = switchIndex;<br />    }<br /><br />    public String getSwitchIndex() {<br />        return switchIndex;<br />    }<br /><br />    public void setValue(String value) {<br />        this.value = value;<br />    }<br /><br />    public String getValue() {<br />        return value;<br />    }<br /><br />    public void setDisabled(String disabled) {<br />        this.disabled = disabled;<br />    }<br /><br />    public String getDisabled() {<br />        return disabled;<br />    }<br /><br />    public void setReadonly(String readonly) {<br />        this.readonly = readonly;<br />    }<br /><br />    public String getReadonly() {<br />        return readonly;<br />    }<br />}<br />&nbsp;

This class intermediates between the selectSwicther tag for this JSF component in the JSP page and the UIComponent class. It handles a number of attributes we can define for the tag in the JSP page and makes them available to the UIComponent class, dealing with straight values as well as ValueBindings.

2. Describe the Tag in the Tag Library Description

The TLD file has the following entry:

  &lt;tag&gt;<br />    &lt;description&gt;Switch between Radiobuttons and Dropdownlist depending on number of select items&lt;/description&gt;<br />    &lt;display-name&gt;Switch Select Component&lt;/display-name&gt;<br />    &lt;name&gt;SelectSwitcher&lt;/name&gt;<br />    &lt;tag-class&gt;nl.amis.jsf.tags.SelectSwitcherTag&lt;/tag-class&gt;<br />    &lt;body-content&gt;JSP&lt;/body-content&gt;<br />    &lt;attribute&gt;<br />      &lt;name&gt;id&lt;/name&gt;<br />    &lt;/attribute&gt;<br />    &lt;attribute&gt;<br />      &lt;name&gt;rendered&lt;/name&gt;<br />    &lt;/attribute&gt;<br />    &lt;attribute&gt;<br />      &lt;name&gt;readonly&lt;/name&gt;<br />    &lt;/attribute&gt;<br />    &lt;attribute&gt;<br />      &lt;name&gt;disabled&lt;/name&gt;<br />    &lt;/attribute&gt;<br />    &lt;attribute&gt;<br />      &lt;name&gt;binding&lt;/name&gt;<br />    &lt;/attribute&gt;<br />    &lt;attribute&gt;<br />      &lt;name&gt;value&lt;/name&gt;<br />    &lt;/attribute&gt;<br />    &lt;attribute&gt;<br />      &lt;name&gt;switchIndex&lt;/name&gt;<br />    &lt;/attribute&gt;<br />    &lt;example&gt;Brief snippet showing how to use this tag.&lt;/example&gt;<br />  &lt;/tag&gt;<br />&nbsp;

3. Register the JSF Component in the faces-config.xml file

All JSF components need to be registered in a faces-config.xml file. Either the main one in WEB-INF, or one in a META-INF subdirectory of one of the JAR files in WEB-INF\lib. Or in one of the files listed in the context parameter javax.faces.CONFIG_FILES in the web.xml file. The component registration for SelectSwitcher looks like this:

  &lt;component&gt;<br />    &lt;component-type&gt;SelectSwitcher&lt;/component-type&gt;<br />    &lt;component-class&gt;nl.amis.jsf.HtmlSelectSwitcher&lt;/component-class&gt;<br />  &lt;/component&gt;<br />&nbsp;

The important stuff: the HtmlSelectSwitcher UIComponent class

Now I will describe my intention with the SelectSwitcher: I want to be able to insert two SelectOne… components in my JSP page – one for small sets of values and one for large sets. I want to be able to easily define, both with a hard-coded value as well as with a valuebinding, what the number of allowable values is that specifies the boundary between a small set and a large set.

I only want to define the SelectItem and SelectItems elements – with the allowable values themselves – once. Other attributes such as readOnly, rendered and JavaScript eventhandlers should also be defined only once. Ideally I should be able to define a special case for the situation of readOnly==true. In that situation, I do not want to display a SelectOne…, but instead perhaps an OutputText that displays the Label (not the value!) that is currently set.

It would be nice if the small-set-of-values and large-set-of-values situations would allow me to do a little bit more than only specify a SelectOne.. element. It would be even better to be able to define a container element, labels etc. For example by using a facet for each situation…

Well, we have already seen the final result that could be used in this way:

&lt;AMISJSF:SelectSwitcher id=&quot;colorSelectSwitcher&quot;                                 <br />      readonly=&quot;#{DemoSelectSwitcherBean.readOnly}&quot;                              <br />      switchIndex=&quot;#{DemoSelectSwitcherBean.switchValue}&quot;                        <br />      value=&quot;#{DemoSelectSwitcherBean.selectValue}&quot;&gt;                             <br />  &lt;f:facet name=&quot;small&quot;&gt;                                                         <br />    &lt;h:panelGrid columns=&quot;2&quot;&gt;                                                    <br />      &lt;af:outputText value=&quot;Choose a color from this small selection&quot;/&gt;          <br />    &lt;af:selectOneRadio layout=&quot;vertical&quot;/&gt;                                       <br />    &lt;/h:panelGrid&gt;                                                               <br />  &lt;/f:facet&gt;                                                                     <br />  &lt;f:facet name=&quot;large&quot;&gt;                                                         <br />    &lt;h:panelGrid columns=&quot;2&quot;&gt;                                                    <br />      &lt;af:outputText value=&quot;Choose a color from this long list&quot;/&gt;                <br />      &lt;af:selectOneChoice /&gt;                                                     <br />    &lt;/h:panelGrid&gt;                                                               <br />  &lt;/f:facet&gt;                                                                     <br />  &lt;f:facet name=&quot;readonly&quot;&gt;                                                      <br />    &lt;h:panelGrid columns=&quot;2&quot;&gt;                                                    <br />      &lt;af:outputText value=&quot;Here is the read only value for the selected color&quot;/&gt;<br />      &lt;af:outputText value=&quot;READONLY&quot; id=&quot;here&quot;/&gt;                                <br />    &lt;/h:panelGrid&gt;                                                               <br />  &lt;/f:facet&gt;
                          <br />  &lt;f:selectItem itemLabel=&quot;Blue&quot; itemValue=&quot;XXX&quot;/&gt;                               <br />  &lt;f:selectItem itemLabel=&quot;Yellow&quot; itemValue=&quot;YYY&quot;/&gt;                             <br />  &lt;f:selectItem itemLabel=&quot;Green&quot; itemValue=&quot;GGG&quot;/&gt;                              <br />  &lt;f:selectItem itemLabel=&quot;Red&quot; itemValue=&quot;RRR&quot;/&gt;                                <br />  &lt;f:selectItem itemLabel=&quot;White&quot; itemValue=&quot;WWW&quot;/&gt;                              <br />&lt;/AMISJSF:SelectSwitcher&gt;                                                        <br />&nbsp;

Here we see the three facets for the cases of readOnly, small set and large set. Each facet can contain one component, which can be a container such as panelGrid that in turn can contain many elements.

The allowable values are defined just once, as child elements of SelectSwitcher. They will be assigned to the active SelectOne component at run time. Note that even though in this snippet we have a set of static selectItem elements, they might just as well be dynamic – bound – selectItems elements.

The SelectSwitcher element has a value attribute, that has been specified with a ValueBinding, an EL expression that binds it to the selectValue property in the DemoSelectSwitcherBean. At run time, this value binding will be transfered to the active SelectOne.. component.

The switchIndex attribute is the one we can use to specify the boundary between a small set of allowable values and a big one. If the actual set of values has more elements than switchIndex, the SelectSwitcher will render the large facet. If the number of elements is less than or equal to switchIndex, the small facet is rendered. Note that if readOnly evaluates to true, neither small nor large is rendered but the readOnly facet instead, if it exists. In that case, the SwitcherElement will look for a UIComponent inside the readOnly facet with id equals here. It will set the Label for the current value to the value attribute of this UIComponent.

The code for the HtmlSelectSwicther class:

package nl.amis.jsf;<br /><br />import java.io.IOException;<br /><br />import java.util.ArrayList;<br />import java.util.List;<br /><br />import javax.faces.component.EditableValueHolder;<br />import javax.faces.component.UIComponent;<br />import javax.faces.component.UIComponentBase;<br />import javax.faces.component.UIInput;<br />import javax.faces.component.UISelectItem;<br />import javax.faces.component.UISelectItems;<br />import javax.faces.component.ValueHolder;<br />import javax.faces.context.FacesContext;<br />import javax.faces.convert.Converter;<br /><br />import oracle.adfinternal.view.faces.renderkit.uix.SelectItemSupport;<br /><br /><br />public class HtmlSelectSwitcher extends UIComponentBase implements ValueHolder {<br />    public HtmlSelectSwitcher() {<br />    }<br /><br />    public static final String FACET_SMALL = &quot;small&quot;;<br />    public static final String FACET_LARGE = &quot;large&quot;;<br />    public static final String FACET_READONLY = &quot;readonly&quot;;<br /><br />    public static final String ATTRIBUTE_READONLY = &quot;readonly&quot;;<br />    public static final String ATTRIBUTE_SWITCHINDEX = &quot;switchIndex&quot;;<br /><br />    public void encodeEnd(FacesContext context) throws IOException {<br />    }<br /><br />    public void encodeChildren(FacesContext FacesContext) throws IOException {<br />        super.encodeChildren(FacesContext);<br />    }<br /><br />    public void encodeBegin(FacesContext FacesContext) throws IOException {<br />        super.encodeBegin(FacesContext);<br />        String switchIndexString = (String)getAttributes().get(ATTRIBUTE_SWITCHINDEX);<br />        Integer switchIndex = new Integer(switchIndexString);<br />        if ((switchIndex == null) || (switchIndex.intValue() == 0)) {<br />            switchIndex = new Integer(5);<br />        }<br />        List kids = getChildren();<br />        int childCount = this.getChildCount();<br />        List siList = new ArrayList();<br />        int sicount = 0;<br />        for (int i = 0; i &lt; childCount; i++) {<br />            if (kids.get(i) instanceof UISelectItem) {<br />                siList.add((UISelectItem)kids.get(i));<br />            } else if (kids.get(i) instanceof UISelectItems) {<br />                siList.add((UISelectItems)kids.get(i));<br />            }<br />        }<br />        // this next method calls is only meaningful if this implements ValueHolder<br />        sicount = SelectItemSupport.getSelectItemCount(this);<br /><br />        Boolean readonly = Boolean.FALSE;<br />        if (getAttributes().containsKey(ATTRIBUTE_READONLY)) {<br />            Object ro = getAttributes().get(ATTRIBUTE_READONLY);<br />            if (ro instanceof String) {<br />                readonly = new Boolean((String)ro);<br />            } else if (ro instanceof Boolean) {<br />                readonly = (Boolean)ro;<br />            }<br />        } else if (getValueBinding(ATTRIBUTE_READONLY) != null) {<br />            readonly = <br />                    (Boolean)getValueBinding(ATTRIBUTE_READONLY).getValue(FacesContext);<br />        }<br /><br /><br /><br />        UIComponent select = null;<br />        UIComponent facet = null;<br />        Object facets = this.getFacets();<br />        facet = findFacet(FACET_LARGE, this);<br />        select = findFirstInput(facet);<br />        // if rendered is not false, the input component would still try do decode itself<br />        // and would reset the value in doing so<br />        select.setRendered(false);<br />        facet = findFacet(FACET_SMALL, this);<br />        select = findFirstInput(facet);<br />        select.setRendered(false);<br /><br /><br />        if (readonly) {<br />            facet = findFacet(FACET_READONLY, this);<br />            UIComponent target = facet.findComponent(&quot;here&quot;);<br />            facet.setRendered(true);<br />            facet.getAttributes().put(&quot;title&quot;, <br />                                      &quot;This is the panelgrid for the readonly situation&quot;);<br />            Object value = getAttributes().get(&quot;value&quot;);<br />            if (getValueBinding(&quot;value&quot;) != null) {<br />                value = getValueBinding(&quot;value&quot;).getValue(FacesContext);<br />            }<br />            for (UISelectItem si: (List&lt;UISelectItem&gt;)siList) {<br />                if ((si.getItemValue()).equals(value)) {<br />                    target.getAttributes().put(&quot;value&quot;, si.getItemLabel());<br />                    break;<br />                }<br />            }<br />        } else {<br />            if (sicount &gt; <br />                switchIndex.intValue()) { // larger than cut-off value<br />                facet = findFacet(FACET_LARGE, this);<br />                select = findFirstInput(facet);<br />            } else {<br /><br />                facet = findFacet(FACET_SMALL, this);<br />                select = findFirstInput(facet);<br />            }<br /><br /><br />            select.setRendered(true);<br />            copyAttribute(ATTRIBUTE_READONLY,&quot;readOnly&quot;, this, select);<br />            copyAttribute(&quot;value&quot;,&quot;value&quot;, this, select);<br />            <br />            int num = select.getChildCount();<br />            for (int i = 0; i &lt; num; i++) {<br />                select.getChildren().remove(0);<br />            }<br />            select.getChildren().addAll(siList);<br />        }<br />        // now make sure that the facet gets rendered, along with its children<br />        facet.encodeBegin(FacesContext);<br />        facet.encodeChildren(FacesContext);<br />        facet.encodeEnd(FacesContext);<br />    }<br /><br />    private UIComponent findFacet(String facetName, UIComponent component) {<br />        // note: getFacets.get() returns the (only) UIComponent within the Facet!!<br />        return (UIComponent)component.getFac

ets().get(facetName);<br />    }<br /><br />    private UIComponent findFirstInput(UIComponent component) {<br />        if (component == null)<br />           return null;<br />        if ((component instanceof EditableValueHolder)||(component instanceof UIInput))<br />            return component;<br />        for (UIComponent child: (List&lt;UIComponent&gt;)component.getChildren()) {<br />            if ((child instanceof EditableValueHolder) ||(child instanceof UIInput))<br />                return child;<br />            else {<br />                UIComponent input = findFirstInput(child);<br />                if (input != null)<br />                    return input;<br />            }<br />        }<br />        return null;<br />    }<br /><br />    private void copyAttribute(String sourceAttributeName, String targetAttributeName, UIComponent sourceComponent, UIComponent targetComponent) {<br />        if (sourceComponent.getAttributes().containsKey(sourceAttributeName)) {<br />            targetComponent.getAttributes().put(targetAttributeName, sourceComponent.getAttributes().get(sourceAttributeName));<br />        } else if (sourceComponent.getValueBinding(sourceAttributeName) != null) {<br />            targetComponent.setValueBinding(targetAttributeName, <br />                                   sourceComponent.getValueBinding(sourceAttributeName));<br />        }<br />    }<br /><br /><br />    public Object getLocalValue() {<br />        return null;<br />    }<br /><br />    public Object getValue() {<br />        // get value from rendered child<br />        return null;<br />    }<br /><br />    public void setValue(Object object) {<br />        // pass value to rendered child??<br />    }<br /><br /><br />    public void decode(FacesContext facesContext) {<br />        super.decode(facesContext);<br />    }<br /><br />    public Converter getConverter() {<br />        return null;<br />    }<br /><br />    public void setConverter(Converter converter) {<br />    }<br /><br />    public String getFamily() {<br />        return null;<br />    }<br />}<br />&nbsp;

Going over the code – and it is by no means polished and perfect – the important things are:

1. Determine the number of SelectItem children in the SelectSwitcher element by calling the SelectItemSupport.getSelectItemCount() method

2. Build a Collection with SelectItem children – to transfer eventually to the SelectOne.. element on duty

3. Check whether the SelectSwitcher element is set to readonly

4. Set the first Input Element in both Large and Small facet to non-rendered (we assume the first Input element is in fact the SelectOne… in those facets); we set them to non-rendered to be sure that later on the SelectOne.. element that is not on duty is not accidentally seen as rendered as therefore will decode its value and update its ValueBinding, resetting it since the SelectOne.. element not on duty will not find its value in the request.

5. If the SelectSwitcher is readonly, locate the readonly facet; locate the element with ID equals here and set the value of that element equal to the label for the current value of the SelectSwitcher; the label is determined by looking for the value among the SelectItem elements and taking the ItemLabel for the SelectItem whose ItemValue matches.

6. If not read only, then if we are looking at a large set of values, find the first UIInput element in the large facet. Else find the first UIInput in the small facet.

7. Set the UIInput thus identified to rendered is true, copy the value and readOnly attributes as well as all SelectItem children from the SelectSwitcher element to this UIInput and have the selected facet render itself and its children.

During decode, the SelectSwitcher itself has to do nothing. All of its rendered child components will do their own decode, and thereby intercept a potential new value in the current SelectOne.. element. 

 

Note: in the code snippet above, there are references to a bean. This bean is configured in the faces-config.xml file as follows:

  &lt;managed-bean&gt;<br />    &lt;managed-bean-name&gt;DemoSelectSwitcherBean&lt;/managed-bean-name&gt;<br />    &lt;managed-bean-class&gt;nl.amis.jsf.beans.DemoSelectSwitcherBean&lt;/managed-bean-class&gt;<br />    &lt;managed-bean-scope&gt;session&lt;/managed-bean-scope&gt;<br />  &lt;/managed-bean&gt;<br />&nbsp;

and its implementation is this: 

package nl.amis.jsf.beans;<br /><br />public class DemoSelectSwitcherBean {<br /><br />    Object selectValue;<br />    String switchValue=&quot;4&quot;;<br />    Boolean readOnly= new Boolean(&quot;false&quot;);<br /><br /><br />    public DemoSelectSwitcherBean() {<br />    }<br /><br />    public void setSelectValue(Object selectValue) {<br />        this.selectValue = selectValue;<br />    }<br /><br />    public Object getSelectValue() {<br />        return selectValue;<br />    }<br /><br />    public void setSwitchValue(String switchValue) {<br />        this.switchValue = switchValue;<br />    }<br /><br />    public String getSwitchValue() {<br />        return switchValue;<br />    }<br /><br />    public void setReadOnly(Boolean readOnly) {<br />        this.readOnly = readOnly;<br />    }<br /><br />    public Boolean getReadOnly() {<br />        return readOnly;<br />    }<br />}<br />&nbsp;

The entire JDeveloper Application in which I created and tested this component will be available for download shortly. 

Resources

Some resources you may find helpful when it comes to developing custom JSF components:

Exadel’s Tutorial – How to Write your own JSF Components

Richard Hightower on IBM Developer Works: JSF for nonbelievers: JSF component development

Building Custom Java Server Faces UI Components by Chris Schalk (on The Server Side)

 

 

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

Comments are closed.