Composite Components in JSF 2.0

Since JSF 2.0, it’s very easy to create a reusable component, known as composite components. In this tutorial, we show you how to create a simple composite components (stored as “register.xhtml“), which is an user registration form, includes name and email text fields (h:inputText) and a submit button (h:commandButton). In addition, we also show you how to use it.

The composite:interface tag is used to declare the configurable values which are expose to the developer who use it. And composite:implementation tag is declares all the XHTML markups, which are the content of the composite component, inside the composite:implementation tag, you can access composite:interface attribute with the expression #{cc.attrs.attributeName}.

http://java.sun.com/jsf/composite/folder-name-in-resources-folder The folder name of the composite components is defined the component access path, for example, if you put your “register.xhtml” file under folder named “abc”, then you should access it like this :

You are allowed to pass either hard-coded value or backing method or property into the composite component via exposed attributes, when the form is submitted, JSF will do all the backing bean binding automatically.

P.S Here’s the “user” managed or backing bean, for those who are interested.

Hi mkyong, I depend a lot on your blogs. I am currently trying to develop composite components. However after doing some basic stuff I was looking for the schema for http://java.sun.com/jsf/composite namespace and could not get it. That would help me in getting IDE tag completion etc. Can you send me any pointers regarding the same.

regards

Clement

For all those whose page has the inclusion of the composite element having the error that “the name space cannot be found”, you MUST create the resources folder under your “Web Pages” folder. It has to be the same level as your WEB-INF folder. All along I had the impression that the “resources” folder was optional; and was specific to certain types of project. I was using the Java Enterprise application which wasn’t so explicit.

Thank you mkyong too~

Saleh

Thank you for all these tutorials. Many Google searches about Java EE results your website. It is very helpful.

I also work with composite components, but in combination with other fields and text outside the component. What I do is… I put a table on the jsf page that has 2 columns and multiple rows. The first column is a component and the second column contains plain text. When you submit the page without ajax and come back to the same page than all components are placed after the text. It is as jsf parses first all normal tags, text,… and after that the components. Pressing F5 puts everything back normal.

I works like a charm, now is much much easier to built my own composite components.

Thank you for the post.

Haolin

I tried the example in both JSF 2.0/Weblogic and Mojarra JSF 2.1.11/Tomcat. But cc.attrs cannot be evaluated. Did you encounter this problem before? Have your code been tested in the environments listed above yet?