How do I create form controls dinamically with DHTML and deal all those with JSF??
Hugo Alberto Bedolla
Joined: Nov 09, 2010
Hi everybody, greetings from México :
Suppose this next situation: making a view where the form controls are rendered with DHTML (that is, each time the user press a button, a new row with fields is created and field identifiers are both generated and asigned to the fields with the same DHTML as 'form1control1' and so on), but where that controls are not related yet to any UIComponent in the ViewRoot (as they are created dinamically by the client and so the server don't know they are in the form) how can I deal with a) finding that controls and b) using that names for validation and processing within JSF?
With a JSP approach, that controls names & values are coming with the HttpRequest and is easy to deal with that items by calling the request object and using an algorythm that search all the objects with a pattern, so where is an object named 'form1control1' is there, I will find it by using i.e. request.getParameterNames() and request.getParameter("form"+N+"control"+M), for later dealing with making validations and convertions by using code for all the named parameters.
I'm supposing I can do it with AJAX by calling the ViewRoot any time I do a new row with new controls and updating it with adding any control programatically to the three, but, I think that approach is very expensive as I need a) generate an AJAX request, b) to give work to the server anytime I do an action withing the form that implies using processing time and assigning memory resources; so, preffered, I would desire to avoid using AJAX for updating the ViewRoot with any new control created/deleted, Obviously I'm not closed if it is mandatory to use AJAX for updating the ViewRoot to reach that behavior. If I can avoid this last approach, it is too welcome .
Unless you really, really, really have your heart set on DHTML, I recommend your take a cleaner approach.
The easy way to add rows to a table is to simply add rows to the dataTable's model object and redisplay the dataTable, either by a whole-page refresh or a table-only (AJAX) refresh. It's very easy to code and maintain, especially if you use AJAX-supportive frameworks such as RichFaces.
JSF is a pretty abstract framework. One obvious indicator of that is the difficulty you will find in directly accessing incoming HTML form components. It's far, far easier to let JSF set the (¡validated!) data automatically than to code up stuff to pull it by brute force from the raw underlying input.
In fact, you'd probably be better off coding a non-JSF page and feeding it to a servlet or JSP using jQuery if DHTML is a major consideration.
Customer surveys are for companies who didn't pay proper attention to begin with.