treeno
treeno

Reputation: 2600

Subclassing UISelectMany to create a custom JSF Component

I need to create a custom Component with JSF 2.0 (not composite component), that is similar to a SelectManyCheckbox, but has a different UI. I try to do this by subclassing UISelectMany and creating a new custom Renderer.

What I don't understand is, how is the input data on the client passed through the Component into the Bean. There must be a way to collect the http params from the POST, put it in a List and set the List in the Bean.

edited

What I already tried is similar to this:

public void decode(FacesContext context, UIComponent component) {
    if ((context == null) || (component == null)) {
        throw new NullPointerException();
   }
   MapComponent map = (MapComponent) component;
   String key = getName(context, map);
   String value = (String)context.getExternalContext().
       getRequestParameterMap().get(key);
   if (value != null)
      map.setCurrent(value);
   }
}

I can see in the debugger, that my http-params are fetched correctly. I can also see, that my custom Converter will be called after that. But the values do not reach the Bean. But I can see a unspecific validation error in the log. After reading some tutorials on the net I still don't understand how this works. There must be some glue-code, that takes the converted value and passes it to the setter in the Bean. Does anybody know how this works?

Thanks Jan

Upvotes: 0

Views: 425

Answers (2)

BalusC
BalusC

Reputation: 1108762

There must be a way to collect the http params from the POST, put it in a List and set the List in the Bean.

This is normally to be done in decode() method of the Renderer class. In case of Mojarra, it's the com.sun.faces.renderkit.html_basic.MenuRenderer class. Just download the source and peek in there how it's to be done.

Basically, you just grab the request parameter values associated with the component's client ID as parameter name and then set it as submitted value by the UIInput#setSubmittedValue().

public void decode(FacesContext context, UIComponent component) {
    ((UIInput) component).setSubmittedValue(context.getExternalContext().getRequestParameterValuesMap().get(component.getClientId(context)));
}

(of course you need to do some validation beforehand; again, check the original source code)

Upvotes: 1

Rajesh Pantula
Rajesh Pantula

Reputation: 10241

Create your Own class which extends javax.faces.component.UISelectMany

class MySelectMany extends javax.faces.component.UISelectMany
{

//over-ride methods of javax.faces.component.UISelectMany


// this method is inherited from javax.faces.component.UIComponentBase. You can use this method to set the rendererType

public void setRendererType(java.lang.String rendererType)
{

// custom rendering
}

}

Upvotes: 0

Related Questions