This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
I'm making good progress with my first JSF application, and one requirement I have is to have a page with an arbitrary number of names, like a sign up sheet. There can be 0 names to several hundred, depending on how many names the user cares to enter. This was easy to do in Stripes/Struts, but I haven't figured out the JSF equivalent.
I know that JSF web page variables are tied to a back bean. So for example, if I have the below code in the backing bean:
And the front end:
Now I have 10 fields to input names - but what strategy can I employ to add new fields dynamically as the user enters them?
When I did it in Stripes, what I'd do is clone the inputText using jQuery with a pattern, like this:
However, this doesn't seem to work in JSF, if you create new DOM input elements following the pattern of existing JSF generated input elements, JSF isn't going to expand the array holding the list of names upon submit.
Is there a way to accomplish having a growing list of input fields in JSF? I'd like to avoid needing an ajax call to the server just to allocate a new String to the name array and needing to refresh the page.
I don't how to do this, but, to start with you should have an id for the datatable, like:
Many JSF implementations use jQuery behind the scenes, so, you can check if jQuery is already available for you. If not, you can still include it. And then, you can use the above reference to try and see if you can add components.
I earlier did an implementation with JSF 1.2 where a part of the page would be dynamically built. I did it with:
and then in the backing bean, like:
But, in my case, I knew what had to be built before the page was rendered. I am not sure if you can do this without a AJAX request or page refresh.
Derrick Williams wrote:...JSF isn't going to expand the array holding the list of names upon submit.
Regardless of JSF, an ArrayList will grow/shrink dynamically as you add/remove items, so my understanding of your question is a bit confused there. Anyways, if you use the f:ajax tag you can keep adding names to the list without refreshing the page (in a conventional sense) using one input field. I don't know why you would want more than one input field if you can just keep adding and submitting names using just one, unless you have an ulterior motive of course
Front end xhtml page:
I don't know if that is something like what you are after.