Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I create form controls dinamically with DHTML and deal all those with JSF??  RSS feed

 
Hugo Alberto Bedolla
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

As I understand, with JSF I need to associate one control with one UIComponent by declaring the control within the JSP with one tag and therefore, to attach any either or both validator or converter to this control, so the RootView knows the control is there and has an associated name, and it will expect to manage that control... but I need not a JSP-declared control, but one created by the client dinamically with JavaScript, later sent to the server and treated as any other JSP-declared UIComponent, so I can relaunch my question as: How do I deal with controls not declared within any JSP and built dinamically by the client with DHTML, for validation, convertion and processing?

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 .

Thanks so much for your feedback in advance .
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
¡Bienvenidos!

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.

If you were to try and manipulate the dataGrid control with brute-force JavaScript/DHTML, you'd have a problem, because each element in the table has to be bound to a model object in the backing bean or the validation/update/action sequence of JSF, so you're pretty much have to use AJAX to update the model and the JSF component tree, and it would be much uglier.

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!