How to use Component class in JSF2 - jsf

I'm developing custom jsf2 component, that has an datasource attribute. There are some operations on datasource, that belongs to VIEW. For instance, there is a method that returns some image for column header, if the table is sorted by the column, and other image if it doesn't. In JSF1, each component has its Java class, extending UIComponent, so I can implement such methods there. However, I see that in JSF2, component class is created automatically from xhtml, so I doesn't know how to add methods to it. Can anybody explain me how to do it? s it possible to use both composite component and component class in one component?

In JSF 2 you can create component classes in Java in the same way you do in JSF 1 (the API may have changed a bit, but it is mostly the same. For example, look at http://weblogs.java.net/blog/driscoll/archive/2009/10/09/jsf-2-custom-java-components-and-ajax-behaviors

Related

How to specify the type of wrapper for JSF custom component?

I need to create the custom component as SelectOneRadio, but it should not has wrapper <table> But, <ul>, <li> for example. How I can specify the type of wrapper, ul, table or something else, when creating JSF custom components?
There seems to be a conceptual misunderstanding. There's no such thing as "wrapper" in this context. HTML is just generated by the Renderer associated with the UIComponent. If you'd like to change the generated HTML representation, just supply your own custom Renderer.
That said, it's advisable to learn JSF by a decent book so that you get the basic concepts properly right from the beginning on. Creating custom components is already covered in such a book.
See also:
How to override h:selectOneRadio renderer? Where is the renderer class in jsf-impl?
How do I determine the renderer of a built-in component

How do I bind a composite component in JSF?

I have a composite component which is composed of other composite components. Now I would like to render only some of the child components within the parent component. From the Java EE tutorial I take it that I should probably bind my child components to some property of the backing bean for my parent component so that I can access their render attribute.
However, the NetBeans IDE does not know about a binding attribute for my composite components. So how am I supposed to do this?
Use case: The parent component is some wizard which should display only one of the child components at a time. Think of it as a poor man's tabbed view. I know there are libraries which provide ready-made components for this, but I do not want to add another dependency.
Netbeans is lying. That attribute is definitely supported on <ui:component>. Just use it and ignore the warning/error in the IDE. Or better, just use the rendered attribute; the need to conditionally render components is not a valid reason to prefer binding over rendered.

How to programmatically generate JSF components at run time?

I've got to create screens to display a lot of JPA entities in the View. It would be great to create one facelet and pass to it a collection of fields e.g. List<Object>.
The facelet/custom component would need to convert each element of the list into the appropriate tag for display e.g. an enum field to h:selectOneMenu, String field to h:inputText, etc. This would need to be done at run time.
What's the easiest way to do this?
Worked on a project previously that created entire pages dynamically from stored configuration. There are two basic things you need
A BackingBean. You'll used this to get access to the UIComponent on the facelet which will act as the parent to the generated UIComponents. Something like a panelGroup. But, you'll need to bind the UIComponent to the backing bean, in order to have a parent against which you'll add the dynamically-created UIComponents
Access to the Application component. Typically FacesContext.getApplication() (I worked on this in JavaEE 5, so it might look a little different with injection). Once you have the Application component, you call the createComponent method, passing in the type of component you want to create.
It then becomes an activity of creating components dynamically, configuring them in code and adding them to the parent UIComponent defined via a binding bean. It can be tricky, but it can be done.

jsf 1.2 UI Component tag backing bean

I am using JSF 1.2 and I want to create a custom component using ui:component tag. I need to use this component in many places in my application, so I want this component to be highly reusable.
The problem is that for each use of this custom component I need a backing bean to store my information and act as action listener for certain events. I use this component in about 15 places within my application, and I also use this component more than once within a single page.
I don't want to create a custom JSF component (with UIComponent, renderer etc) and I don't know how to manage the backing data.
Is there any way I can specify a class, and the custom component creates an instance of the class each time I use it?
Edit:
Is it possible to extend an existing JSF component? (I use Richfaces implementation)
Any help or idea are welcome.

Custom components in JSF - what about sub-components (children)

When Im creating custom component in JSF do I have to create all sub-components in that same way?
For example:
h:dataTable component use h:column to determinate column parameters
Now if I want to create dataTable component do I have to implement in that same way a column component?
(PS. "that same way" mean:
create DataTable class extends i.e. UIOutput
create DataTableTab class extends UIComponentELTag
append configuration to
custom.taglib.xml
In general the answer is no. If you create a complex component like a datatable, it can simply take advantage of the existing components for defining columns.
As the designer of such a dataTable, you are the one who determines what's needed. The standard column afaik has no knowledge of the dataTable, but the dataTable has knowledge about the columns.
Also note that you don't often need to create tag classes. This is only needed for JSP compatibility, but as JSP is deprecated I would advise you to not support it unless you really have to.

Resources