aspose file tools*
The moose likes JSF and the fly likes How to make an arbitrary number of input fields in JSF? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to make an arbitrary number of input fields in JSF?" Watch "How to make an arbitrary number of input fields in JSF?" New topic
Author

How to make an arbitrary number of input fields in JSF?

Derrick Williams
Greenhorn

Joined: Nov 27, 2010
Posts: 9
Hi All-

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.

Any hints gratefully appreciated!

-Derrick

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1096
    
  10

I don't how to do this, but, to start with you should have an id for the datatable, like:
Once, you have an id, that can be referenced from a Javascript, like:
where 'myForm' is my form's name. If you don't provide an id, JSF will generate an id, but that will be dynamically vary, so you can't reliably reference it in a Javascript.

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.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 162

Derrick Williams wrote:...JSF isn't going to expand the array holding the list of names upon submit.


Hi Derrick,

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

Try this:

Backing bean:



Front end xhtml page:



I don't know if that is something like what you are after.

Alan
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16313
    
  23

The main thing to keep in mind is that the "var" attribute of a dataTable or related multi-element display tag is supposed to refer to a class object, not a unitary object.

So "#{name}" as an input control value isn't going to work very well. Remember, String objects are immutable!

You need a mutable object here, even if it's nothing more than a class that defines a single String property.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
wood burning stoves
 
subject: How to make an arbitrary number of input fields in JSF?